refactor(tracker): better tracker injection

This commit is contained in:
Corentin Thomasset 2022-12-21 21:02:57 +01:00
parent bf88836dbe
commit def60e7248
No known key found for this signature in database
GPG key ID: DBD997E935996158
6 changed files with 67 additions and 37 deletions

View file

@ -1,6 +1,8 @@
import _ from 'lodash';
import type Plausible from 'plausible-tracker';
import { inject } from 'vue';
export { createTrackerService };
export { createTrackerService, useTracker };
function createTrackerService({ plausible }: { plausible: ReturnType<typeof Plausible> }) {
return {
@ -9,3 +11,17 @@ function createTrackerService({ plausible }: { plausible: ReturnType<typeof Plau
},
};
}
function useTracker() {
const plausible: ReturnType<typeof Plausible> | undefined = inject('plausible');
if (_.isNil(plausible)) {
throw new Error('Plausible must be instantiated');
}
const tracker = createTrackerService({ plausible });
return {
tracker,
};
}