A drop-in plugin that integrates Google's
trackEvent directly into Backbone's
navigate function. Works best with
pushState set to
pushState is turned off, it's possible Google will register visits twice on page load. You can mitigate that by removing the
trackEvent from the Google code in your site.
- Backbone.js (Tested in 1.0)
Add the asynchronous Google Analytics code to your site.
Add these dependencies to your site's
<head>, in order:
<script src="underscore.js"></script> <script src="backbone.js"></script> <script src="backbone.analytics.js"></script>
npm install backbone.analytics --save
Anywhere you call your router's navigate method with the trigger option set to true Backbone.Analytics will call
_gaq.push(['_trackPageview', '/some-page']) after completing the Backbone route. This pushes the route to the Google Analytics tracking queue. Once this queue is processed by the Google Analytics script your urls will be tracked to the Google Analytics server.
var TestRouter = BackboneRouter;var router = ;Backbonehistorystart;
Somewhere else in your application, change the view by doing:
Anywhere in your application where you want to update the URL but do not trigger the associated route, you will still need to manually track the action.