views:

31

answers:

2

I am using Google analytic on my web site.I have the following script in the head section of my page -

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-17301453-1']);
  _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);
  })();

</script>

i am using the following method to track the number of clicks -

<a href="http://buzzr.in" onClick="_gaq.push(['_trackPageview', '/testbrand1/testcamp2/http://buzzr.in']);"&gt;Grab This Deal</a>

But the google anayltics website does not seem to be reporting any events/clicks. Please help.

A: 

does it work if you do this?

<a href="#" onClick="_gaq.push(['_trackPageview', '/testbrand1/testcamp2/http://buzzr.in']);"&gt;Grab This Deal</a>

If so, you may need to put something in to wait for the google tracking code to complete before allowing the browser to navigate away.

Mainguy
Please don't use this approach. I can't be the only one who block Google Analytics...
Gert G
+1  A: 

When debugging you may like to either use the Firefox Live HTTP Headers plug-in or Wireshark. These will tell you about requests to Google Analytics.

Depending on the browser and the way the site loads it may be worth using the onload handler of the page to call the Google Anayltics code (with async set to false in the GA snippet) and dynamically assign the onclick event (an id on the tag will help find it). This will ensure the code can run at the appropriate time.

The link may be processed before the tracking event is sent to Google Analytics as you do not return false to the onclick event. AFAIK there is no callback on GA events to let you know when to proceed with the next page.

In short this looks like a race condition between an async GA request and the browser finding the linked site.

The only way to have this functionality would be to use an iframe or new window for the link's target.

Metalshark