Friday, June 22, 2012

piwik stats for your website

After setting up piwik on your server, here is some simple code to get your website to send some usage data. In the .jsp load the piwik script:
<script type="text/javascript">
      var pkBaseURL = (("https:" == document.location.protocol) ?"https://[your-server-name]/" : "http://[your-server-name]/");
      document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>
Then, to tack something in you .javascirpt, the code will look something like this:
var siteId = 1;  // check piwik website settings to get siteId needed for the app
if (typeof pkBaseURL === 'string') {
   try{
 // setup any variables you want to track
 var customVariable1 = 'test';
 // can track up to 5 custom variables
          var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", siteId);
          piwikTracker.setCustomVariable (1, 'customVariable1', customVariable1, 'page');
          piwikTracker.setCustomVariable (2, 'customVariable1', customVariable1, 'page');
          piwikTracker.setCustomVariable (3, 'customVariable1', customVariable1, 'page');
          piwikTracker.trackPageView();
          piwikTracker.enableLinkTracking();
   } catch( err ) {}
}
That's it, pretty simple...

No comments:

Post a Comment