javascript GA-跟踪helper.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript GA-跟踪helper.js相关的知识,希望对你有一定的参考价值。

define(['require', 'exports'], function(require, exports) {

    exports.GATrackingHelper = function(settings) {

        // MEMBERS & DEFAULTS
        var urchinId      = settings.gaqId      || false,
            devId         = settings.devId      || 'UA-3172639-5',
            page          = settings.page       || null,
            trackingLabel = settings.label      || false,
            domainName    = settings.domainName || '.defencejobs.gov.au',
            _dev_log      = settings.devLog     || false,
            loadInterval  = 100,
            ready         = false;
        
        if (!urchinId || !isNaN(urchinId)) {
            alert('Invalid Google Analytics ID given, please ensure its a valid ID like the following example: UA-XXXXXXX-X');
            return;
        }

        // GOOGLE ANALYTICS HELPER ENGINE
        var GATrackingHelperEngine = {
            _load : function() {
                var gaHost = ('https:' == document.location.protocol) ? 'https://ssl.' : 'http://www.';
                var s = document.createElement('script');
                s.src = gaHost + 'google-analytics.com/ga.js';
                document.getElementsByTagName('head')[0].appendChild(s);
                var checker = this._wrap(this, this._check);
                setTimeout(checker, loadInterval);
            },

            _check : function() {      
                if (window['_gat']) {
                    gaTracker = _gat._getTracker(urchinId);

                    if (urchinId != devId) {
                        gaTracker._setDomainName(domainName);
                    }

                    gaTracker._initData();
                    
                    ready       = true;
                    pageTracker = gaTracker;
                } else {
                    var checker = this._wrap(this, this._check);
                    setTimeout(checker, loadInterval);
                }
            },
            
            trackPageview : function(page) {
                if (ready) {
                    (page == null) ?  gaTracker._trackPageview() : gaTracker._trackPageview(page);
                    this.log('manual trackPageview: ' + page);
                } else {
                    var tpv = this._wrap(this, this.trackPageview);
                    setTimeout(function() { tpv(page); }, loadInterval);
                }
            },
            
            trackEvent : function(category, action, label) {
                if (ready) {
                    gaTracker._trackEvent(category, action, (trackingLabel !== false ? trackingLabel : label));
                    this.log('trackEvent: ' + category + ', ' + action + ', ' + label);
                } else {
                    var te = this._wrap(this, this.trackEvent);
                    setTimeout(function() { te(category, action, label); }, loadInterval);
                }
            },
            
            log : function(msg) {
                if (window.console && window.console.log && _dev_log) {
                    console.log(msg);
                }
            },

            _wrap : function(obj, method) {
                return function() {
                    return method.apply(obj, arguments);
                };
            }
        };

        GATrackingHelperEngine._load();
        return GATrackingHelperEngine;
    }
});

以上是关于javascript GA-跟踪helper.js的主要内容,如果未能解决你的问题,请参考以下文章

javascript 测试GA表单/事件跟踪

javascript 使用Google Analytics和BU GA跟踪器跟踪下载情况

javascript 用于返回Google Analytics的自定义事件跟踪器的功能,如果未针对本地/演示环境设置GA,则将其包装

javascript VoyaRU.Analytics.Helper.js

Javascript - GA 和 Adwords

此GA电子商务跟踪代码是否有效?