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

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

diff --git a/app/assets/javascripts/discourse/initializers/page-tracking.js.es6 b/app/assets/javascripts/discourse/initializers/page-tracking.js.es6
index 84a9d51..06653ce 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 6a36cc7..0c9e944 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
       });
     });

GitHub sha: 8e68244e

1 Like