您可以使用 Google Analytics 代码的包含文件吗?

发布于 2024-12-01 09:46:23 字数 646 浏览 3 评论 0原文

我将分析代码移至 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-12-08 09:46:23

是的,您可以将分析代码包含在外部 JavaScript 文件中。您如何确定它不起作用?根据您使用的浏览器,您应该能够查看 DOM 源并查看

您应该看到类似以下内容:

<SCRIPT type="text/javascript" src="http://www.google-analytics.com/ga.js" async></SCRIPT>

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:

<SCRIPT type="text/javascript" src="http://www.google-analytics.com/ga.js" async></SCRIPT>
你怎么这么可爱啊 2024-12-08 09:46:23

我通常在页眉或页脚文件中添加 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文