简体   繁体   English

外部js文件中的Google Analytics(分析)未报告

[英]Google Analytics in external js file not reporting

According to Google Analytics our account is receiving data. 根据Google Analytics(分析),我们的帐户正在接收数据。 I uploaded to our Alpha site here on Monday and so far I can only see 1 visit on the 19th and nothing for the rest of the week, I had people from other offices and other states visit the site :( 我星期一在这里上传到了我们的Alpha网站 ,到目前为止,我只能在19日看到1次访问,在本周的其余时间里什么都没有看到,我有其他办公室和其他州的人访问了该网站:(

在此处输入图片说明


Curious if you may see something obviously wrong with the code below: Everything looks good, even the Search term tracking works (consoles out the term) 奇怪您是否会发现以下代码明显有问题:一切看起来不错,即使“搜索”字词跟踪也可以正常工作(使该字词无效)

//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


Our Alpha site here: http://neytiri.whoat.net/ 我们的Alpha网站位于: http : //neytiri.whoat.net/

The console.logs read out, and no errors (besides the vimeo one) console.logs读出来,没有错误(除了vimeo一个)

I needed to move the var _gaq = _gaq || []; 我需要移动var _gaq = _gaq || []; _gaq = _gaq || [];
and all the _gaq variables outside of the closure. 以及闭包之外的所有_gaq变量。 So above the //GOOGLE ANALYTICS 因此, //GOOGLE ANALYTICS
Works perfectly now :) 现在可以完美工作了:)

在此处输入图片说明

Brian's answer here is what fixed this. Brian的答案是解决此问题的方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM