FIX: currentPath was not changing when transitioning to the same path.

Added a new hook to allow other kinds of analytics.
This commit is contained in:
Robin Ward 2014-01-13 15:30:19 -05:00
parent e7a05c54e8
commit b617468098
3 changed files with 45 additions and 25 deletions

View File

@ -1,25 +0,0 @@
/*global _gaq:true */
/**
The base controller for all things Discourse
@class ApplicationController
@extends Discourse.Controller
@namespace Discourse
@module Discourse
**/
Discourse.ApplicationController = Discourse.Controller.extend({
routeChanged: function(){
if (window._gaq === undefined) { return; }
if(this.afterFirstHit) {
Em.run.schedule('afterRender', function() {
_gaq.push(['_trackPageview']);
});
} else {
this.afterFirstHit = true;
}
}.observes('currentPath')
});

View File

@ -0,0 +1,15 @@
/**
Sets up the PageTracking hook.
**/
Discourse.addInitializer(function() {
var pageTracker = Discourse.PageTracker.current();
pageTracker.start();
// Out of the box, Discourse tries to track google analytics
// if it is present
if (typeof window._gaq !== 'undefined') {
pageTracker.on('change', function() {
window._gaq.push(['_trackPageview']);
});
}
});

View File

@ -0,0 +1,30 @@
/**
Called whenever the "page" changes. This allows us to set up analytics
and other tracking.
To get notified when the page changes, you can install a hook like so:
```javascript
Discourse.PageTracker.current().on('change', function(url) {
console.log('the page changed to: ' + url);
});
```
@class PageTracker
@namespace Discourse
@module Discourse
**/
Discourse.PageTracker = Ember.Object.extend(Ember.Evented, {
start: function() {
if (this.get('started')) { return; }
var router = Discourse.__container__.lookup('router:main'),
self = this;
router.on('didTransition', function() {
self.trigger('change', this.get('url'));
});
this.set('started', true);
}
});
Discourse.PageTracker.reopenClass(Discourse.Singleton);