diff --git a/frontend/sample.env.local b/frontend/sample.env.local index 0fe4239b85..62448cd7c2 100644 --- a/frontend/sample.env.local +++ b/frontend/sample.env.local @@ -4,3 +4,4 @@ E2E_USER_EMAIL= E2E_USER_PASSWORD= GLITCHTIP_DSN= EXTRA_SCRIPT_URL= +ANALYTICS_NAMESPACE= diff --git a/frontend/src/utils/analytics.ts b/frontend/src/utils/analytics.ts index 498f96e8b6..73310ddb96 100644 --- a/frontend/src/utils/analytics.ts +++ b/frontend/src/utils/analytics.ts @@ -1,15 +1,21 @@ /** * Custom tracking for analytics. * - * `window.analytics` should be made available through the `extra.js` injected by the server. + * Any third-party analytics script will need to have been made + * available through the `extra.js` injected by the server. */ -declare global { - // eslint-disable-next-line no-var - var analytics: ( - event: string, - opts: { props?: { [key: string]: unknown } }, - ) => {}; -} + +// type Track = ( +// event: string, +// opts: { props?: { [key: string]: unknown } }, +// ) => {}; + +// ANALYTICS_NAMESPACE is specified with webpack `DefinePlugin` +const analytics = window.process.env.ANALYTICS_NAMESPACE + ? (window as any)[window.process.env.ANALYTICS_NAMESPACE as string] + : null; + +console.log("analytics:", analytics); export enum TrackEvent { ViewPublicCollection = "View Public Collection", @@ -18,12 +24,12 @@ export enum TrackEvent { } export function track(event: TrackEvent, props?: { [key: string]: unknown }) { - if (!(window.analytics as unknown)) { + if (!analytics) { return; } try { - window.analytics(event, { props }); + analytics(event, { props }); } catch (err) { console.debug(err); } diff --git a/frontend/webpack.config.js b/frontend/webpack.config.js index 93fd63b6cc..021d68fe12 100644 --- a/frontend/webpack.config.js +++ b/frontend/webpack.config.js @@ -166,6 +166,9 @@ const main = { new webpack.DefinePlugin({ "window.process.env.WEBSOCKET_HOST": JSON.stringify(WEBSOCKET_HOST), + "window.process.env.ANALYTICS_NAMESPACE": JSON.stringify( + process.env.ANALYTICS_NAMESPACE, + ), }), new webpack.optimize.LimitChunkCountPlugin({