UNPKG

@segment/analytics-next

Version:

Analytics Next (aka Analytics 2.0) is the latest version of Segment’s JavaScript SDK - enabling you to send your data to any tool without having to learn, test, or use a new API every time.

107 lines (92 loc) 2.79 kB
/* eslint-disable @typescript-eslint/no-floating-promises */ import { getCDN, setGlobalCDNUrl } from '../lib/parse-cdn' import { setVersionType } from '../lib/version-type' if (process.env.IS_WEBPACK_BUILD) { if (process.env.ASSET_PATH) { // @ts-ignore __webpack_public_path__ = process.env.ASSET_PATH } else { const cdn = getCDN() setGlobalCDNUrl(cdn) // @ts-ignore __webpack_public_path__ = cdn ? cdn + '/analytics-next/bundles/' : 'https://cdn.segment.com/analytics-next/bundles/' } } setVersionType('web') import { install } from './standalone-analytics' import '../lib/csp-detection' import { shouldPolyfill } from '../lib/browser-polyfill' import { RemoteMetrics } from '../core/stats/remote-metrics' import { embeddedWriteKey } from '../lib/embedded-write-key' import { loadAjsClassicFallback, isAnalyticsCSPError, } from '../lib/csp-detection' import { setGlobalAnalyticsKey } from '../lib/global-analytics-helper' let ajsIdentifiedCSP = false const sendErrorMetrics = (tags: string[]) => { // this should not be instantied at the root, or it will break ie11. const metrics = new RemoteMetrics() metrics.increment('analytics_js.invoke.error', [ ...tags, `wk:${embeddedWriteKey()}`, ]) } function onError(err?: unknown) { console.error('[analytics.js]', 'Failed to load Analytics.js', err) sendErrorMetrics([ 'type:initialization', ...(err instanceof Error ? [`message:${err?.message}`, `name:${err?.name}`] : []), ]) } document.addEventListener('securitypolicyviolation', (e) => { if (ajsIdentifiedCSP || !isAnalyticsCSPError(e)) { return } ajsIdentifiedCSP = true sendErrorMetrics(['type:csp']) loadAjsClassicFallback().catch(console.error) }) /** * Attempts to run a promise and catch both sync and async errors. **/ async function attempt<T>(promise: () => Promise<T>) { try { const result = await promise() return result } catch (err) { onError(err) } } const globalAnalyticsKey = ( document.querySelector( 'script[data-global-segment-analytics-key]' ) as HTMLScriptElement )?.dataset.globalSegmentAnalyticsKey if (globalAnalyticsKey) { setGlobalAnalyticsKey(globalAnalyticsKey) } if (shouldPolyfill()) { // load polyfills in order to get AJS to work with old browsers const script = document.createElement('script') script.setAttribute( 'src', 'https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.7.0/polyfill.min.js' ) if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', () => document.body.appendChild(script) ) } else { document.body.appendChild(script) } script.onload = function (): void { attempt(install) } } else { attempt(install) }