aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/plausible.plugin.ts
blob: 50a694dba38ce11cbcd413e9d32dbba09be5cc0a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { config } from '@/config';
import { noop } from 'lodash';

import Plausible from 'plausible-tracker';
import type { App } from 'vue';

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();
}

export const plausible = {
  install: (app: App) => {
    const plausible = createPlausibleInstance({ config: config.plausible });
    plausible.enableAutoPageviews();

    app.provide('plausible', plausible);
  },
};