diff --git a/src/config.ts b/src/config.ts index 1ceff273..58fa3920 100644 --- a/src/config.ts +++ b/src/config.ts @@ -29,6 +29,12 @@ export const config = figue({ }, }, plausible: { + isTrackerEnabled: { + doc: 'Is the tracker enabled', + format: 'boolean', + default: false, + env: 'VITE_TRACKER_ENABLED', + }, domain: { doc: 'Plausible current domain', format: 'string', diff --git a/src/plugins/plausible.plugin.ts b/src/plugins/plausible.plugin.ts index 10975eac..50a694db 100644 --- a/src/plugins/plausible.plugin.ts +++ b/src/plugins/plausible.plugin.ts @@ -1,11 +1,36 @@ import { config } from '@/config'; +import { noop } from 'lodash'; import Plausible from 'plausible-tracker'; import type { App } from 'vue'; +function createFakePlausibleInstance(): Pick, '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 = Plausible(config.plausible); + const plausible = createPlausibleInstance({ config: config.plausible }); plausible.enableAutoPageviews(); app.provide('plausible', plausible);