简体   繁体   中英

is _gaq.push not working properly?

I am trying to track my pages via google analytics, here is my code

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'XXXXXXXXXX']);
_gaq.push(['_setDomainName', 'somesite.com']);
_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';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

(function ($) {     
    // Log all jQuery AJAX requests to Google Analytics
    $(document).bind('ajaxComplete', function(event, xhr, settings){ 
        console.log('ajax Request');
        console.log(settings.url);
        _gaq.push(['_trackPageview', settings.url]);
    });

})(jQuery);

On each ajax request I can see the console has values

ajax Request
url of the page

it means _gap.push is working (as there is no js error on page). But when I am checking my req/res via Live HttpHeaders there is no req/res to google analytics, How to track it?

here is the screenshot in firebug 在此处输入图片说明

The point in _gaq.push is that, until Google Analytics is actually loaded, "_gaq" is just a normal array. That is: the lack of errors is indeed expected, regardless of whether or not it is "working" in the sense of triggering a request to Google.

The way Google Analytics works is not via ajax (or at least, there is no specific implementation detail regarding how the request will be sent). The method usually used is to create an Image element with the tracking data included in the query string of that image's URL. After all, the page doesn't care what response Google Analytics has, it just wants to send its data and go!

Rather than using LiveHttpHeaders, I would check the 'Network' panel of your developer tools -- if you have the Javascript console, you probably have access to that as well. You should be able to see all of the details of the requests on that panel.

You can also use the debug version of ga.js to diagnose errors. It prints things like "Invalid tracking code" and so on to the Javascript console.

Search for "Debugging with ga_debug.js" on this page:

https://developers.google.com/analytics/resources/articles/gaTrackingTroubleshooting

Look at the http headers sent - I use HttpFox - and filter for 'utm'. Look at the query string (httpfox breaks this out into a table for you) and you can see all of the utm parameters of the hit - account number (utmac), page (utmp), etc. If any of the utm params are unfamiliar, check this reference . This sort of simulation and analysis of the image requests sent to google's servers is very useful for debugging Google Analytics problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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