mirror of
https://github.com/discourse/discourse.git
synced 2025-01-19 05:43:16 +08: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 { startPageTracking, onPageChange } from 'discourse/lib/page-tracker';
|
||||
import { startPageTracking } from 'discourse/lib/page-tracker';
|
||||
import { viewTrackingRequired } from 'discourse/lib/ajax';
|
||||
|
||||
export default {
|
||||
name: "page-tracking",
|
||||
after: 'inject-objects',
|
||||
|
||||
initialize(container) {
|
||||
|
||||
|
@ -12,33 +13,34 @@ export default {
|
|||
router.on('willTransition', viewTrackingRequired);
|
||||
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
|
||||
// if it is present
|
||||
if (typeof window._gaq !== 'undefined') {
|
||||
onPageChange((url, title) => {
|
||||
window._gaq.push(["_set", "title", title]);
|
||||
window._gaq.push(['_trackPageview', url]);
|
||||
appEvents.on('page:changed', data => {
|
||||
window._gaq.push(["_set", "title", data.title]);
|
||||
window._gaq.push(['_trackPageview', data.url]);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Also use Universal Analytics if it is present
|
||||
if (typeof window.ga !== 'undefined') {
|
||||
onPageChange((url, title) => {
|
||||
window.ga('send', 'pageview', {page: url, title: title});
|
||||
appEvents.on('page:changed', data => {
|
||||
window.ga('send', 'pageview', {page: data.url, title: data.title});
|
||||
});
|
||||
}
|
||||
|
||||
// And Google Tag Manager too
|
||||
if (typeof window.dataLayer !== 'undefined') {
|
||||
onPageChange((url, title) => {
|
||||
appEvents.on('page:changed', data => {
|
||||
window.dataLayer.push({
|
||||
'event': 'virtualPageView',
|
||||
'page': {
|
||||
'title': title,
|
||||
'url': url
|
||||
'title': data.title,
|
||||
'url': data.url
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
@ -13,6 +13,7 @@ export default {
|
|||
const user = container.lookup('current-user:main');
|
||||
const keyValueStore = container.lookup('key-value-store: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
|
||||
// TODO 2017 delete this line
|
||||
|
@ -33,7 +34,6 @@ export default {
|
|||
|
||||
bus.subscribe(`/notification/${user.get('id')}`, data => {
|
||||
const store = container.lookup('store:main');
|
||||
const appEvents = container.lookup('app-events:main');
|
||||
|
||||
const oldUnread = user.get('unread_notifications');
|
||||
const oldPM = user.get('unread_private_messages');
|
||||
|
@ -102,7 +102,7 @@ export default {
|
|||
if (!Ember.testing) {
|
||||
if (!site.mobileView) {
|
||||
bus.subscribe(alertChannel(user), data => onNotification(data, user));
|
||||
initDesktopNotifications(bus);
|
||||
initDesktopNotifications(bus, appEvents);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import DiscourseURL from 'discourse/lib/url';
|
||||
import KeyValueStore from 'discourse/lib/key-value-store';
|
||||
import { onPageChange } from 'discourse/lib/page-tracker';
|
||||
|
||||
let primaryTab = false;
|
||||
let liveEnabled = false;
|
||||
|
@ -15,7 +14,7 @@ const context = "discourse_desktop_notifications_";
|
|||
const keyValueStore = new KeyValueStore(context);
|
||||
|
||||
// Called from an initializer
|
||||
function init(messageBus) {
|
||||
function init(messageBus, appEvents) {
|
||||
liveEnabled = false;
|
||||
mbClientId = messageBus.clientId;
|
||||
|
||||
|
@ -49,14 +48,14 @@ function init(messageBus) {
|
|||
liveEnabled = true;
|
||||
try {
|
||||
// Preliminary checks passed, continue with setup
|
||||
setupNotifications();
|
||||
setupNotifications(appEvents);
|
||||
} catch (e) {
|
||||
Em.Logger.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
// This function is only called if permission was granted
|
||||
function setupNotifications() {
|
||||
function setupNotifications(appEvents) {
|
||||
|
||||
window.addEventListener("storage", function(e) {
|
||||
// note: This event only fires when other tabs setItem()
|
||||
|
@ -85,7 +84,7 @@ function setupNotifications() {
|
|||
document.addEventListener("scroll", resetIdle);
|
||||
}
|
||||
|
||||
onPageChange(resetIdle);
|
||||
appEvents.on('page:changed', 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;
|
||||
|
||||
const cache = {};
|
||||
|
@ -16,7 +11,7 @@ export function getTransient(key) {
|
|||
return cache[key];
|
||||
}
|
||||
|
||||
export function startPageTracking(router) {
|
||||
export function startPageTracking(router, appEvents) {
|
||||
if (_started) { return; }
|
||||
|
||||
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
|
||||
// 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++;
|
||||
_.each(cache, (v,k) => {
|
||||
|
@ -36,18 +34,3 @@ export function startPageTracking(router) {
|
|||
});
|
||||
_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 { addWidgetCleanCallback } from 'discourse/components/mount-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 { h } from 'virtual-dom';
|
||||
import { addFlagProperty } from 'discourse/components/site-header';
|
||||
|
@ -350,7 +349,8 @@ class PluginApi {
|
|||
```
|
||||
**/
|
||||
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