Created
May 14, 2014 14:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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