mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-01 00:08:27 +01:00
c0ec6f20b2
## About the changes ![image](https://user-images.githubusercontent.com/2625371/219385549-2c77ddbe-d5a3-428c-878d-5e76a536d43b.png) https://linear.app/unleash/issue/1-694/widgets-explanation-plausible-buttons
45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
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 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 };
|
|
};
|