1

I was looking for a way to track a AJAX requests using Google Analytics. I found a lot of articles about it. However, All of them were providing similar solution like this SO Question. But this is not working for me and after a month nothing has been tracked with Google Analytics.

What is more wondering to me is that this code simply do nothing. No Ajax or any other request will send to Google. So how it could work ? I really expect that do send request or reload an iframe at least. But it seems that really is not doing anything.

First I'm doing like this:

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_setDomainName', 'domain.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);
})();

And then in other place:

$.ajax({
    'url' : 'ajax.php',
    success: function(){
        _gaq.push(["_trackPageview", "/search/"+keyword+"/"]);
    }
});

I am doing something wrong ?

Community
  • 1
  • 1
Omid
  • 4,575
  • 9
  • 43
  • 74
  • Although it doesn't actually answer your question: I use gaug.es (http://get.gaug.es/), it's pretty easy for things like this. It's like the apple of the trackers – macool Feb 24 '13 at 16:29
  • 1
    are you sure your ajax call is getting called after GA has loaded? Your GA code loads asynchronously so your ajax call could fire before GA has fully loaded... – royse41 Feb 24 '13 at 16:31
  • @seanxe How does it make scene ? Ajax request will be after loading GA for sure. – Omid Feb 24 '13 at 16:36
  • @omid not necessarily - i was just asking to make sure you didn't have your ajax request appear earlier than GA in your markup. are you sure your success method is being run? – royse41 Feb 24 '13 at 16:41
  • @seanxe Yes i put an alert and debugged it. You really wondering how this script work without sending any request. – Omid Feb 24 '13 at 16:47
  • 2
    Is "keyword" actually defined ? I thought you'd have to pass the ajax return value as a parameter to the success function. – Eike Pierstorff Feb 24 '13 at 20:35
  • `_gaq.push(['_setDomainName', 'domain.com']);` are you sure the domain you list here matches up with the actual domain the code is on? Try commenting out that line and see if GA makes its call – CrayonViolent Feb 24 '13 at 21:31
  • 1
    Are you running your site on localhost for testing? [Google Analytics GIF request not sent](http://stackoverflow.com/questions/9738815/google-analytics-gif-request-not-sent/9741228#9741228) might help... – mike Feb 25 '13 at 03:11
  • If the AJAX request doesn't go through you should be worried in fixing the AJAX before plugin Google Analytics events into it. – Eduardo Feb 25 '13 at 16:06
  • @EikePierstorff, Crayon, mike, Eduardo: Script is running online, Ajax success will triggered and I'm tracking ajax request in ajax search. So when user tries to search all scripts and Google Analytics has been load completely for sure. – Omid Feb 25 '13 at 20:23

1 Answers1

0

This guideline helped me to solve the problem.
https://support.google.com/analytics/answer/1012264

Omid
  • 4,575
  • 9
  • 43
  • 74