Install the SDK
Step-by-step guides
Where to put the snippet, how to track events and route changes, and how to check it works — for each stack:
Script tag (any framework)
<script src="https://cdn.jsdelivr.net/npm/@racoelho/analytics@0.2/dist/index.global.js"></script>
<script>window.analytics = new Analytics.Analytics({ siteKey: 'YOUR_SITE_KEY' })</script>The SDK is about 4KB gzipped, batches events, and by default tracks pageviews, Web Vitals and uncaught JavaScript errors. The instance is on window.analytics for custom events.
npm (any bundler)
Create one instance when the app starts in the browser. The constructor sends the first pageview.
npm install @racoelho/analytics
import { Analytics } from '@racoelho/analytics'
export const analytics = new Analytics({ siteKey: 'YOUR_SITE_KEY' })
analytics.track('signup', { plan: 'pro' })The SDK doesn’t watch client-side route changes: in a single-page app, call analytics.pageview() after each navigation. The guides show where for each router.
Server-side ingestion
Send events directly from your backend for things that happen outside the browser (purchases, signups, API conversions):
await fetch('https://function-bun-production-3339.up.railway.app/v1/i', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.ANALISA_SECRET}`,
},
body: JSON.stringify({
site_key: 'YOUR_SITE_KEY',
batch: [{
event: 'purchase',
ts: Date.now(),
session_id: 'sess_abc',
user_id: 'user_123',
props: { value: 9900, currency: 'USD', order_id: 'ord_456' },
}],
}),
})The Authorization: Bearer header uses your app's secret_key (rotate it via dashboard). Server-side requests bypass Allowed origins checks.
Configuration options
| Option | Default | Description |
|---|---|---|
siteKey | — | Required. |
autoPageview | true | Send a pageview when the SDK starts (client-side route changes need `pageview()`), and flush the queue when the page is hidden or closed. |
autoWebVitals | true | Observe and report LCP/FCP/CLS/INP/TTFB. |
autoErrors | true | Capture uncaught errors and unhandled promise rejections. |
respectDNT | true | Skip tracking if browser sends Do-Not-Track. |
batchSize | 10 | Flush queue when N events buffered. |
flushIntervalMs | 3000 | Flush timer. |
debug | false | Verbose console logs. |