import { AnalyticsBrowser } from '.' import { embeddedWriteKey } from '../lib/embedded-write-key' import { AnalyticsSnippet } from './standalone-interface' import { getGlobalAnalytics, setGlobalAnalytics, } from '../lib/global-analytics-helper' import { Analytics } from 'index' import { logger } from '../plugins/logger' function getWriteKey(): string | undefined { if (embeddedWriteKey()) { return embeddedWriteKey() } const analytics = getGlobalAnalytics() if (analytics?._writeKey) { return analytics._writeKey } const regex = /http.*\/analytics\.js\/v1\/([^/]*)(\/platform)?\/analytics.*/ const scripts = Array.prototype.slice.call( document.querySelectorAll('script') ) let writeKey: string | undefined = undefined for (const s of scripts) { const src = s.getAttribute('src') ?? '' const result = regex.exec(src) if (result && result[1]) { writeKey = result[1] break } } if (!writeKey && document.currentScript) { const script = document.currentScript as HTMLScriptElement const src = script.src const result = regex.exec(src) if (result && result[1]) { writeKey = result[1] } } return writeKey } export async function install(): Promise { const writeKey = getWriteKey() const options = getGlobalAnalytics()?._loadOptions ?? {} if (!writeKey) { console.error( 'Failed to load Write Key. Make sure to use the latest version of the Dreamdata snippet, which can be found in your source settings.' ) return } setGlobalAnalytics( (await AnalyticsBrowser.standalone(writeKey, options)) as AnalyticsSnippet ) // Clear cookie-less namespace if that has already loaded if ('dreamdata-cl' in window) { logger.debug( 'Clearing Dreamdata cookie-less as regular script has now been initialized' ) delete window['dreamdata-cl'] } } declare global { interface Window { dreamdata?: Analytics 'dreamdata-cl'?: Analytics } } export async function installCookieless(): Promise { if (window.dreamdata && window.dreamdata.initialized) { logger.debug( 'Skipping initializing Dreamdata cookie-less as regular script has already been initialized' ) return } if (!('dreamdata-cl' in window)) { logger.debug( 'Skipping initializing Dreamdata cookie-less as window object no longer exists' ) return } const writeKey = getWriteKey() const options = getGlobalAnalytics()?._loadOptions ?? {} options.anonymous = true if (!writeKey) { console.error( 'Failed to load Write Key. Make sure to use the latest version of the Dreamdata snippet, which can be found in your source settings.' ) return } setGlobalAnalytics( (await AnalyticsBrowser.standalone(writeKey, options)) as AnalyticsSnippet ) }