Tracking Site Activity (Google Analytics) using Backbone

backbone.js, google-analytics

Solution

I prefer "do it yourself" style :) It's really simple:

var Router = Backbone.Router.extend({

    initialize: function()
    {
        //track every route change as a page view in google analytics
        this.bind('route', this.trackPageview);
    },

    trackPageview: function ()
    {
        var url = Backbone.history.getFragment();

        //prepend slash
        if (!/^\//.test(url) && url != "")
        {
            url = "/" + url;
        }

        _gaq.push(['_trackPageview', url]);
    }
}

And you add google analytics script to your page as usual.

Problem

I am looking the best way to track the `Site Activity` in `Google Analytics` for a web app made with `Backbone` and `Requires`. Looking At Google's Page, I found this drop-in plugin - Backbone.Analytics. My questions are: 1) using `Backbone.Analytics`, should I change backbone.analytics.js in order to add `_gaq.push(['_setAccount', 'UA-XXXXX-X']);`? 2) Are there other possible solutions/plugins?

Original source