Skip to content

Commit

Permalink
specify analytics namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
SuaYoo committed Dec 23, 2024
1 parent f207b2e commit b27218d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions frontend/sample.env.local
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ E2E_USER_EMAIL=
E2E_USER_PASSWORD=
GLITCHTIP_DSN=
EXTRA_SCRIPT_URL=
ANALYTICS_NAMESPACE=
26 changes: 16 additions & 10 deletions frontend/src/utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -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]

Check warning on line 15 in frontend/src/utils/analytics.ts

View workflow job for this annotation

GitHub Actions / test

This assertion is unnecessary since it does not change the type of the expression

Check failure on line 15 in frontend/src/utils/analytics.ts

View workflow job for this annotation

GitHub Actions / setup-and-build

Unexpected any. Specify a different type

Check warning on line 15 in frontend/src/utils/analytics.ts

View workflow job for this annotation

GitHub Actions / setup-and-build

This assertion is unnecessary since it does not change the type of the expression
: null;

console.log("analytics:", analytics);

export enum TrackEvent {
ViewPublicCollection = "View Public Collection",
Expand All @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit b27218d

Please sign in to comment.