您可以使用 Google Analytics 代码的包含文件吗?
我将分析代码移至 JavaScript 包含文件中,但现在它似乎无法跟踪。任何人都可以阐明这一点吗?
Analytics.js 的内容
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_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);
})();
包括代码
<script src="js/analytics.js" type="text/javascript"></script>
I moved my analytics code to a javascript include file and now it does not seem to be tracking. Can anyone shed any light on this?
Contents of analytics.js
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-1']);
_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);
})();
Include code
<script src="js/analytics.js" type="text/javascript"></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,您可以将分析代码包含在外部 JavaScript 文件中。您如何确定它不起作用?根据您使用的浏览器,您应该能够查看 DOM 源并查看
是否已附加到文档中。
您应该看到类似以下内容:
Yes, you can include the analytics code in an external JavaScript file. How are you determining that it is not working? Depending on the browser you are using, you should be able to view the DOM source and see if the
<script>
was appended to the document.You should see something along the lines of:
我通常在页眉或页脚文件中添加 GA 跟踪代码,然后在页面上包含(页眉、页脚)文件。
页面加载时,您可以通过查看页面源来验证 GA 代码。 (右键单击)
I usually add the GA tracking code in the header or footer file and then I include (header, footer) file on the page.
When you page loads, you can verify the GA code by looking the source of the page. (Right click)