From 1a6af8fc41e78de0d6029761704da1dcd1b2e211 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Sun, 28 Aug 2022 15:55:18 -0700 Subject: [PATCH] Update tracker to use no dependencies. --- tracker/index.js | 61 +++++++++++++++++++++++++++++++----------------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/tracker/index.js b/tracker/index.js index 17ee467d..1eb54d63 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -1,6 +1,3 @@ -import { doNotTrack, hook } from '../lib/web'; -import { removeTrailingSlash } from '../lib/url'; - (window => { const { screen: { width, height }, @@ -15,6 +12,41 @@ import { removeTrailingSlash } from '../lib/url'; if (!currentScript) return; + const assign = (a, b) => { + Object.keys(b).forEach(key => { + if (b[key] !== undefined) a[key] = b[key]; + }); + return a; + }; + + const hook = (_this, method, callback) => { + const orig = _this[method]; + + return (...args) => { + callback.apply(null, args); + + return orig.apply(_this, args); + }; + }; + + const doNotTrack = () => { + const { doNotTrack, navigator, external } = window; + + const msTrackProtection = 'msTrackingProtectionEnabled'; + const msTracking = () => { + return external && msTrackProtection in external && external[msTrackProtection](); + }; + + const dnt = doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || msTracking(); + + return dnt == '1' || dnt === 'yes'; + }; + + const trackingDisabled = () => + (localStorage && localStorage.getItem('umami.disabled')) || + (dnt && doNotTrack()) || + (domain && !domains.includes(hostname)); + const _data = 'data-'; const _false = 'false'; const attr = currentScript.getAttribute.bind(currentScript); @@ -25,21 +57,15 @@ import { removeTrailingSlash } from '../lib/url'; const cssEvents = attr(_data + 'css-events') !== _false; const domain = attr(_data + 'domains') || ''; const domains = domain.split(',').map(n => n.trim()); - - const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/; - const eventSelect = "[class*='umami--']"; - - const trackingDisabled = () => - (localStorage && localStorage.getItem('umami.disabled')) || - (dnt && doNotTrack()) || - (domain && !domains.includes(hostname)); - const root = hostUrl - ? removeTrailingSlash(hostUrl) + ? hostUrl.replace(/\/$/, '') : currentScript.src.split('/').slice(0, -1).join('/'); const endpoint = `${root}/api/collect`; const screen = `${width}x${height}`; - const listeners = {}; + const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/; + const eventSelect = "[class*='umami--']"; + + let listeners = {}; let currentUrl = `${pathname}${search}`; let currentRef = document.referrer; let cache; @@ -54,13 +80,6 @@ import { removeTrailingSlash } from '../lib/url'; url: currentUrl, }); - const assign = (a, b) => { - Object.keys(b).forEach(key => { - if (b[key] !== undefined) a[key] = b[key]; - }); - return a; - }; - const collect = (type, payload) => { if (trackingDisabled()) return;