summaryrefslogtreecommitdiffstats
path: root/browser/extensions/webcompat/shims/google-analytics-and-tag-manager.js
blob: 8809fca8ece18422ea8eede2d45248d299a0bf8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

/**
 * Bug 1713687 - Shim Google Analytics and Tag Manager
 *
 * Sites often rely on the Google Analytics window object and will
 * break if it fails to load or is blocked. This shim works around
 * such breakage.
 *
 * Sites also often use the Google Optimizer (asynchide) code snippet,
 * only for it to cause multi-second delays if Google Analytics does
 * not load. This shim also avoids such delays.
 *
 * They also rely on Google Tag Manager, which often goes hand-in-
 * hand with Analytics, but is not always blocked by anti-tracking
 * lists. Handling both in the same shim handles both cases.
 */

if (window[window.GoogleAnalyticsObject || "ga"]?.loaded === undefined) {
  const DEFAULT_TRACKER_NAME = "t0";

  const trackers = new Map();

  const run = function (fn, ...args) {
    if (typeof fn === "function") {
      try {
        fn(...args);
      } catch (e) {
        console.error(e);
      }
    }
  };

  const create = (id, cookie, name, opts) => {
    id = id || opts?.trackerId;
    if (!id) {
      return undefined;
    }
    cookie = cookie || opts?.cookieDomain || "_ga";
    name = name || opts?.name || DEFAULT_TRACKER_NAME;
    if (!trackers.has(name)) {
      let props;
      try {
        props = new Map(Object.entries(opts));
      } catch (_) {
        props = new Map();
      }
      trackers.set(name, {
        get(p) {
          if (p === "name") {
            return name;
          } else if (p === "trackingId") {
            return id;
          } else if (p === "cookieDomain") {
            return cookie;
          }
          return props.get(p);
        },
        ma() {},
        requireSync() {},
        send() {},
        set(p, v) {
          if (typeof p !== "object") {
            p = Object.fromEntries([[p, v]]);
          }
          for (const k in p) {
            props.set(k, p[k]);
            if (k === "hitCallback") {
              run(p[k]);
            }
          }
        },
      });
    }
    return trackers.get(name);
  };

  const cmdRE = /((?<name>.*?)\.)?((?<plugin>.*?):)?(?<method>.*)/;

  function ga(cmd, ...args) {
    if (arguments.length === 1 && typeof cmd === "function") {
      run(cmd, trackers.get(DEFAULT_TRACKER_NAME));
      return undefined;
    }

    if (typeof cmd !== "string") {
      return undefined;
    }

    const groups = cmdRE.exec(cmd)?.groups;
    if (!groups) {
      console.error("Could not parse GA command", cmd);
      return undefined;
    }

    let { name, plugin, method } = groups;

    if (plugin) {
      return undefined;
    }

    if (cmd === "set") {
      trackers.get(name)?.set(args[0], args[1]);
    }

    if (method === "remove") {
      trackers.delete(name);
      return undefined;
    }

    if (cmd === "send") {
      run(args.at(-1)?.hitCallback);
      return undefined;
    }

    if (method === "create") {
      let id, cookie, fields;
      for (const param of args.slice(0, 4)) {
        if (typeof param === "object") {
          fields = param;
          break;
        }
        if (id === undefined) {
          id = param;
        } else if (cookie === undefined) {
          cookie = param;
        } else {
          name = param;
        }
      }
      return create(id, cookie, name, fields);
    }

    return undefined;
  }

  Object.assign(ga, {
    create: (a, b, c, d) => ga("create", a, b, c, d),
    getAll: () => Array.from(trackers.values()),
    getByName: name => trackers.get(name),
    loaded: true,
    remove: t => ga("remove", t),
  });

  // Process any GA command queue the site pre-declares (bug 1736850)
  const q = window[window.GoogleAnalyticsObject || "ga"]?.q;
  window[window.GoogleAnalyticsObject || "ga"] = ga;

  if (Array.isArray(q)) {
    const push = o => {
      ga(...o);
      return true;
    };
    q.push = push;
    q.forEach(o => push(o));
  }

  // Also process the Google Tag Manager dataLayer (bug 1713688)
  const dl = window.dataLayer;

  if (Array.isArray(dl) && !dl.find(e => e["gtm.start"])) {
    const push = function (o) {
      setTimeout(() => run(o?.eventCallback), 1);
      return true;
    };
    dl.push = push;
    dl.forEach(o => push(o));
  }

  // Run dataLayer.hide.end to handle asynchide (bug 1628151)
  run(window.dataLayer?.hide?.end);
}

if (!window?.gaplugins?.Linker) {
  window.gaplugins = window.gaplugins || {};
  window.gaplugins.Linker = class {
    autoLink() {}
    decorate(url) {
      return url;
    }
    passthrough() {}
  };
}