Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking Site Activity (Google Analytics) using Backbone

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?

like image 725
js999 Avatar asked Jun 14 '12 09:06

js999


2 Answers

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.

like image 191
algiecas Avatar answered Sep 28 '22 01:09

algiecas


You shouldn't have to change anything. Just add your Google Analytics code snippet, like normal, and include Backbone.Analytics as you would any other Javascript library.

like image 36
KendallB Avatar answered Sep 28 '22 02:09

KendallB