通过引用将 Google Analytics 对象传递给函数时出现问题
我为我的 javascript 做了一个帮助器,以便跟踪一些 ajax 事件,这是它的设置的简短版本
analytics:{
active: false,
gaq: null,
init: function(gaq){
this.active = true;
this.gaq = gaq;
$('a[href^=\"http://\"]').live('click', function() {
helper.analytics.trackPageview('/outgoing/' + $(this).attr('href'));
return true;
});
},
trackPageview: function(page){
if(this.active === false){
return;
}
this.gaq.push(['_trackPageview',page]);
}
},
我有通用的谷歌分析设置
<script type="text/javascript">
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_setDomainName', '.example.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready( function() {
helper.analytics.init(_gaq);
});
</script>
但是在控制台中记录 _gaq
会导致目的。记录 helper.analytics.gaq
会生成一个数组,其中附加了新的综合浏览量,但 Google Analytics 中并未跟踪该综合浏览量。
为什么 _gaq 不通过引用传递给助手?
I made a helper for my javascript in order to track some ajax events, here's a short version of what it's set up to be
analytics:{
active: false,
gaq: null,
init: function(gaq){
this.active = true;
this.gaq = gaq;
$('a[href^=\"http://\"]').live('click', function() {
helper.analytics.trackPageview('/outgoing/' + $(this).attr('href'));
return true;
});
},
trackPageview: function(page){
if(this.active === false){
return;
}
this.gaq.push(['_trackPageview',page]);
}
},
And I have the common google analytics setup
<script type="text/javascript">
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_setDomainName', '.example.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
$(document).ready( function() {
helper.analytics.init(_gaq);
});
</script>
However in the console logging _gaq
results in an object. logging helper.analytics.gaq
results in an array, whith new pageviews appended, but the pageview is not being tracked in google analytics.
Why isn't _gaq being passed to the helper by reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建脚本标记时,ga 代码段将
async
属性设置为 true。因此,它将独立于身体加载。您需要将事件处理程序绑定到 ga 脚本标记的 onload 事件。像这样:我还没有测试过这个,但我认为它可能有用。
When creating the script tag, the ga snippet sets the
async
attribute to true. Therefore, it'll load independently from the body. You'll need to bind an event handler to the ga script tag's onload event. Something like so:I've not tested this, but I think it might work.
您在 Crome 开发工具控制台或 Firefox & 中是否看到任何语法错误?萤火虫?
初始脚本标记后有一个
'."
。Do you see any syntax errors either in Crome dev tools console or Firefox & firebug?
You've got a
'."
after the initial script tag.