it-tools/src/plugins/plausible.plugin.ts

39 lines
833 B
TypeScript
Raw Normal View History

import { noop } from 'lodash';
import Plausible from 'plausible-tracker';
2022-04-16 11:51:20 +02:00
import type { App } from 'vue';
import { config } from '@/config';
2022-04-16 11:51:20 +02:00
function createFakePlausibleInstance(): Pick<ReturnType<typeof Plausible>, 'trackEvent' | 'enableAutoPageviews'> {
return {
trackEvent: noop,
enableAutoPageviews: () => noop,
};
}
function createPlausibleInstance({
config,
}: {
config: {
isTrackerEnabled: boolean
domain: string
apiHost: string
trackLocalhost: boolean
}
}) {
if (config.isTrackerEnabled) {
return Plausible(config);
}
return createFakePlausibleInstance();
}
2022-04-16 11:51:20 +02:00
export const plausible = {
install: (app: App) => {
const plausible = createPlausibleInstance({ config: config.plausible });
2022-04-16 11:51:20 +02:00
plausible.enableAutoPageviews();
app.provide('plausible', plausible);
2022-04-16 11:51:20 +02:00
},
};