-
-
Notifications
You must be signed in to change notification settings - Fork 656
/
usePlausibleTracker.ts
45 lines (41 loc) · 1.32 KB
/
usePlausibleTracker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { useCallback, useContext } from 'react';
import { PlausibleContext } from 'contexts/PlausibleContext';
import { EventOptions, PlausibleOptions } from 'plausible-tracker';
/**
* Allowed event names. Makes it easy to remove, since TS will complain.
* Add those to Plausible as Custom event goals.
* @see https://plausible.io/docs/custom-event-goals#2-create-a-custom-event-goal-in-your-plausible-analytics-account
* @example `'download | 'invite' | 'signup'`
**/
type CustomEvents =
| 'invite'
| 'upgrade_plan_clicked'
| 'change_request'
| 'favorite'
| 'maintenance'
| 'message_banner'
| 'hidden_environment'
| 'project_overview'
| 'suggest_tags'
| 'unknown_ui_error'
| 'export_import';
export const usePlausibleTracker = () => {
const plausible = useContext(PlausibleContext);
const trackEvent = useCallback(
(
eventName: CustomEvents,
options?: EventOptions | undefined,
eventData?: PlausibleOptions | undefined
) => {
if (plausible?.trackEvent) {
plausible.trackEvent(eventName, options, eventData);
} else {
if (options?.callback) {
options.callback();
}
}
},
[plausible]
);
return { trackEvent };
};