aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/plausible.plugin.ts
blob: cc8998e46a2ee50fd256bddc6b3252b12270ffa0 (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 { noop } from 'lodash';

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

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