简体   繁体   English

网页中多次包含Google Analytics(分析)代码

[英]Google analytics code is included multiple times in a page

I have a Enterprise Trading site. 我有一个企业交易网站。 I have included the Google analytic's code in every jsp of my site. 我在我网站的每个jsp中都包含了Google analytic's代码。 Now the problem is since in my site some pages are included within other pages not using frames just by ajax call and changing the div contents. 现在的问题是,在我的站点中,某些页面包含在其他页面中,而这些页面并不仅仅通过ajax调用和更改div内容就使用框架。 As a result the when a new page is loaded within the same page Google Analytic's wont be called since the entire page is not loaded, and the browser considers only page load to load the js, Not frame change. 结果,当在同一页面中加载新页面时,由于不会加载整个页面,因此不会调用Google Analytic,而浏览器仅考虑页面加载来加载js,而不更改框架。 Can any one suggest a solution for this 有人可以为此提出解决方案吗

The simpliest solution is use a global variable, like this: 最简单的解决方案是使用全局变量,如下所示:

if (! window.googleAnalytics) {
    window.googleAnalytics = true;
    // ga code here ...
}

Better alternative way is: 更好的替代方法是:

When I add Google Analytics on site, I wrote small js file, named google-analytics.js, here is contents: 在网站上添加Google Analytics(分析)时,我写了一个小js文件,名为google-analytics.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');
}

This script must be loaded after jQuery.js loaded. 必须在加载jQuery.js之后加载此脚本。 Insert, your GA-ID and site url in constructor's arguments. 在构造函数的参数中插入您的GA-ID和网站网址。 You can call window.googleAnalytics.track(), after AJAX page load and url changed. 更改AJAX页面加载和网址后,您可以调用window.googleAnalytics.track()。 Good Luck! 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您已在此页面上多次包含Google Maps API - You have included the Google Maps API multiple times on this page “您在此页面上多次包含 Google Maps API”错误 - "You have included the Google Maps API multiple times on this page" Error 试图多次加载角度+在此页面上多次包含Google Maps API - Tried to load angular more than once + included the Google Maps API multiple times on this page 您已在此页面上多次包含Google Maps API错误尽管我没有包含 - You have included the Google Maps API multiple times on this page Error Although i have'nt include 您已在此页面上多次包含 Google 地图 JavaScript API。 这可能会导致意外错误 - You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors 您在此页面上多次包含 Google Maps API。 这可能会导致意外错误(wordpress) - you have included the Google Maps API multiple times on this page. This may cause unexpected errors (wordpress) 同一页面上的多个Google Analytics(分析) - Multiple Google analytics on same page 您如何管理同一页面中多次包含的相同代码的 CSS 选择器? - How can you manage CSS selectors for identical code included multiple times in the same page? 同一页面上有两个不同的 Google API,但您在此页面上多次包含 Google Maps API 错误 - Two different Google APIs on same page, but get You have included the Google Maps API multiple times on this page Error Ruby使用地图进行部分更新:您已在此页面上多次包含Google Maps API。 这可能会导致意外错误 - Ruby updating partial with a map: You have included the Google Maps API multiple times on this page. This may cause unexpected errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM