Skip to content

Commit 6630636

Browse files
feat!: migrate to @plausible-analytics/tracker
1 parent f59c0cf commit 6630636

8 files changed

Lines changed: 82 additions & 97 deletions

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
"release": "bumpp"
4040
},
4141
"dependencies": {
42-
"@barbapapazes/plausible-tracker": "^0.5.6",
4342
"@nuxt/kit": "^4.3.1",
43+
"@plausible-analytics/tracker": "^0.4.4",
4444
"defu": "^6.1.4",
4545
"ufo": "^1.6.3"
4646
},

pnpm-lock.yaml

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/module.ts

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
addServerHandler,
55
createResolver,
66
defineNuxtModule,
7-
useLogger,
87
} from '@nuxt/kit'
98
import { defu } from 'defu'
109
import { joinURL, withLeadingSlash } from 'ufo'
@@ -30,14 +29,6 @@ export interface ModuleOptions {
3029
*/
3130
hashMode?: boolean
3231

33-
/**
34-
* Whether events shall be tracked when running the site locally.
35-
*
36-
* @deprecated Please use `ignoredHostnames` instead.
37-
* @default false
38-
*/
39-
trackLocalhost?: boolean
40-
4132
/**
4233
* Hostnames to ignore when tracking events.
4334
*
@@ -80,12 +71,30 @@ export interface ModuleOptions {
8071
* Track all outbound link clicks automatically.
8172
*
8273
* @remarks
83-
* If enabled, a [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) automagically detects link nodes throughout the application and binds `click` events to them.
74+
* If enabled, outbound link clicks are tracked automatically.
8475
*
8576
* @default false
8677
*/
8778
autoOutboundTracking?: boolean
8879

80+
/**
81+
* Track file downloads automatically.
82+
*
83+
* @remarks
84+
* When enabled, clicks on links to common file types are tracked as `File Download` events.
85+
* Pass an object with `fileExtensions` to customize which file types are tracked.
86+
*
87+
* @default false
88+
*/
89+
fileDownloads?: boolean | { fileExtensions: string[] }
90+
91+
/**
92+
* Track form submissions automatically.
93+
*
94+
* @default false
95+
*/
96+
formSubmissions?: boolean
97+
8998
/**
9099
* Log events to the console if they are ignored.
91100
*
@@ -126,16 +135,16 @@ export default defineNuxtModule<ModuleOptions>({
126135
domain: '',
127136
ignoredHostnames: undefined,
128137
ignoreSubDomains: false,
129-
trackLocalhost: undefined,
130138
apiHost: 'https://plausible.io',
131139
autoPageviews: true,
132140
autoOutboundTracking: false,
141+
fileDownloads: false,
142+
formSubmissions: false,
133143
logIgnoredEvents: false,
134144
proxy: false,
135145
proxyBaseEndpoint: '/_plausible',
136146
},
137147
setup(options, nuxt) {
138-
const logger = useLogger('plausible')
139148
const { resolve } = createResolver(import.meta.url)
140149

141150
// Set default hostnames if `ignoredHostnames` is not set
@@ -144,16 +153,6 @@ export default defineNuxtModule<ModuleOptions>({
144153
// Dedupe `ignoredHostnames` items
145154
options.ignoredHostnames = Array.from(new Set(options.ignoredHostnames))
146155

147-
if (options.trackLocalhost !== undefined) {
148-
logger.warn('The `trackLocalhost` option has been deprecated. Please use `ignoredHostnames` instead.')
149-
}
150-
// Migrate `trackLocalhost` to `ignoredHostnames`
151-
else if (options.trackLocalhost) {
152-
options.ignoredHostnames = options.ignoredHostnames.filter(
153-
domain => domain !== 'localhost',
154-
)
155-
}
156-
157156
// Add module options to public runtime config
158157
nuxt.options.runtimeConfig.public.plausible = defu(
159158
nuxt.options.runtimeConfig.public.plausible as Required<ModuleOptions>,
@@ -191,24 +190,6 @@ export default defineNuxtModule<ModuleOptions>({
191190
mode: 'client',
192191
})
193192

194-
// Split plugins to reduce bundle size
195-
196-
if (options.autoPageviews) {
197-
addPlugin({
198-
src: resolve('runtime/plugin-auto-pageviews.client'),
199-
mode: 'client',
200-
order: 1,
201-
})
202-
}
203-
204-
if (options.autoOutboundTracking) {
205-
addPlugin({
206-
src: resolve('runtime/plugin-auto-outbound-tracking.client'),
207-
mode: 'client',
208-
order: 2,
209-
})
210-
}
211-
212193
// Add preconnect link when proxy is not used
213194
if (!options.proxy) {
214195
addPlugin({

src/runtime/composables/useTrackEvent.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import type {
2-
EventName,
3-
EventOptions,
4-
Plausible,
5-
} from '@barbapapazes/plausible-tracker'
1+
import type { PlausibleEventOptions } from '@plausible-analytics/tracker'
62
import { useNuxtApp } from '#imports'
73

84
/**
@@ -18,8 +14,8 @@ import { useNuxtApp } from '#imports'
1814
* // Tracks the `Download` goal passing a `method` property.
1915
* useTrackEvent('Download', { props: { method: 'HTTP' } })
2016
*/
21-
export function useTrackEvent(eventName: EventName, options?: EventOptions) {
17+
export function useTrackEvent(eventName: string, options?: PlausibleEventOptions) {
2218
if (import.meta.client) {
23-
;(useNuxtApp().$plausible as Plausible)?.trackEvent(eventName, options)
19+
useNuxtApp().$plausible?.trackEvent(eventName, options)
2420
}
2521
}

src/runtime/composables/useTrackPageview.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EventOptions, Plausible } from '@barbapapazes/plausible-tracker'
1+
import type { PlausibleEventOptions } from '@plausible-analytics/tracker'
22
import { useNuxtApp } from '#imports'
33

44
/**
@@ -10,8 +10,8 @@ import { useNuxtApp } from '#imports'
1010
* @example
1111
* useTrackPageview()
1212
*/
13-
export function useTrackPageview(options?: EventOptions) {
13+
export function useTrackPageview(options?: PlausibleEventOptions) {
1414
if (import.meta.client) {
15-
;(useNuxtApp().$plausible as Plausible)?.trackPageview(options)
15+
useNuxtApp().$plausible?.trackPageview(options)
1616
}
1717
}

src/runtime/plugin-auto-outbound-tracking.client.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/runtime/plugin-auto-pageviews.client.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/runtime/plugin.client.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,66 @@
1+
import type { PlausibleEventOptions, PlausibleRequestPayload } from '@plausible-analytics/tracker'
12
import type {} from 'nuxt/app'
3+
import type { ModuleOptions } from '../module'
24
import { defineNuxtPlugin, useRuntimeConfig } from '#imports'
3-
import { createPlausibleTracker } from '@barbapapazes/plausible-tracker'
5+
import { init, track } from '@plausible-analytics/tracker'
6+
import { joinURL } from 'ufo'
47

58
export default defineNuxtPlugin({
69
name: 'plausible',
710
setup() {
8-
const { plausible: options } = useRuntimeConfig().public
11+
const { plausible: options } = useRuntimeConfig().public as { plausible: Required<ModuleOptions> }
912

1013
if (!options.enabled)
1114
return
1215

13-
const plausible = createPlausibleTracker({
14-
...options,
15-
logIgnored: options.logIgnoredEvents,
16+
const ignoredHostnames = options.ignoredHostnames ?? []
17+
18+
init({
1619
domain: options.domain || window.location.hostname,
17-
apiHost: options.proxy ? options.proxyBaseEndpoint : options.apiHost,
20+
endpoint: options.proxy
21+
? joinURL(options.proxyBaseEndpoint, 'api/event')
22+
: joinURL(options.apiHost, 'api/event'),
23+
autoCapturePageviews: options.autoPageviews,
24+
hashBasedRouting: options.hashMode,
25+
outboundLinks: options.autoOutboundTracking,
26+
fileDownloads: options.fileDownloads,
27+
formSubmissions: options.formSubmissions,
28+
// The tracker's built-in check also covers 127.x.x.x, [::1], and file: protocol
29+
captureOnLocalhost: !ignoredHostnames.includes('localhost'),
30+
logging: options.logIgnoredEvents,
31+
// Handle non-localhost ignored hostnames (e.g. staging/preview domains)
32+
transformRequest: buildHostnameFilter(ignoredHostnames, options.ignoreSubDomains),
1833
})
1934

35+
const plausible = {
36+
trackEvent(eventName: string, eventOptions?: PlausibleEventOptions) {
37+
track(eventName, eventOptions ?? {})
38+
},
39+
trackPageview(eventOptions?: PlausibleEventOptions) {
40+
track('pageview', eventOptions ?? {})
41+
},
42+
}
43+
2044
return {
2145
provide: {
2246
plausible,
2347
},
2448
}
2549
},
2650
})
51+
52+
function buildHostnameFilter(ignoredHostnames: string[], ignoreSubDomains: boolean) {
53+
const customIgnoredHostnames = ignoredHostnames.filter(h => h !== 'localhost')
54+
if (customIgnoredHostnames.length === 0)
55+
return undefined
56+
57+
return (payload: PlausibleRequestPayload) => {
58+
const hostname = window.location.hostname
59+
const isIgnored = customIgnoredHostnames.some(ignored =>
60+
ignoreSubDomains
61+
? hostname === ignored || hostname.endsWith(`.${ignored}`)
62+
: hostname === ignored,
63+
)
64+
return isIgnored ? null : payload
65+
}
66+
}

0 commit comments

Comments
 (0)