Sunday, February 19, 2012

How to add Google Analytics to ajax page in drupal

We know that Google Analytics works with url hit .No of URL hits no of page hits occur,but what about ajax page suppose some part is inside a page and with click of that ajax area create hits for the page with out page refresh.For that solution is

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-800000-5']);
  _gaq.push(['_trackPageview']);

  (function() {alert('1');
    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>

For doing this in drupal you will have to add this script in HEAD section of ypour theme html.tpl.php  file and call in  onClick event like this <a href="" onClick="_gaq.push(['_trackPageview', '/pp']);">Click it</a>
it adds hits to page called as pp you can give name of any page you like its your custom url which you are giving.