mirror of
https://github.com/IliesAllali/parenthese.git
synced 2026-11-19 08:52:50 +00:00
Depuis le chargement différé de PostHog, page_viewed partait au premier rendu, avant init(), et était jeté au lieu d'attendre dans la file. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
60 lines
1.4 KiB
JavaScript
60 lines
1.4 KiB
JavaScript
/**
|
|
* PostHog analytics wrapper.
|
|
* Activate by setting VITE_POSTHOG_KEY in your .env file.
|
|
* Events are silently dropped when the key is absent.
|
|
*/
|
|
|
|
const KEY = import.meta.env.VITE_POSTHOG_KEY
|
|
let ph = null
|
|
let initStarted = false
|
|
let enabled = Boolean(KEY)
|
|
const queuedEvents = []
|
|
const MAX_QUEUED_EVENTS = 100
|
|
|
|
function flushQueue() {
|
|
if (!ph || typeof ph.capture !== 'function') return
|
|
while (queuedEvents.length > 0) {
|
|
const next = queuedEvents.shift()
|
|
ph.capture(next.event, next.props)
|
|
}
|
|
}
|
|
|
|
export const analytics = {
|
|
init() {
|
|
if (initStarted) return
|
|
initStarted = true
|
|
|
|
if (!KEY) return
|
|
|
|
import('posthog-js')
|
|
.then(({ default: posthog }) => {
|
|
posthog.init(KEY, {
|
|
api_host: import.meta.env.VITE_POSTHOG_HOST || 'https://us.i.posthog.com',
|
|
cross_subdomain_cookie: true,
|
|
defaults: '2026-01-30',
|
|
person_profiles: 'identified_only',
|
|
autocapture: false,
|
|
capture_pageview: false,
|
|
})
|
|
ph = posthog
|
|
flushQueue()
|
|
})
|
|
.catch(() => {
|
|
enabled = false
|
|
queuedEvents.length = 0
|
|
})
|
|
},
|
|
|
|
capture(event, props = {}) {
|
|
if (!event) return
|
|
if (ph && typeof ph.capture === 'function') {
|
|
ph.capture(event, props)
|
|
return
|
|
}
|
|
|
|
if (!enabled) return
|
|
if (queuedEvents.length >= MAX_QUEUED_EVENTS) return
|
|
queuedEvents.push({ event, props })
|
|
},
|
|
}
|