mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 04:52:45 +08:00
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:
parent
e7a05c54e8
commit
b617468098
|
@ -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')
|
||||
|
||||
});
|
|
@ -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']);
|
||||
});
|
||||
}
|
||||
});
|
30
app/assets/javascripts/discourse/lib/page_tracker.js
Normal file
30
app/assets/javascripts/discourse/lib/page_tracker.js
Normal 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);
|
Loading…
Reference in New Issue
Block a user