Files
parenthese/landing/src/analytics.js
T
IliesAllaliandClaude Opus 5.5 4c0d281919 Landing : les événements émis avant l'init de PostHog sont mis en file
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>
2026-09-24 14:11:55 +02:00

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 })
},
}