2022-12-21 21:02:57 +01:00
|
|
|
import _ from 'lodash';
|
2022-12-21 00:03:31 +01:00
|
|
|
import type Plausible from 'plausible-tracker';
|
2022-12-21 21:02:57 +01:00
|
|
|
import { inject } from 'vue';
|
2022-12-21 00:03:31 +01:00
|
|
|
|
2022-12-21 21:02:57 +01:00
|
|
|
export { createTrackerService, useTracker };
|
2022-12-21 00:03:31 +01:00
|
|
|
|
|
|
|
function createTrackerService({ plausible }: { plausible: ReturnType<typeof Plausible> }) {
|
|
|
|
return {
|
|
|
|
trackEvent({ eventName }: { eventName: string }) {
|
|
|
|
plausible.trackEvent(eventName);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-12-21 21:02:57 +01:00
|
|
|
|
|
|
|
function useTracker() {
|
|
|
|
const plausible: ReturnType<typeof Plausible> | undefined = inject('plausible');
|
|
|
|
|
|
|
|
if (_.isNil(plausible)) {
|
2023-05-28 23:13:24 +02:00
|
|
|
throw new TypeError('Plausible must be instantiated');
|
2022-12-21 21:02:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const tracker = createTrackerService({ plausible });
|
|
|
|
|
|
|
|
return {
|
|
|
|
tracker,
|
|
|
|
};
|
|
|
|
}
|