From 8e68244eea470efe0418dc1ab68a3687e81f8733 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 30 Apr 2019 15:57:51 +0200 Subject: [PATCH] FIX: prevents google to track certain pages (#7463) --- .../discourse/initializers/page-tracking.js.es6 | 16 ++++++++++++---- .../discourse/lib/page-tracker.js.es6 | 11 +++++++++-- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 index 84a9d51de79..06653ce1752 100644 --- a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 +++ b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 @@ -24,8 +24,10 @@ export default { // if it is present if (typeof window._gaq !== "undefined") { appEvents.on("page:changed", data => { - window._gaq.push(["_set", "title", data.title]); - window._gaq.push(["_trackPageview", data.url]); + if (!data.replacedOnlyQueryParams) { + window._gaq.push(["_set", "title", data.title]); + window._gaq.push(["_trackPageview", data.url]); + } }); return; } @@ -33,13 +35,19 @@ export default { // Also use Universal Analytics if it is present if (typeof window.ga !== "undefined") { 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 if (typeof window.dataLayer !== "undefined") { - appEvents.on("page:changed", googleTagManagerPageChanged); + appEvents.on("page:changed", data => { + if (!data.replacedOnlyQueryParams) { + googleTagManagerPageChanged(data); + } + }); } } }; diff --git a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 index 6a36cc7cbc6..0c9e9445c98 100644 --- a/app/assets/javascripts/discourse/lib/page-tracker.js.es6 +++ b/app/assets/javascripts/discourse/lib/page-tracker.js.es6 @@ -15,7 +15,12 @@ export function startPageTracking(router, appEvents) { if (_started) { 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"); const url = Discourse.getURL(router.get("url")); @@ -23,10 +28,12 @@ export function startPageTracking(router, appEvents) { // next runloop to have the correct title. Ember.run.next(() => { let title = Discourse.get("_docTitle"); + appEvents.trigger("page:changed", { url, title, - currentRouteName: router.get("currentRouteName") + currentRouteName: router.get("currentRouteName"), + replacedOnlyQueryParams }); });