admin管理员组

文章数量:1425195

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']);
_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/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

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.']);
_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./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

Share Improve this question edited Sep 13, 2012 at 6:05 coure2011 asked Sep 13, 2012 at 5:53 coure2011coure2011 42.6k87 gold badges225 silver badges361 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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

本文标签: javascriptis gaqpush not working properlyStack Overflow