我正在尝试将Google Analytics(分析)和autotrack library添加到Chrome扩展程序中。
并且特别想使用插件if (userAllowTracking) {
//create & inject the user websites script;
var script = $('<script/>');
$(script).attr('src', chrome.extension.getURL('path/to/injected.js') + '?clientId=' + myClientId);
$('head').prepend(script);
//create & inject the autotrack plugin script;
var scriptPlugins = $('<script/>');
$(scriptPlugins).attr('src', chrome.extension.getURL('path/to/autotrack/2.4.1/autotrack.js'));
$(scriptPlugins).attr('async', true);
$(script).after($(scriptPlugins));
}
来跟踪处于可见状态的页面多长时间。
为此,我将代码和GA链接插入到页面的上下文中,如下所示:
injected.js
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics_debug.js','ga');
中:我插入了GA代码段,该代码段注入了主脚本:
window.ga_debug = {trace: true};
我正在使用上面的调试链接,并将模式设置为调试后言,以获取详细的日志:
ga('create', {
'trackingId': 'UA-AAAAAAAA-A',
'name': myTracker,
'clientId': myClientId,
});
然后,我创建跟踪器:
ga(myTracker + '.set', {
'page': (document.location.protocol !== 'file:') ? '/site/' + document.location.hostname : '/site/local-file-page',
'title': (document.location.protocol !== 'file:') ? '/site/' + document.location.pathname : '/site/local-file-title',
'location': (document.location.protocol !== 'file:') ? '/site/' + document.location.href : '/site/local-file-location',
'transport': 'beacon',
'anonymizeIp': true,
'sendHitTask': null,
});
设置一些设置:
ga(myTracker + '.require', 'pageVisibilityTracker', {
'sendInitialPageview': true,
'pageLoadsMetricIndex': 2,
'visibleMetricIndex': 1,
});
并需要插件:
Parent:
...
print "blabla"
kill(pid_of_child,signal)
...
我在控制台中遇到以下错误:
创建插件实例时出错:{0:“ myTracker.require”,1:“ pageVisibilityTracker”,2:{sendInitialPageview:true,pageLoadsMetricIndex:2,visibleMetricIndex:1}}
analytics_debug.js:23
这对我来说似乎是个模糊的错误,我不知道为什么会发生或如何解决?
我在做什么错了?