从外部JS文件使用谷歌Analytics(分析)异步code [英] Using Google Analytics asynchronous code from external JS file
问题描述
我想加入谷歌Analytics(分析)的异步版本跟踪code到网站。
我想保持的JavaScript在一个单独的文件,并从那里调用它。
下面就是我目前在我的.js文件了:
函数addLoadEvent(FUNC){
VAR oldonload =的window.onload;
如果(typeof运算的window.onload!='功能'){
在window.onload = FUNC;
}其他{
在window.onload =函数(){
oldonload();
FUNC();
}
}
}功能loadtracking(){
VAR _gaq = _gaq || [];
_gaq.push(['_ setAccount','UA-XXXXXXX-X']);
_gaq.push(['_ trackPageview的']); (函数(){
VAR GA =使用document.createElement('脚本'); ga.type =文/ JavaScript的'; ga.async = TRUE;
ga.src =(的https:== document.location.protocol的https:// ssl的':'HTTP:// WWW'?)+'。谷歌-analytics.com / ga.js的';
变种S = document.getElementsByTagName('脚本')[0]; s.parentNode.insertBefore(GA,S);
})();
}addLoadEvent(loadtracking);
和这里就是我在已经得到了< HEAD>
我的母版页标记:
<脚本类型=文/ JavaScript的SRC =JS /谷歌的analytics.js>< / SCRIPT>
不过,显然是一个问题,因为几天后,我没有通过!让统计
任何想法我需要改变吗?
谢谢,
尼尔
编辑:确定...以下一些反馈后,我要加我的.js文件的新的的当前内容。我会保持更新,因此,如果/当这得到解决,希望能帮助其他人试图做类似的事情。
VAR _gaq = _gaq || [];功能loadtracking(){
window._gaq.push(['_ setAccount','UA-XXXXXXX-X']);
window._gaq.push(['_ trackPageview的']); (函数(){
VAR GA =使用document.createElement('脚本'); ga.type =文/ JavaScript的'; ga.async = TRUE;
ga.src =(的https:== document.location.protocol的https:// ssl的':'HTTP:// WWW'?)+'。谷歌-analytics.com / ga.js的';
变种S = document.getElementsByTagName('脚本')[0]; s.parentNode.insertBefore(GA,S);
})();
}loadtracking();
您VAR _gaq是一个函数内。这意味着它的局部范围该函数内部,并不会整体存在。 GA依赖于全局变量_gaq。如果你想保持这样的功能在里面,引用它作为window._gaq。
I'm trying to add the asynchronous version of the Google Analytics tracking code to a website.
I'd like to keep the JavaScript in a separate file, and call it from there.
Here's what I've currently got in my .js file:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
oldonload();
func();
}
}
}
function loadtracking() {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
_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);
})();
}
addLoadEvent(loadtracking);
And here's what I've got in the <head>
tag of my Master page:
<script type="text/javascript" src="js/google-analytics.js" ></script>
However, there's obviously a problem as after a few days, I'm not getting stats through!
Any ideas what I need to change?
Thanks, Neil
EDIT: Ok... After some feedback below, I'm going to add the new current contents of my .js file. I'll keep it updated so that if/when this gets solved, it will hopefully help other people trying to do similar things.
var _gaq = _gaq || [];
function loadtracking() {
window._gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
window._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);
})();
}
loadtracking();
Your "var _gaq" is inside a function. That means it's locally scoped inside that function and won't exist globally. GA depends on the global variable _gaq. If you want to keep it inside a function like that, reference it as window._gaq.
这篇关于从外部JS文件使用谷歌Analytics(分析)异步code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!