Depcreate `PageTracker` in favor of `PluginAPI`

This commit is contained in:
Robin Ward 2016-02-19 16:30:59 -05:00
parent 9b0b213ba2
commit e848ea5a99
4 changed files with 45 additions and 39 deletions

View File

@ -1,5 +1,5 @@
import { cleanDOM } from 'discourse/routes/discourse';
import PageTracker from 'discourse/lib/page-tracker';
import { startPageTracking, onPageChange } from 'discourse/lib/page-tracker';
export default {
name: "page-tracking",
@ -34,13 +34,12 @@ export default {
}
};
const pageTracker = PageTracker.current();
pageTracker.start();
startPageTracking(router);
// Out of the box, Discourse tries to track google analytics
// if it is present
if (typeof window._gaq !== 'undefined') {
pageTracker.on('change', function(url, title) {
onPageChange((url, title) => {
window._gaq.push(["_set", "title", title]);
window._gaq.push(['_trackPageview', url]);
});
@ -49,7 +48,7 @@ export default {
// Also use Universal Analytics if it is present
if (typeof window.ga !== 'undefined') {
pageTracker.on('change', function(url, title) {
onPageChange.on('change', (url, title) => {
window.ga('send', 'pageview', {page: url, title: title});
});
}

View File

@ -1,6 +1,6 @@
import DiscourseURL from 'discourse/lib/url';
import PageTracker from 'discourse/lib/page-tracker';
import KeyValueStore from 'discourse/lib/key-value-store';
import { onPageChange } from 'discourse/lib/page-tracker';
let primaryTab = false;
let liveEnabled = false;
@ -84,7 +84,8 @@ function setupNotifications() {
if (document) {
document.addEventListener("scroll", resetIdle);
}
PageTracker.on("change", resetIdle);
onPageChange(resetIdle);
}
function resetIdle() {

View File

@ -1,37 +1,31 @@
import Singleton from 'discourse/mixins/singleton';
const PageTracker = Ember.Object.extend(Ember.Evented);
let _pageTracker = PageTracker.create();
/**
Called whenever the "page" changes. This allows us to set up analytics
and other tracking.
let _started = false;
export function startPageTracking(router) {
if (_started) { return; }
To get notified when the page changes, you can install a hook like so:
router.on('didTransition', function() {
this.send('refreshTitle');
const url = Discourse.getURL(this.get('url'));
```javascript
PageTracker.current().on('change', function(url, title) {
console.log('the page changed to: ' + url + ' and title ' + title);
// Refreshing the title is debounced, so we need to trigger this in the
// next runloop to have the correct title.
Em.run.next(() => {
_pageTracker.trigger('change', url, Discourse.get('_docTitle'));
});
```
**/
const PageTracker = Ember.Object.extend(Ember.Evented, {
start: function() {
if (this.get('started')) { return; }
});
_started = true;
}
var router = Discourse.__container__.lookup('router:main'),
self = this;
export function onPageChange(fn) {
_pageTracker.on('change', fn);
}
router.on('didTransition', function() {
this.send('refreshTitle');
var url = Discourse.getURL(this.get('url'));
// Refreshing the title is debounced, so we need to trigger this in the
// next runloop to have the correct title.
Em.run.next(function() {
self.trigger('change', url, Discourse.get('_docTitle'));
});
});
this.set('started', true);
// backwards compatibility
export default {
current() {
console.warn(`Using PageTracker.current() is deprecated. Your plugin should use the PluginAPI`);
return _pageTracker;
}
});
PageTracker.reopenClass(Singleton);
export default PageTracker;
};

View File

@ -6,7 +6,7 @@ import { includeAttributes } from 'discourse/lib/transform-post';
import { addToolbarCallback } from 'discourse/components/d-editor';
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
import { decorateWidget } from 'discourse/widgets/widget';
import PageTracker from 'discourse/lib/page-tracker';
import { onPageChange } from 'discourse/lib/page-tracker';
let _decorateId = 0;
function decorate(klass, evt, cb) {
@ -139,8 +139,20 @@ class PluginApi {
addWidgetCleanCallback('post-stream', fn);
}
/**
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
api.onPageChange((url, title) => {
console.log('the page changed to: ' + url + ' and title ' + title);
});
```
**/
onPageChange(fn) {
PageTracker.current().on('change', fn);
onPageChange(fn);
}
}