Google Analytics _trackEvent 问题
我在使用 Google Analytics 和 _trackEvent 时遇到了一些菜鸟麻烦。
在文档中使用它似乎很简单,但我不能这么简单 工作的例子。对 _trackEvent 的调用失败,并显示“TypeError: o is undefined”
对 _trackPageview 的调用成功,我可以在分析仪表板中看到它已更新。
我尝试查看 ga.js 以了解发生了什么 - 只是有一个头痛的表现!
这是我第一次涉足 GA——尤其是自定义事件。该帐户是新的。 一切似乎都已正确设置 - 但我可能不知道是否正确!
看起来很简单 - 但显然我错过了一些东西。 非常感谢任何帮助我摘除眼罩的帮助!
-vs
示例 HTML - 只需要一个跟踪代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker;
try {
pageTracker = _gat._getTracker("UA-XXXXXX-1");
pageTracker._trackPageview();
} catch(err) {
console.log(err.toString());
}
$(document).ready(function() {
try {
pageTracker._trackEvent('my_category', 'my_action', 'my_optional_label', 42);
} catch(err) {
console.log('trackEvent ' + err.toString());
}
});
</script>
</head>
<body>
</body>
</html>
I'm having some noob troubles with Google Analytics and _trackEvent.
Using it seems straight forward in the documentation, but I can't get this simple
example to work. The call to _trackEvent fails with 'TypeError: o is undefined'
The call to _trackPageview is succeeding and I can see it updated in the analytics dashboard.
I tried peeking at ga.js to understand what's up - just have a headache to show for it!
This is my first foray into GA - especially with custom events. The account is new.
Everything seemed to be correctly setup - but I probably wouldn't know if it wasn't!
It seems so simple - but obviously I'm missing something.
Any help with removing my blind-folds is much appreciated!
-vs
Example HTML - only need a tracking code.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker;
try {
pageTracker = _gat._getTracker("UA-XXXXXX-1");
pageTracker._trackPageview();
} catch(err) {
console.log(err.toString());
}
$(document).ready(function() {
try {
pageTracker._trackEvent('my_category', 'my_action', 'my_optional_label', 42);
} catch(err) {
console.log('trackEvent ' + err.toString());
}
});
</script>
</head>
<body>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码块出现菜鸟错误!这是完整版本。
noob error with the code-block! here's the full version.
结果是在调用 _trackEvent 之前需要调用 _initData()。不知道为什么会这样,但现在似乎有效。希望它对其他人有帮助。
修改后的样本..
Turns out one needs to call _initData() before the call to _trackEvent. Not sure why this is the case, but it seems to work now. Hopefully it helps someone else.
The modified sample..