Skip to content

Instantly share code, notes, and snippets.

@Plou
Created May 14, 2014 14:29
Show Gist options
  • Select an option

  • Save Plou/af5cd1ce8c3e9986353a to your computer and use it in GitHub Desktop.

Select an option

Save Plou/af5cd1ce8c3e9986353a to your computer and use it in GitHub Desktop.
Track ajaxLoaded pages with google analytics Or plug it to backbone navigate method
# Override the navigate method to plug Ganalytics to it
originalLoadUrl = Backbone.History.prototype.loadUrl;
# This cleans up fragment the same way that loadUrl does as of Backbone 0.5.3
Backbone.History.prototype.loadUrl = (fragmentOverride) ->
fragment = this.fragment = this.getFragment(fragmentOverride)
console.log fragment, window.ga
if window.ga != undefined
window.ga('send', 'pageview', {page: '/' + fragment})
return originalLoadUrl.call(this, fragmentOverride)
originalNavigate = Backbone.History.prototype.navigate
# This doesn't clean up fragment before sending to GA, so there may be normalization issues
Backbone.History.prototype.navigate = (fragment, triggerRoute) ->
if !triggerRoute && window.ga != undefined
window.ga('send', 'pageview', {page: '/' + fragment})
return originalNavigate.call(this, fragment, triggerRoute)
// use the right object to match the version of google analitycs you're using
var url = "/ajaxLoadedPage";
// push to google analytics with the ga object
ga('send', 'pageview', {page: '/' + fragment})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment