mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Use appEvents
for page tracking so widgets can listen to it easily
This commit is contained in:
parent
c7aa354ee8
commit
954013a45c
@ -1,9 +1,10 @@
|
|||||||
import { cleanDOM } from 'discourse/lib/clean-dom';
|
import { cleanDOM } from 'discourse/lib/clean-dom';
|
||||||
import { startPageTracking, onPageChange } from 'discourse/lib/page-tracker';
|
import { startPageTracking } from 'discourse/lib/page-tracker';
|
||||||
import { viewTrackingRequired } from 'discourse/lib/ajax';
|
import { viewTrackingRequired } from 'discourse/lib/ajax';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "page-tracking",
|
name: "page-tracking",
|
||||||
|
after: 'inject-objects',
|
||||||
|
|
||||||
initialize(container) {
|
initialize(container) {
|
||||||
|
|
||||||
@ -12,33 +13,34 @@ export default {
|
|||||||
router.on('willTransition', viewTrackingRequired);
|
router.on('willTransition', viewTrackingRequired);
|
||||||
router.on('didTransition', cleanDOM);
|
router.on('didTransition', cleanDOM);
|
||||||
|
|
||||||
startPageTracking(router);
|
let appEvents = container.lookup('app-events:main');
|
||||||
|
startPageTracking(router, appEvents);
|
||||||
|
|
||||||
// Out of the box, Discourse tries to track google analytics
|
// Out of the box, Discourse tries to track google analytics
|
||||||
// if it is present
|
// if it is present
|
||||||
if (typeof window._gaq !== 'undefined') {
|
if (typeof window._gaq !== 'undefined') {
|
||||||
onPageChange((url, title) => {
|
appEvents.on('page:changed', data => {
|
||||||
window._gaq.push(["_set", "title", title]);
|
window._gaq.push(["_set", "title", data.title]);
|
||||||
window._gaq.push(['_trackPageview', url]);
|
window._gaq.push(['_trackPageview', data.url]);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Also use Universal Analytics if it is present
|
// Also use Universal Analytics if it is present
|
||||||
if (typeof window.ga !== 'undefined') {
|
if (typeof window.ga !== 'undefined') {
|
||||||
onPageChange((url, title) => {
|
appEvents.on('page:changed', data => {
|
||||||
window.ga('send', 'pageview', {page: url, title: title});
|
window.ga('send', 'pageview', {page: data.url, title: data.title});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// And Google Tag Manager too
|
// And Google Tag Manager too
|
||||||
if (typeof window.dataLayer !== 'undefined') {
|
if (typeof window.dataLayer !== 'undefined') {
|
||||||
onPageChange((url, title) => {
|
appEvents.on('page:changed', data => {
|
||||||
window.dataLayer.push({
|
window.dataLayer.push({
|
||||||
'event': 'virtualPageView',
|
'event': 'virtualPageView',
|
||||||
'page': {
|
'page': {
|
||||||
'title': title,
|
'title': data.title,
|
||||||
'url': url
|
'url': data.url
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -13,6 +13,7 @@ export default {
|
|||||||
const user = container.lookup('current-user:main');
|
const user = container.lookup('current-user:main');
|
||||||
const keyValueStore = container.lookup('key-value-store:main');
|
const keyValueStore = container.lookup('key-value-store:main');
|
||||||
const bus = container.lookup('message-bus:main');
|
const bus = container.lookup('message-bus:main');
|
||||||
|
const appEvents = container.lookup('app-events:main');
|
||||||
|
|
||||||
// clear old cached notifications, we used to store in local storage
|
// clear old cached notifications, we used to store in local storage
|
||||||
// TODO 2017 delete this line
|
// TODO 2017 delete this line
|
||||||
@ -33,7 +34,6 @@ export default {
|
|||||||
|
|
||||||
bus.subscribe(`/notification/${user.get('id')}`, data => {
|
bus.subscribe(`/notification/${user.get('id')}`, data => {
|
||||||
const store = container.lookup('store:main');
|
const store = container.lookup('store:main');
|
||||||
const appEvents = container.lookup('app-events:main');
|
|
||||||
|
|
||||||
const oldUnread = user.get('unread_notifications');
|
const oldUnread = user.get('unread_notifications');
|
||||||
const oldPM = user.get('unread_private_messages');
|
const oldPM = user.get('unread_private_messages');
|
||||||
@ -102,7 +102,7 @@ export default {
|
|||||||
if (!Ember.testing) {
|
if (!Ember.testing) {
|
||||||
if (!site.mobileView) {
|
if (!site.mobileView) {
|
||||||
bus.subscribe(alertChannel(user), data => onNotification(data, user));
|
bus.subscribe(alertChannel(user), data => onNotification(data, user));
|
||||||
initDesktopNotifications(bus);
|
initDesktopNotifications(bus, appEvents);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
import DiscourseURL from 'discourse/lib/url';
|
import DiscourseURL from 'discourse/lib/url';
|
||||||
import KeyValueStore from 'discourse/lib/key-value-store';
|
import KeyValueStore from 'discourse/lib/key-value-store';
|
||||||
import { onPageChange } from 'discourse/lib/page-tracker';
|
|
||||||
|
|
||||||
let primaryTab = false;
|
let primaryTab = false;
|
||||||
let liveEnabled = false;
|
let liveEnabled = false;
|
||||||
@ -15,7 +14,7 @@ const context = "discourse_desktop_notifications_";
|
|||||||
const keyValueStore = new KeyValueStore(context);
|
const keyValueStore = new KeyValueStore(context);
|
||||||
|
|
||||||
// Called from an initializer
|
// Called from an initializer
|
||||||
function init(messageBus) {
|
function init(messageBus, appEvents) {
|
||||||
liveEnabled = false;
|
liveEnabled = false;
|
||||||
mbClientId = messageBus.clientId;
|
mbClientId = messageBus.clientId;
|
||||||
|
|
||||||
@ -49,14 +48,14 @@ function init(messageBus) {
|
|||||||
liveEnabled = true;
|
liveEnabled = true;
|
||||||
try {
|
try {
|
||||||
// Preliminary checks passed, continue with setup
|
// Preliminary checks passed, continue with setup
|
||||||
setupNotifications();
|
setupNotifications(appEvents);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
Em.Logger.error(e);
|
Em.Logger.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function is only called if permission was granted
|
// This function is only called if permission was granted
|
||||||
function setupNotifications() {
|
function setupNotifications(appEvents) {
|
||||||
|
|
||||||
window.addEventListener("storage", function(e) {
|
window.addEventListener("storage", function(e) {
|
||||||
// note: This event only fires when other tabs setItem()
|
// note: This event only fires when other tabs setItem()
|
||||||
@ -85,7 +84,7 @@ function setupNotifications() {
|
|||||||
document.addEventListener("scroll", resetIdle);
|
document.addEventListener("scroll", resetIdle);
|
||||||
}
|
}
|
||||||
|
|
||||||
onPageChange(resetIdle);
|
appEvents.on('page:changed', resetIdle);
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetIdle() {
|
function resetIdle() {
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
import deprecated from 'discourse-common/lib/deprecated';
|
|
||||||
|
|
||||||
const PageTracker = Ember.Object.extend(Ember.Evented);
|
|
||||||
let _pageTracker = PageTracker.create();
|
|
||||||
|
|
||||||
let _started = false;
|
let _started = false;
|
||||||
|
|
||||||
const cache = {};
|
const cache = {};
|
||||||
@ -16,7 +11,7 @@ export function getTransient(key) {
|
|||||||
return cache[key];
|
return cache[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function startPageTracking(router) {
|
export function startPageTracking(router, appEvents) {
|
||||||
if (_started) { return; }
|
if (_started) { return; }
|
||||||
|
|
||||||
router.on('didTransition', function() {
|
router.on('didTransition', function() {
|
||||||
@ -25,7 +20,10 @@ export function startPageTracking(router) {
|
|||||||
|
|
||||||
// Refreshing the title is debounced, so we need to trigger this in the
|
// Refreshing the title is debounced, so we need to trigger this in the
|
||||||
// next runloop to have the correct title.
|
// next runloop to have the correct title.
|
||||||
Em.run.next(() => _pageTracker.trigger('change', url, Discourse.get('_docTitle')));
|
Ember.run.next(() => {
|
||||||
|
let title = Discourse.get('_docTitle');
|
||||||
|
appEvents.trigger('page:changed', { url, title });
|
||||||
|
});
|
||||||
|
|
||||||
transitionCount++;
|
transitionCount++;
|
||||||
_.each(cache, (v,k) => {
|
_.each(cache, (v,k) => {
|
||||||
@ -36,18 +34,3 @@ export function startPageTracking(router) {
|
|||||||
});
|
});
|
||||||
_started = true;
|
_started = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function onPageChange(fn) {
|
|
||||||
_pageTracker.on('change', fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
// backwards compatibility
|
|
||||||
const BackwardsCompat = {
|
|
||||||
current() {
|
|
||||||
deprecated(`Using PageTracker.current() is deprecated. Your plugin should use the PluginAPI`);
|
|
||||||
return _pageTracker;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Discourse.PageTracker = BackwardsCompat;
|
|
||||||
export default BackwardsCompat;
|
|
||||||
|
@ -6,7 +6,6 @@ import { includeAttributes } from 'discourse/lib/transform-post';
|
|||||||
import { addToolbarCallback } from 'discourse/components/d-editor';
|
import { addToolbarCallback } from 'discourse/components/d-editor';
|
||||||
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
|
import { addWidgetCleanCallback } from 'discourse/components/mount-widget';
|
||||||
import { createWidget, reopenWidget, decorateWidget, changeSetting } from 'discourse/widgets/widget';
|
import { createWidget, reopenWidget, decorateWidget, changeSetting } from 'discourse/widgets/widget';
|
||||||
import { onPageChange } from 'discourse/lib/page-tracker';
|
|
||||||
import { preventCloak } from 'discourse/widgets/post-stream';
|
import { preventCloak } from 'discourse/widgets/post-stream';
|
||||||
import { h } from 'virtual-dom';
|
import { h } from 'virtual-dom';
|
||||||
import { addFlagProperty } from 'discourse/components/site-header';
|
import { addFlagProperty } from 'discourse/components/site-header';
|
||||||
@ -350,7 +349,8 @@ class PluginApi {
|
|||||||
```
|
```
|
||||||
**/
|
**/
|
||||||
onPageChange(fn) {
|
onPageChange(fn) {
|
||||||
onPageChange(fn);
|
let appEvents = this.container.lookup('app-events:main');
|
||||||
|
appEvents.on('page:changed', data => fn(data.url, data.title));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user