Wednesday 11 September 2013

Adding google analytics to jsp application

First register your application in google analytics.
Your application will be given a unique app id.
Then add the following code in your jsp.

<!-- Google Analytics module-->
<script type="text/javascript">

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

Note: It will take one or two days to track the number of users in your application.
The moment you deploy the code it will start tracking but it will take a day time to get updated in the Google Analytics site.