{"version":3,"file":"tracker.js","sources":["../../../../../Flow.Showcase.Static.Bundler/Root/entrypoints/tracker.ts"],"sourcesContent":["// @ts-nocheck\n\n(window => {\n const {\n screen: { width, height },\n navigator: { language },\n location,\n document,\n history,\n } = window;\n const { pathname, search } = location;\n const { currentScript } = document;\n\n if (!currentScript) return;\n\n const _data = 'data-';\n const attr = currentScript.getAttribute.bind(currentScript);\n const dnt = attr(_data + 'do-not-track');\n const endpoint = `/tracker`;\n const screen = `${width}x${height}`;\n const eventRegex = /data-track-event-([\\w-_]+)/;\n const eventNameAttribute = _data + 'track-event';\n const delayDuration = 300;\n\n /* Helper functions */\n\n const hook = (_this, method, callback) => {\n const orig = _this[method];\n\n return (...args) => {\n callback.apply(null, args);\n\n return orig.apply(_this, args);\n };\n };\n\n const getPath = url => {\n try {\n return new URL(url).pathname;\n } catch (e) {\n return url;\n }\n };\n\n const getPayload = () => ({\n screen,\n language,\n title,\n url: currentUrl,\n referrer: currentRef,\n date: utcNow\n });\n\n /* Tracking functions */\n\n const doNotTrack = () => {\n const { doNotTrack, navigator } = window;\n const dnt = doNotTrack || navigator.doNotTrack;\n return dnt === '1' || dnt === 'yes';\n };\n\n const trackingDisabled = () => (dnt && doNotTrack());\n\n const handlePush = (state, title, url) => {\n if (!url) return;\n\n currentRef = currentUrl;\n currentUrl = getPath(url.toString());\n\n if (currentUrl !== currentRef) {\n setTimeout(track, delayDuration);\n }\n };\n\n const handleClick = () => {\n const trackElement = el => {\n const attr = el.getAttribute.bind(el);\n const eventName = attr(eventNameAttribute);\n\n if (eventName) {\n const eventData = {};\n\n el.getAttributeNames().forEach(name => {\n const match = name.match(eventRegex);\n\n if (match) {\n eventData[match[1]] = attr(name);\n }\n });\n\n return track(eventName, eventData);\n }\n return Promise.resolve();\n };\n\n const callback = e => {\n const findATagParent = (rootElem, maxSearchDepth) => {\n let currentElement = rootElem;\n for (let i = 0; i < maxSearchDepth; i++) {\n if (currentElement.tagName === 'A') {\n return currentElement;\n }\n currentElement = currentElement.parentElement;\n if (!currentElement) {\n return null;\n }\n }\n return null;\n };\n\n const el = e.target;\n const anchor = el.tagName === 'A' ? el : findATagParent(el, 10);\n\n if (anchor) {\n const { href, target } = anchor;\n const external =\n target === '_blank' ||\n e.ctrlKey ||\n e.shiftKey ||\n e.metaKey ||\n (e.button && e.button === 1);\n const eventName = anchor.getAttribute(eventNameAttribute);\n\n if (eventName && href) {\n if (!external) {\n e.preventDefault();\n }\n return trackElement(anchor).then(() => {\n if (!external) location.href = href;\n });\n }\n } else {\n trackElement(el);\n }\n };\n\n document.addEventListener('click', callback, true);\n };\n\n const observeTitle = () => {\n const callback = ([entry]) => {\n title = entry && entry.target ? entry.target.text : undefined;\n };\n\n const observer = new MutationObserver(callback);\n\n const node = document.querySelector('head > title');\n\n if (node) {\n observer.observe(node, {\n subtree: true,\n characterData: true,\n childList: true,\n });\n }\n };\n\n const send = (payload = 'event') => {\n if (trackingDisabled()) return;\n const headers = {\n 'Content-Type': 'application/json',\n };\n return fetch(endpoint, {\n method: 'POST',\n body: JSON.stringify(payload),\n headers,\n })\n .catch(() => {}); // no-op, gulp error\n };\n\n const track = (obj, data) => {\n if (typeof obj === 'string') {\n return send({\n ...getPayload(),\n name: obj,\n data: typeof data === 'object' ? data : undefined,\n });\n } else if (typeof obj === 'object') {\n return send(obj);\n } else if (typeof obj === 'function') {\n return send(obj(getPayload()));\n }\n return send(getPayload());\n };\n\n const identify = data => {\n return send({\n ...getPayload(),\n name: 'identify',\n data: data\n })\n };\n\n /* Start */\n\n if (!window.tracker) {\n window.tracker = {\n track,\n identify,\n };\n }\n\n let currentUrl = `${pathname}${search}`;\n let currentRef = document.referrer;\n let title = document.title;\n let initialized;\n let now = new Date();\n let utcNow = now.toISOString();\n\n if (!trackingDisabled()) {\n history.pushState = hook(history, 'pushState', handlePush);\n history.replaceState = hook(history, 'replaceState', handlePush);\n handleClick();\n observeTitle();\n\n const init = () => {\n if (document.readyState === 'complete' && !initialized) {\n track();\n initialized = true;\n }\n };\n\n document.addEventListener('readystatechange', init, true);\n\n init();\n }\n})(window);\n"],"names":["window","width","height","language","location","document","history","pathname","search","currentScript","_data","dnt","endpoint","screen","eventRegex","eventNameAttribute","delayDuration","hook","_this","method","callback","orig","args","getPath","url","getPayload","title","currentUrl","currentRef","utcNow","doNotTrack","navigator","trackingDisabled","handlePush","state","track","handleClick","trackElement","el","attr","eventName","eventData","name","match","findATagParent","rootElem","maxSearchDepth","currentElement","i","anchor","href","target","external","observeTitle","entry","observer","node","send","payload","headers","obj","data","identify","initialized","init"],"mappings":"CAECA,GAAU,CACD,KAAA,CACF,OAAQ,CAAE,MAAAC,EAAO,OAAAC,CAAO,EACxB,UAAW,CAAE,SAAAC,CAAS,EACtB,SAAAC,EACA,SAAAC,EACA,QAAAC,CAAA,EACAN,EACE,CAAE,SAAAO,EAAU,OAAAC,CAAA,EAAWJ,EACvB,CAAE,cAAAK,GAAkBJ,EAE1B,GAAI,CAACI,EAAe,OAEpB,MAAMC,EAAQ,QAERC,EADOF,EAAc,aAAa,KAAKA,CAAa,EACzCC,EAAQ,cAAc,EACjCE,EAAW,WACXC,EAAS,GAAGZ,CAAK,IAAIC,CAAM,GAC3BY,EAAa,6BACbC,EAAqBL,EAAQ,cAC7BM,EAAgB,IAIhBC,EAAO,CAACC,EAAOC,EAAQC,IAAa,CAChC,MAAAC,EAAOH,EAAMC,CAAM,EAEzB,MAAO,IAAIG,KACEF,EAAA,MAAM,KAAME,CAAI,EAElBD,EAAK,MAAMH,EAAOI,CAAI,EAErC,EAEMC,EAAiBC,GAAA,CACf,GAAA,CACO,OAAA,IAAI,IAAIA,CAAG,EAAE,cACZ,CACD,OAAAA,CAAA,CAEf,EAEMC,EAAa,KAAO,CACtB,OAAAZ,EACA,SAAAV,EACA,MAAAuB,EACA,IAAKC,EACL,SAAUC,EACV,KAAMC,CAAA,GAKJC,EAAa,IAAM,CACrB,KAAM,CAAE,WAAAA,EAAY,UAAAC,CAAc/B,EAAAA,EAC5BW,EAA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