将新的Google Analytics代码添加到外部文件中 [英] New Google Analytics code into external file

查看:78
本文介绍了将新的Google Analytics代码添加到外部文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的Google Analytics代码如下所示:

New Google Analytics code looks like one below:

<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>

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

我特别想问var _gaq = _gaq || []; [...]部分,因为我知道可以将其余的例如

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

index.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

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

我已经尝试将var _gaq = _gaq || []; [.. 。]代码到不同的位置,但没有任何工作。

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

推荐答案

这似乎是一个类似的问题:使用来自外部JS文件的Google Analytics异步代码

This appears to be a similar question to this one: 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.

这篇关于将新的Google Analytics代码添加到外部文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆