Skip to content

Commit 747f67c

Browse files
emma-sgSuaYoo
authored andcommitted
remove ANALYTICS_NAMESPACE as it's only usable at build time
and replace it with a runtime window.btrixEvent set that can be set in the extra.js config
1 parent 12f358b commit 747f67c

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

frontend/config/define.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,4 @@ const WEBSOCKET_HOST =
2222

2323
module.exports = {
2424
"window.process.env.WEBSOCKET_HOST": JSON.stringify(WEBSOCKET_HOST),
25-
"window.process.env.ANALYTICS_NAMESPACE": JSON.stringify(
26-
process.env.ANALYTICS_NAMESPACE || "",
27-
),
2825
};

frontend/docs/docs/deploy/customization.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,21 @@ inject_extra: >
227227
```
228228

229229
Note that the script will only run when the web app loads, i.e. the first time the app is loaded in the browser and on hard refresh. The script will not run again upon clicking a link in the web app. This shouldn't be an issue with most analytics libraries, which should listen for changes to [window history](https://developer.mozilla.org/en-US/docs/Web/API/History). If you have a custom script that needs to re-run when the frontend URL changes, you'll need to add an event listener for the [`popstate` event](https://developer.mozilla.org/en-US/docs/Web/API/Window/popstate_event).
230+
231+
### Tracking events
232+
233+
Certain events, such as public collection views, link copies, and downloads, can be optionally tracked by setting `window.btrixEvent` in your `inject_extra` config. This should be a function that conforms to the following type:
234+
235+
```ts
236+
type btrixEvent = (
237+
event: string,
238+
extra?: {
239+
props?: {
240+
org_slug: string | null;
241+
collection_id?: string | null;
242+
collection_name?: string | null;
243+
logged_in?: boolean;
244+
};
245+
},
246+
) => void;
247+
```

frontend/sample.env.local

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@ E2E_USER_EMAIL=
44
E2E_USER_PASSWORD=
55
GLITCHTIP_DSN=
66
INJECT_EXTRA=
7-
ANALYTICS_NAMESPACE=

frontend/src/utils/analytics.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,25 @@ export type AnalyticsTrackProps = {
1414
logged_in?: boolean;
1515
};
1616

17+
declare global {
18+
interface Window {
19+
btrixEvent?: (
20+
event: string,
21+
extra?: { props?: AnalyticsTrackProps },
22+
) => void;
23+
}
24+
}
25+
1726
export function track(
1827
event: `${AnalyticsTrackEvent}`,
1928
props?: AnalyticsTrackProps,
2029
) {
21-
// ANALYTICS_NAMESPACE is specified with webpack `DefinePlugin`
22-
const analytics = window.process.env.ANALYTICS_NAMESPACE
23-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
24-
(window as any)[window.process.env.ANALYTICS_NAMESPACE]
25-
: null;
26-
27-
if (!analytics) {
30+
if (!window.btrixEvent) {
2831
return;
2932
}
3033

3134
try {
32-
analytics(event, { props });
35+
window.btrixEvent(event, { props });
3336
} catch (err) {
3437
console.debug(err);
3538
}

0 commit comments

Comments
 (0)