javascript-与Google Analytics(分析)一样的ClickTale实作
作者:互联网
我想在Google解析或任何JavaScript中实现这样的功能
时间报告显示访问者与每个单独领域以及整个在线表单互动的时间.较长的交互时间可能意味着特定字段的请求过于复杂.
我该怎么办,请提供一些建议?
解决方法:
您可以跟踪在每个字段上花费的时间,并将其发送到服务器,然后再提交表单.下面的示例跟踪每个字段的焦点时间,并将其存储在字段本身的自定义属性中.在提交表单之前,我们将跟踪数据组合成一个JSON字符串,然后将其发布到服务器,此后,表单提交可以照常进行.
即:
$(document).ready(function() {
$('#id-of-your-form').find('input, select, textarea').each(function() { // add more field types as needed
$(this).attr('started', 0);
$(this).attr('totalTimeSpent', 0); // this custom attribute stores the total time spent on the field
$(this).focus(function() {
$(this).attr('started', (new Date()).getTime());
});
$(this).blur(function() {
// recalculate total time spent and store in it custom attribute
var timeSpent = parseDouble($(this).attr('totalTimeSpent')) + ((new Date()).getTime() - parseDouble($(this).attr('started')));
$(this).attr('totalTimeSpent', timeSpent);
});
});
$('#id-of-your-form').submit(function() {
// generate tracking data dump from custom attribute values stored in each field
var trackingData = [];
$(this).find('input, select, textarea').each(function(i) { // add more field types as needed
// tracking data can contain the index, id and time spent for each field
trackingData.push({index: i, id: $(this).attr('id'), millesecs: $(this).attr('totalTimeSpent')});
});
// post it (NOTE: add error handling as needed)
$.post('/server/trackFieldTimes', {trackingData: JSON.stringify(trackingData)}, function(data) {
// tracking data submitted
});
// return true so that form submission can continue.
return true;
});
});
标签:google-analytics,javascript-events,javascript 来源: https://codeday.me/bug/20191202/2084989.html