let __DEBUG__ = process.env.NODE_ENV !== 'production' export function updateDebug(debug: boolean) { __DEBUG__ = debug } export function removeElement($el: HTMLElement) { $el.parentNode?.removeChild($el) } export const isObject = (item: any) => { return ( item && typeof item === 'object' && !Array.isArray(item) && item !== null ) } export const warn = (text: string) => { if (!__DEBUG__) { return } console.warn(`[mixgather] ${text}`) } export const isBrowser = () => { if (typeof window === 'undefined' || typeof document === 'undefined') { return false } return true } export const mergeDeep = (target: T, ...sources: any): T => { if (!sources.length) { return target } const source = sources.shift() if (!isObject(target) || !isObject(source)) { return target } for (const key in source) { if (isObject(source[key])) { if (!target[key]) { Object.assign(target, { [key]: {} }) } mergeDeep(target[key], source[key]) } else { Object.assign(target, { [key]: source[key] }) } } return mergeDeep(target, ...sources) } export const addScriptLoadListener = ( scriptElement: HTMLScriptElement ): Promise => { const listener = new Promise((resolve) => { const callback = () => { scriptElement.removeEventListener('load', callback) resolve() } scriptElement?.addEventListener('load', callback) }) const timeout = new Promise((_, reject) => { setTimeout(() => { reject('[mixgather] Failed to load Script.') }, 5000) }) return Promise.race([listener, timeout]) } export function loadGTag(id: string) { const url = 'https://www.googletagmanager.com/gtag/js' const preconnectOrigin = 'https://www.googletagmanager.com' const createScript = (): HTMLScriptElement => { const head = document.head || document.getElementsByTagName('head')[0] const script = document.createElement('script') script.async = true script.src = `${url}?id=${id}&l=dataLayer` if (preconnectOrigin) { const link = document.createElement('link') link.href = preconnectOrigin link.rel = 'preconnect' head.appendChild(link) } head.appendChild(script) return script } return addScriptLoadListener(createScript()) } // @TODO: beautify the snippet /* eslint-disable */ export function loadMixpanel() { const script = (function (c, a) { if (!a.__SV) { let b: any = window try { let d, m, j, k = b.location, f = k.hash d = function (a: any, b: any) { return (m = a.match(RegExp(b + '=([^&]*)'))) ? m[1] : null } f && d(f, 'state') && ((j = JSON.parse(decodeURIComponent(d(f, 'state')))), 'mpeditor' === j.action && (b.sessionStorage.setItem('_mpcehash', f), history.replaceState( j.desiredHash || '', c.title, k.pathname + k.search ))) } catch (n) {} let l, h window['mixpanel'] = a a._i = [] a.init = function (b: any, d: any, g: any) { function c(b: any, i: any) { const a = i.split('.') 2 == a.length && ((b = b[a[0]]), (i = a[1])) b[i] = function () { b.push([i].concat(Array.prototype.slice.call(arguments, 0))) } } let e = a 'undefined' !== typeof g ? (e = a[g] = []) : (g = 'mixpanel') e.people = e.people || [] e.toString = function (b: any) { let a = 'mixpanel' 'mixpanel' !== g && (a += '.' + g) b || (a += ' (stub)') return a } e.people.toString = function () { return e.toString(1) + '.people (stub)' } l = 'disable time_event track track_pageview track_links track_forms track_with_groups add_group set_group remove_group register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.remove'.split( ' ' ) for (h = 0; h < l.length; h++) c(e, l[h]) const f = 'set set_once union unset remove delete'.split(' ') e.get_group = function () { function a(c: any) { b[c] = function () { const call2_args = arguments const call2 = [c].concat( Array.prototype.slice.call(call2_args, 0) ) e.push([d, call2]) } } for ( let b = {}, d = ['get_group'].concat( Array.prototype.slice.call(arguments, 0) ), c = 0; c < f.length; c++ ) a(f[c]) return b } a._i.push([b, d, g]) } a.__SV = 2 b = c.createElement('script') as HTMLScriptElement b.type = 'text/javascript' b.async = true b.src = window['customScriptSrc'] ? window['customScriptSrc'] : 'undefined' !== typeof window['MIXPANEL_CUSTOM_LIB_URL'] ? window['MIXPANEL_CUSTOM_LIB_URL'] : 'file:' === c.location.protocol && '//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js'.match(/^\/\//) ? 'https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js' : '//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js' const head = document.head || document.getElementsByTagName('head')[0] head.appendChild(b) return b } })(document, window['mixpanel'] || []) return addScriptLoadListener(script) } /* eslint-enable */