Javascript 谷歌分析代码在一个页面中包含多次
我有一个企业交易网站。我在我网站的每个jsp中都包含了Javascript 谷歌分析代码在一个页面中包含多次,javascript,jsp,Javascript,Jsp,我有一个企业交易网站。我在我网站的每个jsp中都包含了GoogleAnalytic的代码。现在的问题是,在我的站点中,一些页面被包含在其他页面中,而不是通过ajax调用和更改div内容来使用框架。因此,当在同一页面中加载新页面时,不会调用Google Analytic,因为整个页面都没有加载,浏览器只考虑页面加载来加载js,而不考虑帧更改。任何人都可以为此提出解决方案吗?最简单的解决方案是使用全局变量,如下所示: if (! window.googleAnalytics) { windo
GoogleAnalytic的代码。现在的问题是,在我的站点中,一些页面被包含在其他页面中,而不是通过ajax调用和更改div内容来使用框架。因此,当在同一页面中加载新页面时,不会调用Google Analytic,因为整个页面都没有加载,浏览器只考虑页面加载来加载js,而不考虑帧更改。任何人都可以为此提出解决方案吗?最简单的解决方案是使用全局变量,如下所示:
if (! window.googleAnalytics) {
window.googleAnalytics = true;
// ga code here ...
}
更好的替代方法是:
当我在网站上添加Google Analytics时,我编写了一个名为Google-Analytics.js的小js文件,内容如下:
function GoogleAnalytics(accountId, productionServerUrl) {
var _gaq = window._gaq = window._gaq || [];
if (window.googleAnalytics) {
throw new Error("GoogleAnalytics tools already created!");
}
window.googleAnalytics = this;
this.accountId = accountId;
this.dummy = (window.location.hostname != productionServerUrl);
// Work's only on production server and ignore sandboxes
if (this.dummy) {
return ;
}
_gaq.push(['_setAccount', this.accountId]);
_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';
ga.onload = function() {
//console.log("GA Loaded !!!!", this, arguments);
window.googleAnalytics.getTracker();
};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
}
GoogleAnalytics.prototype.getTracker = function () {
if (this.dummy) {
return null;
}
if (undefined == this.pageTracker) {
if (window._gat == undefined) {
console.error("! Google Analytics script is not loaded !");
return null;
}
this.pageTracker = window._gat._getTracker(this.accountId);
}
return this.pageTracker;
}
GoogleAnalytics.prototype.track = function () {
if (this.dummy) {
console.log(" >> GoogleAnalytics.track()", window.location.href, this, arguments);
}
if (this.dummy) {
return ;
}
var tracker = this.getTracker();
if (null == tracker) {
return;
}
tracker._trackPageview();
}
if (undefined == window.googleAnalytics) {
window.googleAnalytics = new GoogleAnalytics('UA-XXXXXXXX-X', 'yoursite.ru');
}
必须在加载jQuery.js之后加载此脚本。
在构造函数的参数中插入GA-ID和站点url。
在AJAX页面加载和url更改后,可以调用window.googleAnalytics.track()。
祝你好运 最简单的解决方案是使用全局变量,如下所示:
if (! window.googleAnalytics) {
window.googleAnalytics = true;
// ga code here ...
}
更好的替代方法是:
当我在网站上添加Google Analytics时,我编写了一个名为Google-Analytics.js的小js文件,内容如下:
function GoogleAnalytics(accountId, productionServerUrl) {
var _gaq = window._gaq = window._gaq || [];
if (window.googleAnalytics) {
throw new Error("GoogleAnalytics tools already created!");
}
window.googleAnalytics = this;
this.accountId = accountId;
this.dummy = (window.location.hostname != productionServerUrl);
// Work's only on production server and ignore sandboxes
if (this.dummy) {
return ;
}
_gaq.push(['_setAccount', this.accountId]);
_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';
ga.onload = function() {
//console.log("GA Loaded !!!!", this, arguments);
window.googleAnalytics.getTracker();
};
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
}
GoogleAnalytics.prototype.getTracker = function () {
if (this.dummy) {
return null;
}
if (undefined == this.pageTracker) {
if (window._gat == undefined) {
console.error("! Google Analytics script is not loaded !");
return null;
}
this.pageTracker = window._gat._getTracker(this.accountId);
}
return this.pageTracker;
}
GoogleAnalytics.prototype.track = function () {
if (this.dummy) {
console.log(" >> GoogleAnalytics.track()", window.location.href, this, arguments);
}
if (this.dummy) {
return ;
}
var tracker = this.getTracker();
if (null == tracker) {
return;
}
tracker._trackPageview();
}
if (undefined == window.googleAnalytics) {
window.googleAnalytics = new GoogleAnalytics('UA-XXXXXXXX-X', 'yoursite.ru');
}
必须在加载jQuery.js之后加载此脚本。
在构造函数的参数中插入GA-ID和站点url。
在AJAX页面加载和url更改后,可以调用window.googleAnalytics.track()。
祝你好运