简体   繁体   English

我可以将Google分析放在外部JS吗?

[英]Can I put Google analytics in external JS?

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-123-2']);
  _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>

Apart from caching issues (if I change my tracking ID), is there anything else I need to be aware of? 除了缓存问题(如果我更改我的跟踪ID),还有什么我需要注意的吗? Will it still function? 它还能运作吗? It recommends putting it before </head> , all my other JS is before </body> , is it ok to put it there? 它建议把它放在</head>之前,我所有的其他JS都在</body>之前,是否可以把它放在那里?

Besides potentially needing to change the tracking code, the only potential issue I can think of is that, in the scheme of things, visitors who leave between when that file is requested and when it arrives will not be tracked. 除了可能需要更改跟踪代码之外,我能想到的唯一潜在问题是,在该方案中,不会跟踪在请求该文件和何时到达之间的访问者。 Or, if for some bizarre reason, the external script fails to load, you will not have tracked that user. 或者,如果由于某些奇怪的原因,外部脚本无法加载,您将无法跟踪该用户。 Besides that, you're safe to include it in an external script. 除此之外,您可以安全地将其包含在外部脚本中。

All this code really does is find the first script element in the DOM and puts a new script element just before it. 所有这些代码确实是找到DOM中的第一个脚本元素,并在它之前放置一个新的脚本元素。 The new script element is pretty much equivalent to: 新的脚本元素几乎相当于:

<script type="text/javascript" async src="https://ssl.google-analytics.com/ga.js">

on HTTPS pages, and: 在HTTPS页面上,和:

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

on HTTP pages. 在HTTP页面上。

It works fine anywhere in the page, in head and in body. 它在页面的任何位置,头部和身体都能正常工作。 Also it doesn't slow down your page rendering if it is in head so it doesn't really matter where you put it. 它也不会减慢你的页面渲染速度,如果它在头部,所以你放在哪里并不重要。

The only difference is that when you have it in the head then you can easily connect your Analytics account with Google Webmaster Tools for that page and if it's in the body then you have to use some other form of verification to prove that it is your website. 唯一的区别是,当您将其纳入头部时,您可以轻松地将您的Google Analytics帐户与该网页的Google网站站长工具相关联,如果它位于正文中,那么您必须使用其他形式的验证来证明它是您的网站。

Putting it in an external file would mean one more HTTP request if it is not in the cache and potentially would save just few lines of code if it is in the cache, but then you can't easily change the tracking ID for any given page. 如果它不在缓存中,那么将它放在外部文件中就意味着再多一个HTTP请求,如果它在缓存中,可能只会保存几行代码,但是你不能轻易地更改任何给定页面的跟踪ID 。

Other than that I would be careful with putting it in an external script because it may be against the terms of service. 除此之外,我会小心将它放在外部脚本中,因为它可能违反了服务条款。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM