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

查看:35
本文介绍了是否可以将 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"));

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

是否可以从外部 JS 文件中调用此脚本?我想要类似的东西:

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>

并将其中一个放在我的每个 HTML 页面上.
我上面的代码将在 googleanalytics.js
Google 的指示是将代码放在每个页面中.问题在于它使得更改跟踪代码变得更加困难.(我们的 DEV 和 PROD 页面使用不同的跟踪代码).
我已经试过了,但它似乎不起作用.
这样做有什么问题吗?还是有其他原因导致了问题?

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?

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

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.

只是想一想,Google Analytics(分析)的报告通常会滞后一天左右,因此当您进行更改时,您需要一些时间才能知道它是否有效.我喜欢做的是点击一个不经常获得流量的页面,以确保我的跟踪设置正确.

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.

此外,您可以尝试在 <script 标签中将链接设为绝对链接.它可能只是在错误的地方寻找分析代码.

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天全站免登陆