将新的 Google Analytics 代码写入外部文件 [英] New Google Analytics code into external file

查看:17
本文介绍了将新的 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天全站免登陆