Using Piwik for a Single Page Application

Creating a one-page / thick client application, and I wonder what is best used to enable and track using http://piwik.org/ p>

I would like to use Piwik in such a way that it is architecturally robust and replaced with another library in the future.

There seem to be two main tracking options with Piwik:

  • Fill the global _paq array with commands, then load the script (I don’t understand how to write future "pages" or change variables, though)
  • Get and use var myTracker = Piwik.getTracker()

_paq approach:

 myApp.loadAnalytics = function() { /* dynamically insert piwik.PHP  */ } myApp.track = function(pageName) { window._paq = window._paq || []; _paq.push(['setDocumentTitle', pageName]); _paq.push(["trackPageView"]); } myApp.loadAnalytics() // Then, anywhere in the application, and as many times as I want (I hope :) myApp.track('reports/eastWing') // Track a "page" change, lightbox event, or anything else 

.getTracker() :

 myApp.loadAnalytics = function() { /* dynamically insert piwik.PHP  */ } myApp.track = function(pageName) { myApp.tracker = myApp.tracker || Piwik.getTracker('https://mysite.com', 1); myApp.tracker.trackPageView(pageName); } myApp.loadAnalytics() // Then, anywhere in the application, and as many times as I want (I hope :) myApp.track('reports/eastWing') // Track a "page" change, lightbox event, or anything else 

Are these approaches functionally identical? Is it preferable for another for a single page application?

+7
source share
2 answers

The complete solution using .getTracker looks like this: https://gist.github.com/SimplGy/5349360

Not sure if it's better to use the _paq array.

+1
source

In order for the tracking library used (for example, piwik) to be completely independent of your application, you need to write a small class that will proxy functions to the Piwik tracker. Later, if you switch from Piwik to XYZ, you can simply update this proxy class, rather than updating multiple files that do some tracking.

Asynchronous code is required for your application (for example, calling any "track *" method will send a request)

+1
source

All Articles