From e848ea5a998b2fa92b7e7fc0c1885d7047f1c8e7 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 19 Feb 2016 16:30:59 -0500 Subject: [PATCH] Depcreate `PageTracker` in favor of `PluginAPI` --- .../initializers/page-tracking.js.es6 | 9 ++-- .../lib/desktop-notifications.js.es6 | 5 +- .../discourse/lib/page-tracker.js.es6 | 54 +++++++++---------- .../discourse/lib/plugin-api.js.es6 | 16 +++++- 4 files changed, 45 insertions(+), 39 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 index 2511dd3ebac..f458900a087 100644 --- a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 +++ b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 @@ -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}); }); } diff --git a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 index d20a1c0109d..de1d6afc338 100644 --- a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 +++ b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 @@ -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() { diff --git a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 index 351c78211ed..7ef49363b9d 100644 --- a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 +++ b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 @@ -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; +}; diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 1a90fc088a9..a012eb55e55 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -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); } }