@prunellejs/analytics 0.7.0 → 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/llms.txt +2 -1
- package/package.json +2 -2
- package/src/index.ts +7 -6
package/llms.txt
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
## Imports
|
|
6
6
|
|
|
7
|
-
- `@prunellejs/analytics`: `analytics`, `plausible`, `umami`, `matomo`, `custom`, `computeVisitorId`, `DEFAULT_EVENT_ENDPOINT`, `DEFAULT_VITALS_ENDPOINT`; types `AnalyticsOptions`, `AnalyticsProvider`, `AnalyticsPageview`, `AnalyticsEvent`, `AnalyticsEventProps`, `WebVitalMetric`, `WebVitalName`, `WebVitalRating`, `WebVitalsOptions`, `PlausibleOptions`, `UmamiOptions`, `MatomoOptions`, `CustomAnalyticsOptions`.
|
|
7
|
+
- `@prunellejs/analytics`: `analytics`, `plausible`, `umami`, `matomo`, `custom`, `coarseUserAgent`, `computeVisitorId`, `DEFAULT_EVENT_ENDPOINT`, `DEFAULT_OPT_OUT_ENDPOINT`, `DEFAULT_VITALS_ENDPOINT`, `OPT_OUT_COOKIE`; types `AnalyticsOptions`, `AnalyticsProvider`, `AnalyticsPageview`, `AnalyticsEvent`, `AnalyticsEventProps`, `WebVitalMetric`, `WebVitalName`, `WebVitalRating`, `WebVitalsOptions`, `PlausibleOptions`, `UmamiOptions`, `MatomoOptions`, `CustomAnalyticsOptions`.
|
|
8
8
|
- `@prunellejs/analytics/client`: `trackEvent`; types `AnalyticsEventProps`, `TrackEventOptions` (browser-safe, zero server imports).
|
|
9
9
|
|
|
10
10
|
## Setup (`analytics(options)`)
|
|
@@ -35,6 +35,7 @@ export default defineConfig({
|
|
|
35
35
|
plugins: [
|
|
36
36
|
analytics({
|
|
37
37
|
provider: plausible({ domain: 'example.com', host: 'https://stats.example.com' }),
|
|
38
|
+
eventProps: ['plan'],
|
|
38
39
|
}),
|
|
39
40
|
],
|
|
40
41
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prunellejs/analytics",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "CNIL-compliant server-side first-party audience measurement, custom events, and optional Web Vitals plugin for Prunelle",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -22,6 +22,6 @@
|
|
|
22
22
|
"directory": "packages/plugin-analytics"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"prunelle": "^0.7.
|
|
25
|
+
"prunelle": "^0.7.1"
|
|
26
26
|
}
|
|
27
27
|
}
|
package/src/index.ts
CHANGED
|
@@ -12,7 +12,7 @@ import type {
|
|
|
12
12
|
} from './types'
|
|
13
13
|
import { definePlugin } from 'prunelle/config'
|
|
14
14
|
import { resolveConsentCategory } from './consent'
|
|
15
|
-
import { escAttr } from 'prunelle/server'
|
|
15
|
+
import { escAttr, safeRedirect } from 'prunelle/server'
|
|
16
16
|
import {
|
|
17
17
|
coarseUserAgent,
|
|
18
18
|
computeVisitorId,
|
|
@@ -346,7 +346,7 @@ function isOptedOut(ctx: RequestContext): boolean {
|
|
|
346
346
|
}
|
|
347
347
|
|
|
348
348
|
// POST stops counting this visitor, DELETE resumes it; a form post (no JS) is sent back to the page it came from.
|
|
349
|
-
function createOptOutHandler(): (ctx: RequestContext) => Response {
|
|
349
|
+
function createOptOutHandler(site: string | undefined): (ctx: RequestContext) => Response {
|
|
350
350
|
return (ctx) => {
|
|
351
351
|
if (ctx.request.method === 'DELETE')
|
|
352
352
|
ctx.cookies.delete(OPT_OUT_COOKIE, { path: '/' })
|
|
@@ -354,8 +354,8 @@ function createOptOutHandler(): (ctx: RequestContext) => Response {
|
|
|
354
354
|
ctx.cookies.set(OPT_OUT_COOKIE, '1', { path: '/', maxAge: OPT_OUT_MAX_AGE_S, httpOnly: true, sameSite: 'lax', category: 'essential' })
|
|
355
355
|
if (!(ctx.request.headers.get('accept') ?? '').includes('text/html'))
|
|
356
356
|
return new Response(null, { status: 204 })
|
|
357
|
-
const back = parseSameOriginCandidate(ctx.request.headers.get('referer'), ctx.url.origin, ctx.url.origin)
|
|
358
|
-
return new Response(null, { status: 303, headers: { location: back !== undefined ? `${back.pathname}${back.search}` : '/' } })
|
|
357
|
+
const back = parseSameOriginCandidate(ctx.request.headers.get('referer'), resolveBaseOrigin(site, ctx.url.origin), ctx.url.origin)
|
|
358
|
+
return new Response(null, { status: 303, headers: { location: back !== undefined ? safeRedirect(`${back.pathname}${back.search}`) : '/' } })
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
361
|
|
|
@@ -644,8 +644,9 @@ export function analytics(options: AnalyticsOptions): PrunellePlugin {
|
|
|
644
644
|
if (eventEndpoint !== false)
|
|
645
645
|
api.route('POST', eventEndpoint, createEventHandler(cfg, api.site, api.dev, dispatch), routeOpts)
|
|
646
646
|
if (cfg.optOutEndpoint !== false) {
|
|
647
|
-
|
|
648
|
-
api.route('
|
|
647
|
+
const optOutHandler = createOptOutHandler(api.site)
|
|
648
|
+
api.route('POST', cfg.optOutEndpoint, optOutHandler, { csrf: true })
|
|
649
|
+
api.route('DELETE', cfg.optOutEndpoint, optOutHandler, { csrf: true })
|
|
649
650
|
}
|
|
650
651
|
if (viewEndpoint !== false)
|
|
651
652
|
api.route('POST', viewEndpoint, createViewHandler(cfg, isInternalPath, api.site, dispatch), routeOpts)
|