外部js文件中的Google Analytics未报告

时间:2013-08-22 15:59:21

标签: javascript jquery google-analytics google-api

根据Google Analytics,我们的帐户正在接收数据。我星期一上传到我们的Alpha site here,到目前为止我只能在19日看到1次访问,本周余下时间没有任何内容,我让其他办公室和其他州的人访问该网站:(

enter image description here


如果您可能会看到下面的代码明显错误,那就很好奇: 一切看起来都不错,甚至搜索术语跟踪工作(调出术语)

//GOOGLE ANALYTICS
//======================================================================
WHOAT.analytics = (function ($, w, undefined) {
'use strict';

//initial google setup
var _gaq = _gaq || [];
_gaq.lang = 'en';
_gaq.push(['_setAccount', 'UA-xxxxx-x']);
_gaq.push(['_setDomainName', 'whoat.net']);
_gaq.push(['_setAllowLinker', false]);
_gaq.push(['_gat._anonymizeIp']);
_gaq.push(['_trackPageview']);

// console.log('inside Analytics');
console.log(_gaq);

function init() {
    (function () {
        var ga = document.createElement('script'); ga.type = 'text/javascript';
        var s = document.getElementsByTagName('script')[0];
        ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        s.parentNode.insertBefore(ga, s);

        console.log(ga);
    }());
}

//TRACK PAGE VIEWS
var trackPageView = function (url) {
    //get the url from the pathname if it wasn't passed in
    if (url === null || url === undefined) {
        url = w.location.pathname;
    }
    //if an id is part of the url, replace the id with 'details'
    url = url.replace(new RegExp(/\/[0-9]+$/), '/details');
    _gaq.push(['_trackPageview', url]);

    console.log('analytics trackPageView: '+url);
};

//TRACK SEARCH EVENT
var trackSearchEvent = function (searchTerm) {
    var url = w.location.pathname;
    url = url.replace(new RegExp(/\/[0-9]+$/), '/details');
    _gaq.push(['_trackEvent', 'search', searchTerm, 'url for search is ' + url]);

    console.log('analytics trackSearchEvent: '+searchTerm);
};

//TRACK VIDEO EVENT
var trackVideoEvent = function (event, videoID, time) {
    var label;
    if (time !== null && time !== undefined) {
        label = 'video id is ' + videoID + " and time is " + time;
    } else {
        label = 'video id is ' + videoID;
    }

    _gaq.push(['_trackEvent', 'video', event, label]);
};

//track the page just landed on
trackPageView();

return {
    trackPageView: trackPageView,
    trackSearchEvent : trackSearchEvent,
    trackVideoEvent : trackVideoEvent,
    init : init
};
}(jQuery, window));

$(document).ready(function () {
    WHOAT.analytics.init();
}); // document ready


我们的Alpha网站:http://neytiri.whoat.net/

console.logs读出,没有错误(除了vimeo之外)

1 个答案:

答案 0 :(得分:1)

我需要移动var _gaq = _gaq || [];
以及闭包之外的所有_gaq变量。因此,//GOOGLE ANALYTICS上方完美地工作:)

enter image description here

Brian's answer here解决了这个问题。