diff --git a/app/components/Analytics.tsx b/app/components/Analytics.tsx index cbb4abfacb..b60436f0ad 100644 --- a/app/components/Analytics.tsx +++ b/app/components/Analytics.tsx @@ -1,4 +1,3 @@ -/* global ga */ import { escape } from "es-toolkit/compat"; import * as React from "react"; import type { PublicEnv } from "@shared/types"; @@ -11,35 +10,6 @@ type Props = { // TODO: Refactor this component to allow injection from plugins const Analytics: React.FC = ({ children }: Props) => { - // Google Analytics 3 - React.useEffect(() => { - if (!env.GOOGLE_ANALYTICS_ID?.startsWith("UA-")) { - return; - } - - // standard Google Analytics script - window.ga = - window.ga || - function (...args) { - (ga.q = ga.q || []).push(args); - }; - - ga.l = +new Date(); - ga("create", env.GOOGLE_ANALYTICS_ID, "auto"); - ga("send", "pageview"); - const script = document.createElement("script"); - script.type = "text/javascript"; - script.src = "https://www.google-analytics.com/analytics.js"; - script.async = true; - - // Track PWA install event - window.addEventListener("appinstalled", () => { - ga("send", "event", "pwa", "install"); - }); - - document.getElementsByTagName("head")[0]?.appendChild(script); - }, []); - // Google Analytics 4 React.useEffect(() => { const measurementIds = []; diff --git a/app/index.tsx b/app/index.tsx index 2d3546cc65..6f16183e9a 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -110,20 +110,6 @@ if (element) { }); } -window.addEventListener("load", async () => { - // installation does not use Google Analytics, or tracking is blocked on client - // no point loading the rest of the analytics bundles - if (!env.GOOGLE_ANALYTICS_ID || !window.ga) { - return; - } - await import("~/utils/autotrack"); - window.ga("require", "outboundLinkTracker"); - window.ga("require", "urlChangeTracker"); - window.ga("require", "eventTracker", { - attributePrefix: "data-", - }); -}); - // Skip registration on public share views, anonymous visitors gain nothing // from having the app precached. if ( diff --git a/app/typings/index.d.ts b/app/typings/index.d.ts index 18f91d7e96..5650d05229 100644 --- a/app/typings/index.d.ts +++ b/app/typings/index.d.ts @@ -1,5 +1,3 @@ -declare module "~/utils/autotrack"; - declare module "vite/modulepreload-polyfill"; declare module "string-replace-to-array"; diff --git a/app/typings/window.d.ts b/app/typings/window.d.ts index e0014385a6..7411e72f82 100644 --- a/app/typings/window.d.ts +++ b/app/typings/window.d.ts @@ -1,4 +1,3 @@ -/// import type RootStore from "~/stores/RootStore"; declare global { diff --git a/app/utils/Analytics.ts b/app/utils/Analytics.ts index 4eb4bf8416..b49fbb590e 100644 --- a/app/utils/Analytics.ts +++ b/app/utils/Analytics.ts @@ -14,11 +14,6 @@ export default class Analytics { action: string, metadata?: Record ) => { - // GA3 - if (window.ga) { - window.ga("send", "event", event, action); - } - // GA4 if (window.gtag) { window.gtag("event", event, { action, ...metadata }); diff --git a/app/utils/autotrack.js b/app/utils/autotrack.js deleted file mode 100644 index b48584d741..0000000000 --- a/app/utils/autotrack.js +++ /dev/null @@ -1,707 +0,0 @@ -/** - * Vendored from https://github.com/googleanalytics/autotrack (v2.4.1) - * and its dependency https://github.com/philipwalton/dom-utils (v0.9.0) - * - * Copyright 2016 Google Inc. All Rights Reserved. - * Licensed under the Apache License, Version 2.0 - * - * Only the three plugins used by Outline are included: - * - eventTracker - * - outboundLinkTracker - * - urlChangeTracker - * - * The original libraries are no longer maintained. - */ - -/* oxlint-disable */ - -// --------------------------------------------------------------------------- -// dom-utils: matches -// --------------------------------------------------------------------------- - -const nativeMatches = - Element.prototype.matches || Element.prototype.webkitMatchesSelector; - -function matchesSelector(element, selector) { - if (typeof selector != "string") { - return false; - } - if (nativeMatches) { - return nativeMatches.call(element, selector); - } - const nodes = element.parentNode.querySelectorAll(selector); - for (let i = 0, node; (node = nodes[i]); i++) { - if (node == element) { - return true; - } - } - return false; -} - -function matches(element, test) { - if (element && element.nodeType == 1 && test) { - if (typeof test == "string" || test.nodeType == 1) { - return element == test || matchesSelector(element, test); - } else if ("length" in test) { - for (let i = 0, item; (item = test[i]); i++) { - if (element == item || matchesSelector(element, item)) { - return true; - } - } - } - } - return false; -} - -// --------------------------------------------------------------------------- -// dom-utils: parents -// --------------------------------------------------------------------------- - -function parents(element) { - const list = []; - while (element && element.parentNode && element.parentNode.nodeType == 1) { - element = element.parentNode; - list.push(element); - } - return list; -} - -// --------------------------------------------------------------------------- -// dom-utils: closest -// --------------------------------------------------------------------------- - -function closest(element, selector, shouldCheckSelf = false) { - if (!(element && element.nodeType == 1 && selector)) { - return; - } - const parentElements = (shouldCheckSelf ? [element] : []).concat( - parents(element) - ); - for (let i = 0, parent; (parent = parentElements[i]); i++) { - if (matches(parent, selector)) { - return parent; - } - } -} - -// --------------------------------------------------------------------------- -// dom-utils: delegate -// --------------------------------------------------------------------------- - -function delegate(ancestor, eventType, selector, callback, opts = {}) { - const listener = function (event) { - let delegateTarget; - - if (opts.composed && typeof event.composedPath == "function") { - const composedPath = event.composedPath(); - for (let i = 0, node; (node = composedPath[i]); i++) { - if (node.nodeType == 1 && matches(node, selector)) { - delegateTarget = node; - } - } - } else { - delegateTarget = closest(event.target, selector, true); - } - - if (delegateTarget) { - callback.call(delegateTarget, event, delegateTarget); - } - }; - - ancestor.addEventListener(eventType, listener, opts.useCapture); - - return { - destroy() { - ancestor.removeEventListener(eventType, listener, opts.useCapture); - }, - }; -} - -// --------------------------------------------------------------------------- -// dom-utils: parseUrl -// --------------------------------------------------------------------------- - -const HTTP_PORT = "80"; -const HTTPS_PORT = "443"; -const DEFAULT_PORT = RegExp(":(" + HTTP_PORT + "|" + HTTPS_PORT + ")$"); - -const anchor = document.createElement("a"); -const urlCache = {}; - -function parseUrl(url) { - url = !url || url == "." ? location.href : url; - - if (urlCache[url]) { - return urlCache[url]; - } - - anchor.href = url; - - if (url.charAt(0) == "." || url.charAt(0) == "/") { - return parseUrl(anchor.href); - } - - let port = - anchor.port == HTTP_PORT || anchor.port == HTTPS_PORT ? "" : anchor.port; - port = port == "0" ? "" : port; - - const host = anchor.host.replace(DEFAULT_PORT, ""); - const origin = anchor.origin ? anchor.origin : anchor.protocol + "//" + host; - const pathname = - anchor.pathname.charAt(0) == "/" ? anchor.pathname : "/" + anchor.pathname; - - return (urlCache[url] = { - hash: anchor.hash, - host, - hostname: anchor.hostname, - href: anchor.href, - origin, - pathname, - port, - protocol: anchor.protocol, - search: anchor.search, - }); -} - -// --------------------------------------------------------------------------- -// dom-utils: getAttributes -// --------------------------------------------------------------------------- - -function getAttributes(element) { - const attrs = {}; - if (!(element && element.nodeType == 1)) { - return attrs; - } - const map = element.attributes; - if (map.length === 0) { - return {}; - } - for (let i = 0, attr; (attr = map[i]); i++) { - attrs[attr.name] = attr.value; - } - return attrs; -} - -// --------------------------------------------------------------------------- -// autotrack: utilities -// --------------------------------------------------------------------------- - -function camelCase(str) { - return str.replace(/[-_]+(\w?)/g, function (_match, p1) { - return p1.toUpperCase(); - }); -} - -function capitalize(str) { - return str.charAt(0).toUpperCase() + str.slice(1); -} - -function createFieldsObj( - defaultFields, - userFields, - tracker, - hitFilter, - target, - event -) { - if (typeof hitFilter == "function") { - const originalBuildHitTask = tracker.get("buildHitTask"); - return { - buildHitTask: (model) => { - model.set(defaultFields, null, true); - model.set(userFields, null, true); - hitFilter(model, target, event); - originalBuildHitTask(model); - }, - }; - } else { - return Object.assign({}, defaultFields, userFields); - } -} - -function getAttributeFields(element, prefix) { - const attributes = getAttributes(element); - const attributeFields = {}; - - Object.keys(attributes).forEach(function (attribute) { - if (attribute.indexOf(prefix) === 0 && attribute != prefix + "on") { - let value = attributes[attribute]; - if (value == "true") { - value = true; - } - if (value == "false") { - value = false; - } - const field = camelCase(attribute.slice(prefix.length)); - attributeFields[field] = value; - } - }); - - return attributeFields; -} - -function withTimeout(callback, wait = 2000) { - let called = false; - const fn = function () { - if (!called) { - called = true; - callback(); - } - }; - setTimeout(fn, wait); - return fn; -} - -// --------------------------------------------------------------------------- -// autotrack: method-chain -// --------------------------------------------------------------------------- - -const methodChainInstances = []; - -function getOrCreateMethodChain(context, methodName) { - let chain = methodChainInstances.filter( - (h) => h.context == context && h.methodName == methodName - )[0]; - - if (!chain) { - chain = createMethodChain(context, methodName); - methodChainInstances.push(chain); - } - return chain; -} - -function createMethodChain(context, methodName) { - const isTask = /Task$/.test(methodName); - const originalMethodReference = isTask - ? context.get(methodName) - : context[methodName]; - - const instance = { - context, - methodName, - isTask, - originalMethodReference, - methodChain: [], - boundMethodChain: [], - wrappedMethod(...args) { - const lastBoundMethod = - instance.boundMethodChain[instance.boundMethodChain.length - 1]; - return lastBoundMethod(...args); - }, - add(overrideMethod) { - this.methodChain.push(overrideMethod); - this.rebindMethodChain(); - }, - remove(overrideMethod) { - const index = this.methodChain.indexOf(overrideMethod); - if (index > -1) { - this.methodChain.splice(index, 1); - if (this.methodChain.length > 0) { - this.rebindMethodChain(); - } else { - this.destroy(); - } - } - }, - rebindMethodChain() { - this.boundMethodChain = []; - for (let method, i = 0; (method = this.methodChain[i]); i++) { - const previousMethod = - this.boundMethodChain[i - 1] || - this.originalMethodReference.bind(this.context); - this.boundMethodChain.push(method(previousMethod)); - } - }, - destroy() { - const index = methodChainInstances.indexOf(this); - if (index > -1) { - methodChainInstances.splice(index, 1); - if (this.isTask) { - this.context.set(this.methodName, this.originalMethodReference); - } else { - this.context[this.methodName] = this.originalMethodReference; - } - } - }, - }; - - if (isTask) { - context.set(methodName, instance.wrappedMethod); - } else { - context[methodName] = instance.wrappedMethod; - } - - return instance; -} - -const MethodChain = { - add(context, methodName, methodOverride) { - getOrCreateMethodChain(context, methodName).add(methodOverride); - }, - remove(context, methodName, methodOverride) { - getOrCreateMethodChain(context, methodName).remove(methodOverride); - }, -}; - -// --------------------------------------------------------------------------- -// autotrack: usage tracking -// --------------------------------------------------------------------------- - -const VERSION = "2.4.1"; -const DEV_ID = "i5iSjo"; -const VERSION_PARAM = "_av"; -const USAGE_PARAM = "_au"; - -const pluginIds = { - EVENT_TRACKER: 2, - OUTBOUND_LINK_TRACKER: 6, - URL_CHANGE_TRACKER: 9, -}; - -const PLUGIN_COUNT = 10; - -function convertHexToBin(hex) { - return parseInt(hex || "0", 16).toString(2); -} - -function convertBinToHex(bin) { - return parseInt(bin || "0", 2).toString(16); -} - -function padZeros(str, len) { - while (str.length < len) { - str = "0" + str; - } - return str; -} - -function flipBitOn(str, index) { - return str.substr(0, index) + 1 + str.substr(index + 1); -} - -function trackPlugin(tracker, pluginIndex) { - const usageHex = tracker.get("&" + USAGE_PARAM); - let usageBin = padZeros(convertHexToBin(usageHex), PLUGIN_COUNT); - usageBin = flipBitOn(usageBin, PLUGIN_COUNT - pluginIndex); - tracker.set("&" + USAGE_PARAM, convertBinToHex(usageBin)); -} - -function trackVersion(tracker) { - tracker.set("&" + VERSION_PARAM, VERSION); -} - -function trackUsage(tracker, plugin) { - trackVersion(tracker); - trackPlugin(tracker, plugin); -} - -// --------------------------------------------------------------------------- -// autotrack: provide (plugin registration) -// --------------------------------------------------------------------------- - -function provide(pluginName, pluginConstructor) { - const gaAlias = window.GoogleAnalyticsObject || "ga"; - window[gaAlias] = - window[gaAlias] || - function (...args) { - (window[gaAlias].q = window[gaAlias].q || []).push(args); - }; - - window.gaDevIds = window.gaDevIds || []; - if (window.gaDevIds.indexOf(DEV_ID) < 0) { - window.gaDevIds.push(DEV_ID); - } - - window[gaAlias]("provide", pluginName, pluginConstructor); - - window.gaplugins = window.gaplugins || {}; - window.gaplugins[capitalize(pluginName)] = pluginConstructor; -} - -// --------------------------------------------------------------------------- -// Plugin: eventTracker -// --------------------------------------------------------------------------- - -class EventTracker { - constructor(tracker, opts) { - trackUsage(tracker, pluginIds.EVENT_TRACKER); - - if (!window.addEventListener) { - return; - } - - const defaultOpts = { - events: ["click"], - fieldsObj: {}, - attributePrefix: "ga-", - }; - - this.opts = Object.assign(defaultOpts, opts); - this.tracker = tracker; - this.handleEvents = this.handleEvents.bind(this); - - const selector = "[" + this.opts.attributePrefix + "on]"; - - this.delegates = {}; - this.opts.events.forEach((event) => { - this.delegates[event] = delegate( - document, - event, - selector, - this.handleEvents, - { composed: true, useCapture: true } - ); - }); - } - - handleEvents(event, element) { - const prefix = this.opts.attributePrefix; - const events = element.getAttribute(prefix + "on").split(/\s*,\s*/); - - if (events.indexOf(event.type) < 0) { - return; - } - - const defaultFields = { transport: "beacon" }; - const attributeFields = getAttributeFields(element, prefix); - const userFields = Object.assign({}, this.opts.fieldsObj, attributeFields); - const hitType = attributeFields.hitType || "event"; - - this.tracker.send( - hitType, - createFieldsObj( - defaultFields, - userFields, - this.tracker, - this.opts.hitFilter, - element, - event - ) - ); - } - - remove() { - Object.keys(this.delegates).forEach((key) => { - this.delegates[key].destroy(); - }); - } -} - -provide("eventTracker", EventTracker); - -// --------------------------------------------------------------------------- -// Plugin: outboundLinkTracker -// --------------------------------------------------------------------------- - -function linkClickWillUnloadCurrentPage(event, link) { - return !( - event.type != "click" || - link.target == "_blank" || - event.metaKey || - event.ctrlKey || - event.shiftKey || - event.altKey || - event.which > 1 - ); -} - -class OutboundLinkTracker { - constructor(tracker, opts) { - trackUsage(tracker, pluginIds.OUTBOUND_LINK_TRACKER); - - if (!window.addEventListener) { - return; - } - - const defaultOpts = { - events: ["click"], - linkSelector: "a, area", - shouldTrackOutboundLink: this.shouldTrackOutboundLink, - fieldsObj: {}, - attributePrefix: "ga-", - }; - - this.opts = Object.assign(defaultOpts, opts); - this.tracker = tracker; - this.handleLinkInteractions = this.handleLinkInteractions.bind(this); - - this.delegates = {}; - this.opts.events.forEach((event) => { - this.delegates[event] = delegate( - document, - event, - this.opts.linkSelector, - this.handleLinkInteractions, - { composed: true, useCapture: true } - ); - }); - } - - handleLinkInteractions(event, link) { - if (this.opts.shouldTrackOutboundLink(link, parseUrl)) { - const href = link.getAttribute("href") || link.getAttribute("xlink:href"); - const url = parseUrl(href); - - const defaultFields = { - transport: "beacon", - eventCategory: "Outbound Link", - eventAction: event.type, - eventLabel: url.href, - }; - - const userFields = Object.assign( - {}, - this.opts.fieldsObj, - getAttributeFields(link, this.opts.attributePrefix) - ); - - const fieldsObj = createFieldsObj( - defaultFields, - userFields, - this.tracker, - this.opts.hitFilter, - link, - event - ); - - if ( - !navigator.sendBeacon && - linkClickWillUnloadCurrentPage(event, link) - ) { - const clickHandler = () => { - window.removeEventListener("click", clickHandler); - - if (!event.defaultPrevented) { - event.preventDefault(); - - const oldHitCallback = fieldsObj.hitCallback; - fieldsObj.hitCallback = withTimeout(function () { - if (typeof oldHitCallback == "function") { - oldHitCallback(); - } - location.href = href; - }); - } - this.tracker.send("event", fieldsObj); - }; - window.addEventListener("click", clickHandler); - } else { - this.tracker.send("event", fieldsObj); - } - } - } - - shouldTrackOutboundLink(link, parseUrlFn) { - const href = link.getAttribute("href") || link.getAttribute("xlink:href"); - const url = parseUrlFn(href); - return ( - url.hostname != location.hostname && url.protocol.slice(0, 4) == "http" - ); - } - - remove() { - Object.keys(this.delegates).forEach((key) => { - this.delegates[key].destroy(); - }); - } -} - -provide("outboundLinkTracker", OutboundLinkTracker); - -// --------------------------------------------------------------------------- -// Plugin: urlChangeTracker -// --------------------------------------------------------------------------- - -function getPath() { - return location.pathname + location.search; -} - -class UrlChangeTracker { - constructor(tracker, opts) { - trackUsage(tracker, pluginIds.URL_CHANGE_TRACKER); - - if (!history.pushState || !window.addEventListener) { - return; - } - - const defaultOpts = { - shouldTrackUrlChange: this.shouldTrackUrlChange, - trackReplaceState: false, - fieldsObj: {}, - hitFilter: null, - }; - - this.opts = Object.assign(defaultOpts, opts); - this.tracker = tracker; - this.path = getPath(); - - this.pushStateOverride = this.pushStateOverride.bind(this); - this.replaceStateOverride = this.replaceStateOverride.bind(this); - this.handlePopState = this.handlePopState.bind(this); - - MethodChain.add(history, "pushState", this.pushStateOverride); - MethodChain.add(history, "replaceState", this.replaceStateOverride); - window.addEventListener("popstate", this.handlePopState); - } - - pushStateOverride(originalMethod) { - return (...args) => { - originalMethod(...args); - this.handleUrlChange(true); - }; - } - - replaceStateOverride(originalMethod) { - return (...args) => { - originalMethod(...args); - this.handleUrlChange(false); - }; - } - - handlePopState() { - this.handleUrlChange(true); - } - - handleUrlChange(historyDidUpdate) { - setTimeout(() => { - const oldPath = this.path; - const newPath = getPath(); - - if ( - oldPath != newPath && - this.opts.shouldTrackUrlChange.call(this, newPath, oldPath) - ) { - this.path = newPath; - this.tracker.set({ - page: newPath, - title: document.title, - }); - - if (historyDidUpdate || this.opts.trackReplaceState) { - const defaultFields = { transport: "beacon" }; - this.tracker.send( - "pageview", - createFieldsObj( - defaultFields, - this.opts.fieldsObj, - this.tracker, - this.opts.hitFilter - ) - ); - } - } - }, 0); - } - - shouldTrackUrlChange(newPath, oldPath) { - return !!(newPath && oldPath); - } - - remove() { - MethodChain.remove(history, "pushState", this.pushStateOverride); - MethodChain.remove(history, "replaceState", this.replaceStateOverride); - window.removeEventListener("popstate", this.handlePopState); - } -} - -provide("urlChangeTracker", UrlChangeTracker); diff --git a/package.json b/package.json index fbb9c0472f..570beaffc6 100644 --- a/package.json +++ b/package.json @@ -276,7 +276,6 @@ "@types/formidable": "^3.5.1", "@types/fs-extra": "^11.0.4", "@types/fuzzy-search": "^2.1.5", - "@types/google.analytics": "^0.0.46", "@types/invariant": "^2.2.37", "@types/ioredis-mock": "^8.2.8", "@types/js-yaml": "^4.0.9", diff --git a/server/env.ts b/server/env.ts index 59a398672d..20b2df7bbd 100644 --- a/server/env.ts +++ b/server/env.ts @@ -529,7 +529,7 @@ export class Environment { public RELEASE = this.toOptionalString(environment.RELEASE); /** - * A Google Analytics tracking ID, supports v3 or v4 properties. + * A Google Analytics 4 measurement ID. */ @Public @IsOptional() diff --git a/yarn.lock b/yarn.lock index d7bc1bc862..aba2812290 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6311,13 +6311,6 @@ __metadata: languageName: node linkType: hard -"@types/google.analytics@npm:^0.0.46": - version: 0.0.46 - resolution: "@types/google.analytics@npm:0.0.46" - checksum: 10c0/1d763c3e99711eb24b2723195aef398ee1bdbbd32d02e28d493707706f6c0b7bd2a9c23903f5ddce887b8c4cf202c507e78cf77f7ea229b0cba04667cc8cce51 - languageName: node - linkType: hard - "@types/hast@npm:^3.0.0": version: 3.0.5 resolution: "@types/hast@npm:3.0.5" @@ -14023,7 +14016,6 @@ __metadata: "@types/formidable": "npm:^3.5.1" "@types/fs-extra": "npm:^11.0.4" "@types/fuzzy-search": "npm:^2.1.5" - "@types/google.analytics": "npm:^0.0.46" "@types/invariant": "npm:^2.2.37" "@types/ioredis-mock": "npm:^8.2.8" "@types/js-yaml": "npm:^4.0.9"