Javascript 将新的 Google Analytics 代码写入外部文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3870345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:26:27  来源:igfitidea点击:

New Google Analytics code into external file

javascriptjqueryhtmlgoogle-analytics

提问by user467666

New Google Analytics code looks like one below:

新的 Google Analytics 代码如下所示:

<script type="text/javascript">

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

How to move the whole new Google Analytics Asynchronous Tracking Code into an external JavaScript file?

如何将全新的 Google Analytics 异步跟踪代码移动到外部 JavaScript 文件中?

I'm asking especially about "var _gaq = _gaq || []; [...]" part because I know it's possible to move the rest e.g.

我特别询问“var _gaq = _gaq || []; [...]”部分,因为我知道可以移动其余部分,例如

index.html

索引.html

<script type="text/javascript">

 var _gaq = _gaq || [];
 _gaq.push(['_setAccount', 'UA-0000000-00']);
 _gaq.push(['_trackPageview']);

</script>
<script src="include.js" type="text/javascript"></script>

include.js

包含.js

function includeGA()
{
 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()
{
 includeGA();
});

I've already tried to place the "var _gaq = _gaq || []; [...]" code into various locations but nothing worked.

我已经尝试将“var _gaq = _gaq || []; [...]”代码放入不同的位置,但没有任何效果。

采纳答案by MrG

This appears to be a similar question to this one: Using Google Analytics asynchronous code from external JS file

这似乎是一个类似的问题:Using Google Analytics asynchronous code from external JS file

It seems pushing the code into an external file removes the benefit of the new asynchronus code.

似乎将代码推送到外部文件中消除了新异步代码的好处。

回答by Vishal Verma

You shouldn't put GA code in function for two reasons:

出于两个原因,您不应该将 GA 代码放入函数中:

  1. The variables of GA become local. They need to be used globally.
  2. You're calling the function only when the whole of page (document, technically) is loaded. This makes the download process serial (defeating the parallelism of asyn analytics).
  1. GA 的变量成为局部变量。它们需要在全球范围内使用。
  2. 只有当整个页面(技术上的文档)被加载时,您才调用该函数。这使得下载过程是串行的(打败了异步分析的并行性)。

People criticizing the use of external JS for GA code, are bit wrong here. With introduction of asyncattribute in js embed, it makes sense. I keep this GA code in external js and embed it async right in the head of document. Two benefits:

人们批评在 GA 代码中使用外部 JS,这里有点不对。通过在 js embed 中引入async属性,这是有道理的。我将此 GA 代码保留在外部 js 中,并将其异步嵌入到文档的头部。两个好处:

  1. Keeps the code neat (no inline GA)
  2. Caches the js- saves bandwidth & loads faster
  1. 保持代码整洁(无内联 GA)
  2. 缓存 js- 节省带宽并加快加载速度

So the HTML would become:

所以 HTML 会变成:

<head>
    <script type="text/javascript" src="static/scripts.js" async></script>
</head>

and scripts.jswill have:

并且scripts.js将有:

var _gaq=_gaq||[];
_gaq.push(['_setAccount','UA-XXXXXXXX-1']);
_gaq.push(['_setDomainName','.domain.net']);
_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)
})();

回答by gblazex

Then again: don'tput your Google Async snippet in an external filebecause it will blockother contents from downloading. The main point of having an asynchronoustracking code is to make it parallel.

再说一遍:不要将您的 Google 异步代码段放在外部文件中,因为它会阻止下载其他内容。拥有异步跟踪代码的要点是使其并行

If you use the asynchronous GA just put it in the topof you document in an inline script tag. This is the recommendationon Google Analytics websiteas well:

如果您使用异步 GA,只需将它放在文档顶部内联脚本标记中。这也是Google Analytics 网站上的建议

Insert the asynchronous snippet at the bottom of the <head>sectionof your pages, after any other scripts your page or template might use.

在页面部分底部<head>插入异步代码段 ,在页面或模板可能使用的任何其他脚本之后。

回答by Jochem

I don't see why you can't put it above "function includeGA()" in the include.js. Actually, I don't see why you can't copy the whole google script literally into the include.js (without the $ workaround). This should be inlined by the script-include tag, right?

我不明白为什么你不能把它放在 include.js 中的“function includeGA()”之上。实际上,我不明白为什么您不能将整个 google 脚本逐字复制到 include.js 中(没有 $ 解决方法)。这应该由 script-include 标签内联,对吗?