2022-10-14 09:54:15 +02:00
|
|
|
import { useCallback, useContext } from 'react';
|
2022-10-10 14:06:44 +02:00
|
|
|
import { PlausibleContext } from 'contexts/PlausibleContext';
|
|
|
|
import { EventOptions, PlausibleOptions } from 'plausible-tracker';
|
2022-05-06 14:04:09 +02:00
|
|
|
|
2022-10-10 14:06:44 +02:00
|
|
|
/**
|
|
|
|
* 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'`
|
|
|
|
**/
|
2023-02-28 14:29:14 +01:00
|
|
|
export type CustomEvents =
|
2022-12-01 12:25:18 +01:00
|
|
|
| 'invite'
|
|
|
|
| 'upgrade_plan_clicked'
|
|
|
|
| 'change_request'
|
2022-12-21 15:16:15 +01:00
|
|
|
| 'favorite'
|
2022-12-22 16:16:51 +01:00
|
|
|
| 'maintenance'
|
2023-01-09 14:36:10 +01:00
|
|
|
| 'message_banner'
|
2023-01-10 11:33:56 +01:00
|
|
|
| 'hidden_environment'
|
2023-02-17 13:10:27 +01:00
|
|
|
| 'project_overview'
|
2023-02-17 09:38:00 +01:00
|
|
|
| 'suggest_tags'
|
2023-02-17 15:02:07 +01:00
|
|
|
| 'unknown_ui_error'
|
2023-02-21 12:46:29 +01:00
|
|
|
| 'export_import'
|
2023-02-28 14:29:14 +01:00
|
|
|
| 'project_api_tokens'
|
2023-03-10 11:28:02 +01:00
|
|
|
| 'project_stickiness_set'
|
2023-03-22 12:46:16 +01:00
|
|
|
| 'notifications'
|
2023-04-19 19:27:23 +02:00
|
|
|
| 'batch_operations'
|
2023-04-27 15:12:02 +02:00
|
|
|
| 'strategyTitle'
|
2023-05-23 09:16:09 +02:00
|
|
|
| 'strategyImprovements'
|
2023-05-19 16:32:08 +02:00
|
|
|
| 'default_strategy'
|
2023-05-19 12:47:55 +02:00
|
|
|
| 'demo'
|
2023-05-19 16:32:08 +02:00
|
|
|
| 'demo-start'
|
|
|
|
| 'demo-close'
|
|
|
|
| 'demo-finish'
|
|
|
|
| 'demo-see-plans'
|
|
|
|
| 'demo-see-plan'
|
|
|
|
| 'demo-restart'
|
|
|
|
| 'demo-view-demo-link'
|
|
|
|
| 'demo-start-topic'
|
|
|
|
| 'demo-ask-questions'
|
|
|
|
| 'demo-open-demo-web';
|
2022-05-06 14:04:09 +02:00
|
|
|
|
|
|
|
export const usePlausibleTracker = () => {
|
2022-10-10 14:06:44 +02:00
|
|
|
const plausible = useContext(PlausibleContext);
|
2022-05-06 14:04:09 +02:00
|
|
|
|
2022-10-10 14:06:44 +02:00
|
|
|
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();
|
|
|
|
}
|
2022-05-06 14:04:09 +02:00
|
|
|
}
|
2022-10-10 14:06:44 +02:00
|
|
|
},
|
|
|
|
[plausible]
|
|
|
|
);
|
2022-05-06 14:04:09 +02:00
|
|
|
|
2022-10-10 14:06:44 +02:00
|
|
|
return { trackEvent };
|
2022-05-06 14:04:09 +02:00
|
|
|
};
|