mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06: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:
@@ -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);
|
||||||
Reference in New Issue
Block a user