NoMethodError

raise "a blog about rails, javascript, and other stuff"

Track Backbone.js Page Views With Google Analytics

Google Analytics tracks page views based on evaluating the tracking code on page load. Backbone.js makes this difficult because the page never reloads. Fortunately, it’s super easy to add a function to the Backbone router to manually track each route.

Newer Analytics Code

If your tracking code looks like below, then you have the newer Analytics tracking code that uses the ga function instead of the _gaq array.

Add the following code to your router:

Old Analytics Code

If your tracking code looks like this:

Add the following code to your router:

Comments