I'm trying to add the asynchronous version of the Google Analytics tracking code to a website.
我正在尝试将异步版Google Analytics跟踪代码添加到网站中。
I'd like to keep the JavaScript in a separate file, and call it from there.
我想将JavaScript保存在一个单独的文件中,并从那里调用它。
Here's what I've currently got in my .js file:
这是我目前在.js文件中得到的内容:
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.
编辑:好的......在下面的一些反馈之后,我将添加我的.js文件的新当前内容。我将保持更新,以便如果/当这个问题得到解决,它将有希望帮助其他人尝试做类似的事情。
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();
3 个解决方案
#1
23
Your variable definition var _gaq
is inside a function. That means it's locally scoped inside that function and won't exist globally. Google Analytics depends on the global variable _gaq
. If you want to keep it inside a function like that, reference it as window._gaq
.
变量定义var _gaq在函数内部。这意味着它本地作用于该函数内部,并且不会全局存在。 Google Analytics取决于全局变量_gaq。如果你想将它保存在这样的函数中,请将其引用为window._gaq。
#2
14
You completely miss the point of the asynchronous tracking code. Don't put it in an external file because that's exactly like including the old synchronous GA.
你完全忽略了异步跟踪代码的重点。不要把它放在外部文件中,因为这与包含旧的同步GA完全一样。
And most importantly don't defer the tracking code to window.onload
as it may fire too late.
最重要的是,不要将跟踪代码推迟到window.onload,因为它可能会触发太晚。
If you use the asynchronous GA just put it in the top of you document in an inline script tag. This is the recommendation on Google Analytics website as well.
如果您使用异步GA,只需将其放在内联脚本标记中的文档顶部。这也是Google Analytics网站上的推荐。
Insert the asynchronous snippet at the bottom of the
<head>
section of your pages, after any other scripts your page or template might use.在页面或模板可能使用的任何其他脚本之后,将异步代码段插入页面部分的底部。
#3
0
Honestly I have not read through all these posts since they are rather old. However I recently had the need to add Gtag (google tag manager for analytics tracking) to an old website that was a 1000 static HTML files and (LUCKILY) a the html files had a single include js file for the spry menu bar, like i said very old site! For my purposes I wasn't worried about performance but measuring the traffic so we can migrate it. your case may be different but the below code will work for external js includes of Gtag.
老实说,我没有读过所有这些帖子,因为它们已经很老了。但是我最近需要将Gtag(谷歌标签管理器用于分析跟踪)添加到一个旧网站,这是一个1000静态HTML文件和(LUCKILY)一个html文件有一个包含js文件的spry菜单栏,就像我说很旧的网站!出于我的目的,我并不担心性能,而是测量流量,以便我们可以迁移它。您的情况可能会有所不同,但以下代码适用于包含Gtag的外部js。
I used this file to load the code below since the above code is for legacy ga.js
我使用此文件加载下面的代码,因为上面的代码是针对传统的ga.js
//Added Google Anyltics Tag Container Tracking - included here to min rebuilding DOM
function loadGoogleAnalytics(){
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
}
loadGoogleAnalytics(); //Create the script
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
//Confirmed with Google tag Assistant
#1
23
Your variable definition var _gaq
is inside a function. That means it's locally scoped inside that function and won't exist globally. Google Analytics depends on the global variable _gaq
. If you want to keep it inside a function like that, reference it as window._gaq
.
变量定义var _gaq在函数内部。这意味着它本地作用于该函数内部,并且不会全局存在。 Google Analytics取决于全局变量_gaq。如果你想将它保存在这样的函数中,请将其引用为window._gaq。
#2
14
You completely miss the point of the asynchronous tracking code. Don't put it in an external file because that's exactly like including the old synchronous GA.
你完全忽略了异步跟踪代码的重点。不要把它放在外部文件中,因为这与包含旧的同步GA完全一样。
And most importantly don't defer the tracking code to window.onload
as it may fire too late.
最重要的是,不要将跟踪代码推迟到window.onload,因为它可能会触发太晚。
If you use the asynchronous GA just put it in the top of you document in an inline script tag. This is the recommendation on Google Analytics website as well.
如果您使用异步GA,只需将其放在内联脚本标记中的文档顶部。这也是Google Analytics网站上的推荐。
Insert the asynchronous snippet at the bottom of the
<head>
section of your pages, after any other scripts your page or template might use.在页面或模板可能使用的任何其他脚本之后,将异步代码段插入页面部分的底部。
#3
0
Honestly I have not read through all these posts since they are rather old. However I recently had the need to add Gtag (google tag manager for analytics tracking) to an old website that was a 1000 static HTML files and (LUCKILY) a the html files had a single include js file for the spry menu bar, like i said very old site! For my purposes I wasn't worried about performance but measuring the traffic so we can migrate it. your case may be different but the below code will work for external js includes of Gtag.
老实说,我没有读过所有这些帖子,因为它们已经很老了。但是我最近需要将Gtag(谷歌标签管理器用于分析跟踪)添加到一个旧网站,这是一个1000静态HTML文件和(LUCKILY)一个html文件有一个包含js文件的spry菜单栏,就像我说很旧的网站!出于我的目的,我并不担心性能,而是测量流量,以便我们可以迁移它。您的情况可能会有所不同,但以下代码适用于包含Gtag的外部js。
I used this file to load the code below since the above code is for legacy ga.js
我使用此文件加载下面的代码,因为上面的代码是针对传统的ga.js
//Added Google Anyltics Tag Container Tracking - included here to min rebuilding DOM
function loadGoogleAnalytics(){
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = 'https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
}
loadGoogleAnalytics(); //Create the script
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXXXXX-1');
//Confirmed with Google tag Assistant