Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Plausible #35

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ export default {
script: 'https://example.com/proxy.js',
noScript: 'https://example.com/simple/noscript.gif'
},

// Plausible
plausible: {
enabled: true, // or false
/**
* You can overwrite domain name to send stats to multiple Plausible dashboards (optional)
* @see https://plausible.io/docs/plausible-script#can-i-send-stats-to-multiple-dashboards-at-the-same-time
*/
hostname: 'example.com',
/**
* You can configure a proxy (optional)
* @see https://plausible.io/docs/proxy/introduction
*/
script: 'example.com/js/script.js',
},
})
],
}
Expand Down
5 changes: 5 additions & 0 deletions example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export default defineConfig({
// script: 'https://example.com/proxy.js',
// noScript: 'https://example.com/simple/noscript.gif',
// },
// plausible: {
// enabled: true,
// hostname: 'www.custom.com',
// script: 'https://example.com/js/script.js',
// },
}),
],
})
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import type { TikTokPixelOptions } from './tiktok-pixel'
import injectTikTokPixel from './tiktok-pixel'
import type { SimpleAnalyticsOptions } from './simple-analytics'
import injectSimpleAnalytics from './simple-analytics'
import type { PlausibleAnalyticsOptions } from './plausible'
import injectPlausible from './plausible'

export interface VitePluginRadarOptions {
enableDev?: boolean
Expand All @@ -41,6 +43,7 @@ export interface VitePluginRadarOptions {
unbounce?: UnbounceOptions | boolean
tiktok?: TikTokPixelOptions
simpleanalytics?: SimpleAnalyticsOptions
plausible?: PlausibleAnalyticsOptions
}

export function VitePluginRadar({
Expand All @@ -58,6 +61,7 @@ export function VitePluginRadar({
unbounce,
tiktok,
simpleanalytics,
plausible,
}: VitePluginRadarOptions): Plugin {
let viteConfig: ResolvedConfig

Expand Down Expand Up @@ -114,6 +118,9 @@ export function VitePluginRadar({
if (simpleanalytics)
tags.push(...injectSimpleAnalytics(simpleanalytics))

if (plausible)
tags.push(...injectPlausible(plausible))

return tags
},
}
Expand Down
36 changes: 36 additions & 0 deletions src/plausible.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { HtmlTagDescriptor } from 'vite'

export interface PlausibleAnalyticsOptions {
enabled: boolean
hostname?: string
script?: string
}

/**
* @see https://plausible.io/docs/plausible-script
*/
const DefaultScriptUrl = 'https://plausible.io/js/script.js'

function injectTag(options: PlausibleAnalyticsOptions): HtmlTagDescriptor[] {
const tags: HtmlTagDescriptor[] = []

if (!options.enabled)
return tags

const scriptAttrs: Record<string, string | boolean> = {
src: options.script || DefaultScriptUrl,
defer: true,
}

if (options.hostname)
scriptAttrs['data-domain'] = options.hostname

tags.push({
tag: 'script',
injectTo: 'head',
attrs: scriptAttrs,
})

return tags
}
export default injectTag