This repository has been archived on 2023-03-18. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
osr-discourse-src/app/assets/javascripts/discourse/initializers/page-tracking.js.es6
T
Robin Ward f5d391a48a REFACTOR: Move app-events:main to service:app-events (#8152)
AppEvents was always a service object in disguise, so we should move it
to the correct place in the application. Doing this allows other service
objects to inject it easily without container access.

In the future we should also deprecate `this.appEvents` without an
explicit injection too.
2019-10-04 10:06:08 -04:00

59 lines
1.6 KiB
JavaScript

import { cleanDOM } from "discourse/lib/clean-dom";
import {
startPageTracking,
resetPageTracking,
googleTagManagerPageChanged
} from "discourse/lib/page-tracker";
import { viewTrackingRequired } from "discourse/lib/ajax";
export default {
name: "page-tracking",
after: "inject-objects",
initialize(container) {
// Tell our AJAX system to track a page transition
const router = container.lookup("router:main");
router.on("routeWillChange", viewTrackingRequired);
router.on("routeDidChange", cleanDOM);
let appEvents = container.lookup("service:app-events");
startPageTracking(router, appEvents);
// Out of the box, Discourse tries to track google analytics
// if it is present
if (typeof window._gaq !== "undefined") {
appEvents.on("page:changed", data => {
if (!data.replacedOnlyQueryParams) {
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") {
appEvents.on("page:changed", data => {
if (!data.replacedOnlyQueryParams) {
window.ga("send", "pageview", { page: data.url, title: data.title });
}
});
}
// And Google Tag Manager too
if (typeof window.dataLayer !== "undefined") {
appEvents.on("page:changed", data => {
if (!data.replacedOnlyQueryParams) {
googleTagManagerPageChanged(data);
}
});
}
},
teardown() {
resetPageTracking();
}
};