是否可以将Google Analytics代码放入外部JS文件中? [英] Is it possible to put Google Analytics code in an external JS file?

查看:113
本文介绍了是否可以将Google Analytics代码放入外部JS文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var gaJsHost =((https:== document.location.protocol)?https:// ssl。:http:// www。); 
document.write(unescape(%3Cscript src ='+ gaJsHost +google-analytics.com/ga.js'type ='text / javascript'%3E%3C / script%3E));

尝试
{
var pageTracker = _gat._getTracker(UA-XXXXXXX-1);
pageTracker._trackPageview();

catch(err){}

是否可以打电话这个脚本来自外部的JS文件?我想要的是:

 < script type =text / javascriptsrc =googleanalytics.js> < /脚本> 

并将其中的一个放在我的每个HTML页面中。

代码我已经在googleanalytics.js

里面了,Google的说明是将代码放在每个页面中。这样做的问题是,它更难以更改跟踪代码。 (我们在DEV和PROD页面中使用了不同的跟踪代码)。

我已经尝试过了,它似乎没有工作。

是否有什么问题那?或者是否有其他问题导致问题?

重要提示
请注意,我们使用的是IE6和8浏览器(是的,我知道,不需要告诉我)

解决方案

是的,这是可能的。如果它不工作,那么还有其他事情正在发生。

只是一个想法,谷歌分析通常比报告晚一天,因此当您进行更改时,需要一段时间才能知道它正在工作。我喜欢做的是打到一个不经常获得流量的页面,以确保我的跟踪设置正确。



另外,您可以尝试制作链接您的< script 标记中的绝对链接。它可能只是在分析代码的错误位置。


var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try 
{
    var pageTracker = _gat._getTracker("UA-XXXXXXX-1");
    pageTracker._trackPageview();
}
catch(err) {}

Would it be possible to call this script from an external JS file? I wanted to to something like:

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

and put one of these on each of my HTML pages.
The code I have above will be inside googleanalytics.js
Google's instructions was to put the code in each page. The problem with that is it makes it harder to change the tracking code. (We use different tracking codes for our DEV and PROD pages).
I've tried it out and it doesn't seem to be working.
Is there something wrong with doing that? Or is there something else causing the problem?

Important FYI Please note that we are using IE6 and 8 browsers (yes, I know, no need to tell me)

解决方案

Yes this is possible. If it is not working then there is something else going on.

Just a thought, Google Analytics is usually about a day behind in reporting so when you make changes it will take some time before you know it is working. What I like to do is hit a page that does not get traffic very often to assure me that I have my tracking set up correctly.

Also, you might try making the link an absolute link in your <script tag. It might just be looking in the wrong place for the analytics code.

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

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