FIX: prevents google to track certain pages (#7463)

This commit is contained in:
Joffrey JAFFEUX 2019-04-30 15:57:51 +02:00 committed by GitHub
parent 2ebe9e3a8b
commit 8e68244eea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -24,8 +24,10 @@ export default {
// if it is present // if it is present
if (typeof window._gaq !== "undefined") { if (typeof window._gaq !== "undefined") {
appEvents.on("page:changed", data => { appEvents.on("page:changed", data => {
window._gaq.push(["_set", "title", data.title]); if (!data.replacedOnlyQueryParams) {
window._gaq.push(["_trackPageview", data.url]); window._gaq.push(["_set", "title", data.title]);
window._gaq.push(["_trackPageview", data.url]);
}
}); });
return; return;
} }
@ -33,13 +35,19 @@ export default {
// 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") {
appEvents.on("page:changed", data => { appEvents.on("page:changed", data => {
window.ga("send", "pageview", { page: data.url, title: data.title }); if (!data.replacedOnlyQueryParams) {
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") {
appEvents.on("page:changed", googleTagManagerPageChanged); appEvents.on("page:changed", data => {
if (!data.replacedOnlyQueryParams) {
googleTagManagerPageChanged(data);
}
});
} }
} }
}; };

View File

@ -15,7 +15,12 @@ export function startPageTracking(router, appEvents) {
if (_started) { if (_started) {
return; return;
} }
router.on("routeDidChange", () => { router.on("routeDidChange", transition => {
// we ocassionally prevent tracking of replaced pages when only query params changed
// eg: google analytics
const replacedOnlyQueryParams =
transition.urlMethod === "replace" && transition.queryParamsOnly;
router.send("refreshTitle"); router.send("refreshTitle");
const url = Discourse.getURL(router.get("url")); const url = Discourse.getURL(router.get("url"));
@ -23,10 +28,12 @@ export function startPageTracking(router, appEvents) {
// next runloop to have the correct title. // next runloop to have the correct title.
Ember.run.next(() => { Ember.run.next(() => {
let title = Discourse.get("_docTitle"); let title = Discourse.get("_docTitle");
appEvents.trigger("page:changed", { appEvents.trigger("page:changed", {
url, url,
title, title,
currentRouteName: router.get("currentRouteName") currentRouteName: router.get("currentRouteName"),
replacedOnlyQueryParams
}); });
}); });