2023-04-04 18:59:49 +02:00
|
|
|
import { noop } from 'lodash';
|
2022-12-21 21:02:57 +01:00
|
|
|
|
2022-05-24 00:07:54 +02:00
|
|
|
import Plausible from 'plausible-tracker';
|
2022-04-16 11:51:20 +02:00
|
|
|
import type { App } from 'vue';
|
2023-05-28 23:13:24 +02:00
|
|
|
import { config } from '@/config';
|
2022-04-16 11:51:20 +02:00
|
|
|
|
2023-04-04 18:59:49 +02:00
|
|
|
function createFakePlausibleInstance(): Pick<ReturnType<typeof Plausible>, 'trackEvent' | 'enableAutoPageviews'> {
|
|
|
|
return {
|
|
|
|
trackEvent: noop,
|
|
|
|
enableAutoPageviews: () => noop,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function createPlausibleInstance({
|
|
|
|
config,
|
|
|
|
}: {
|
|
|
|
config: {
|
2023-05-28 23:13:24 +02:00
|
|
|
isTrackerEnabled: boolean
|
|
|
|
domain: string
|
|
|
|
apiHost: string
|
|
|
|
trackLocalhost: boolean
|
|
|
|
}
|
2023-04-04 18:59:49 +02:00
|
|
|
}) {
|
|
|
|
if (config.isTrackerEnabled) {
|
|
|
|
return Plausible(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
return createFakePlausibleInstance();
|
|
|
|
}
|
|
|
|
|
2022-04-16 11:51:20 +02:00
|
|
|
export const plausible = {
|
|
|
|
install: (app: App) => {
|
2023-04-04 18:59:49 +02:00
|
|
|
const plausible = createPlausibleInstance({ config: config.plausible });
|
2022-04-16 11:51:20 +02:00
|
|
|
plausible.enableAutoPageviews();
|
|
|
|
|
2022-12-21 21:02:57 +01:00
|
|
|
app.provide('plausible', plausible);
|
2022-04-16 11:51:20 +02:00
|
|
|
},
|
|
|
|
};
|