Riferimento API
Documentazione completa dell'API JavaScript del tracker Klar.
Global Object
The Klar tracker is available as window.klar after the script loads.
Methods
klar.page(path?)
Manually track a pageview. Useful for custom routing implementations, or when data-auto-track="false". klar.trackPageview(path?) is an alias for
the same function.
Parameters
path(optional) — Page path, e.g./pricing(defaults towindow.location.pathname)
Example
// Track current page
klar.page();
// Track a custom path
klar.page('/custom-page'); Only the path is recorded: there is no URL or title argument. A second pageview for the same path within 1.5 seconds is ignored.
klar.track(eventName, properties?)
Track a custom event with optional properties.
Parameters
eventName(required) — Event name (string)properties(optional) — Event data (object)
Example
// Simple event
klar.track('button_click');
// Event with properties
klar.track('purchase', {
product: 'Pro Plan',
price: 29,
currency: 'USD'
});
// Form submission
klar.track('form_submit', {
form: 'contact',
fields: ['name', 'email', 'message']
}); Properties are sent with the event as a JSON object. Keep them small and never put personal data in them.
klar.goal(goalName, properties?)
Record a goal conversion. It tracks the event goal_<goalName> with the
optional properties.
Parameters
goalName(required) — Goal name (string)properties(optional) — Event data (object)
Example
klar.goal('signup');
// tracks the event goal_signupklar.consent(state)
Grant or withdraw consent. Pair it with data-require-consent, which holds
tracking until consent is granted.
Parameters
state(required) —'granted'(ortrue) starts or resumes tracking;'denied'(orfalse) stops it
Example
// After the visitor accepts your own consent prompt
klar.consent('granted'); Klar itself needs no consent banner; this gate is for embedders who want their own.
klar.init(config?)
Start the tracker from JavaScript instead of script attributes. Called automatically
from the script tag's data-* attributes when data-site-id is set.
Config keys
site(orid) — Site IDapiKey— Site API key (same asdata-key)endpoint— Collect endpoint URL (same asdata-endpoint)honorDnt—trueto honor DNT/GPCrequireConsent—trueto wait forklar.consent()autoTrack—falseto disable automatic trackingdebug—trueto log to the console
Example
klar.init({ site: 'abc123', apiKey: 'your-api-key' });klar.identify(userId)
Associate future events with a user ID (while maintaining privacy). The ID is kept in
memory only and is added to event data as user_id; reloading the page
clears it.
Parameters
userId(required) — Anonymous user identifier
Example
// Identify user (use hashed/anonymous IDs)
klar.identify('user_abc123');
// All future events will be associated with this user Privacy Note
⚠️ Do not use personally identifiable information (email, name, etc.). Use hashed or anonymous IDs only.
klar.reset()
Clear the user identification set by klar.identify().
Example
// User logs out
klar.reset();Configuration
Configure Klar using data attributes on the script tag:
data-site-id
Required. Your site ID from the Klar dashboard.
<script
src="https://klar.alphabros.eu/klar.v2.js"
data-site-id="abc123"
data-key="your-api-key"
defer
></script>data-key
Required if your site has an API key — every site created from the
dashboard does. Sent as the data-token/data-key field on the
collect request; a snippet missing it gets a 403 (Invalid API key) from /api/collect and records nothing.
<script
src="https://klar.alphabros.eu/klar.v2.js"
data-site-id="abc123"
data-key="your-api-key"
defer
></script>data-auto-track
Enable/disable automatic pageview tracking. Default: true
<script
src="https://klar.alphabros.eu/klar.v2.js"
data-site-id="abc123"
data-auto-track="false"
defer
></script> When disabled, no pageviews, scroll or time-on-page events are sent automatically; call klar.page() and klar.track() yourself.
data-honor-dnt
Respect the browser's Do Not Track (DNT) and Global Privacy Control (GPC) signals.
Default: false (opt-in). Klar's privacy is already structural (storageless,
no cookies/PII), so honoring DNT/GPC is off by default — set to "true" to suppress tracking for visitors who send these signals.
<script
src="https://klar.alphabros.eu/klar.v2.js"
data-site-id="abc123"
data-honor-dnt="true"
defer
></script> While enabled, no tracking occurs and no identifier is stored if DNT or GPC is set.
data-endpoint
Full URL of the collect endpoint. Default: /api/collect on the origin that served
the tracker script.
<script
src="https://cdn.example.com/klar.v2.js"
data-site-id="abc123"
data-endpoint="https://analytics.example.com/api/collect"
defer
></script>data-require-consent
Hold all tracking until your page calls klar.consent('granted'). Any value
except "false" turns it on. Default: off.
<script
src="https://klar.alphabros.eu/klar.v2.js"
data-site-id="abc123"
data-require-consent
defer
></script>REST API
Server-side tracking using the HTTP API.
POST /api/collect
Send a pageview or event.
This is the endpoint the tracker posts to. You can post to it from your own server too, with the same fields.
Headers
Content-Type: application/json Body fields
s(required) — your site IDtype—pageview(default) oreventp— the page path (defaults to/)r— the referrer URL, if anyw— the screen width in pixelse— the event name, for an eventd— the event properties (an object), for an eventutm_source,utm_medium,utm_campaign— campaign tags; each is trimmed, capped at 100 characters and dropped if it contains@data-key— the site's data key, required when the site has one (every site created from the dashboard does)
Example body (pageview)
{
"s": "abc123",
"type": "pageview",
"p": "/pricing",
"r": "https://chatgpt.com/",
"w": 1440,
"utm_campaign": "launch",
"data-key": "your-site-data-key"
} Example body (event)
{
"s": "abc123",
"type": "event",
"p": "/pricing",
"e": "signup",
"d": {
"plan": "pro"
},
"data-key": "your-site-data-key"
} Response
HTTP 201 with:
{
"ok": true
} Errors
400— the body is not JSON,sis missing, or no site has that ID403Invalid referer— the request'sRefererorOriginhost is not the site's domain or one of its aliases (a server-side request without either header passes this check)403Invalid API key— the site has a data key and the body does not carry it402— the account's monthly pageview limit is reached
Example (cURL)
curl -X POST https://klar.alphabros.eu/api/collect \
-H "Content-Type: application/json" \
-d '{"s":"abc123","type":"pageview","p":"/pricing","r":"https://chatgpt.com/","data-key":"your-site-data-key"}' The data key is the data-key value of your tracking snippet. It is public
(it ships in your page) and only lets you send data. It is not an API v1 key: those
start with klr_, read your stats, and must stay secret.
GET /api/v1/sites/:siteId/export
Download raw pageviews or events with a site API key.
Headers
Authorization: Bearer klr_your_api_key Query parameters
type—pageviewsorevents(required)period—today,7d,30d,90d,year, orall(defaults to30d)
Returns CSV, capped at 100,000 rows to match the session export. A capped response
includes X-Export-Truncated: true. The endpoint uses the same per-key rate
limit as the other /api/v1 endpoints.
Example (cURL)
curl -H "Authorization: Bearer klr_your_api_key" \
"https://klar.alphabros.eu/api/v1/sites/abc123/export?type=pageviews&period=30d"API v1
API v1 reads your stats. Every request needs a site API key, sent as a bearer token. A key
starts with klr_ and belongs to one site. Create one while signed in with POST /api/keys and the body {"siteId": "abc123", "name": "Reports"}: the response carries the
full key, which is not shown again. Every plan has API keys, Free included (up to 3).
Authorization: Bearer klr_your_api_key Errors
401— noAuthorization: Bearerheader, or an unknown or expired key429— the key's per-minute rate limit is used up (60 requests a minute by default)404— no site has that ID, or the endpoint does not exist403— the key belongs to another site
Period
start and end are ISO 8601 timestamps, for example 2026-10-01T00:00:00Z. Without start the period starts at the
beginning of your data; without end it ends now.
GET /api/v1/sites/:siteId/channels
Pageviews by channel, and by AI assistant.
The six channels and the rules that assign them are described in AI assistant traffic. aiAssistants breaks the ai_assistant channel down by assistant.
Example (cURL)
curl -H "Authorization: Bearer klr_your_api_key" \
"https://klar.alphabros.eu/api/v1/sites/abc123/channels?start=2026-10-01T00:00:00Z&end=2026-10-31T23:59:59Z" Response
{
"period": {
"start": "2026-10-01T00:00:00.000Z",
"end": "2026-10-31T23:59:59.000Z"
},
"channels": {
"direct": 412,
"search": 380,
"social": 96,
"ai_assistant": 57,
"email": 31,
"referral": 124
},
"aiAssistants": [
{
"id": "chatgpt",
"label": "ChatGPT",
"pageviews": 33,
"visitors": 29
},
{
"id": "perplexity",
"label": "Perplexity",
"pageviews": 15,
"visitors": 14
},
{
"id": "claude",
"label": "Claude",
"pageviews": 9,
"visitors": 8
}
],
"total": 1100
} - Live data only: imported Plausible or Fathom history is not counted, in the channels
or in
total. - The channels are worked out for at most 1,000 distinct referrer and campaign-tag
groups per request. On a site with more, the pageviews of the remaining groups count
as
referral, so the other channels can be slightly low.
GET /api/v1/sites/:siteId/referrers
Top referrers, with the channel summary.
Example (cURL)
curl -H "Authorization: Bearer klr_your_api_key" \
"https://klar.alphabros.eu/api/v1/sites/abc123/referrers?start=2026-10-01T00:00:00Z&end=2026-10-31T23:59:59Z" Response
{
"referrers": [
{
"referrer": "(direct)",
"count": 412,
"conversions": 0,
"type": "direct",
"ctr": "0.00"
},
{
"referrer": "https://www.google.com/",
"count": 301,
"conversions": 0,
"type": "search",
"ctr": "0.00"
},
{
"referrer": "https://chatgpt.com/",
"count": 33,
"conversions": 0,
"type": "ai_assistant",
"ctr": "0.00"
}
],
"total": 1340,
"summary": {
"direct": 412,
"search": 380,
"social": 96,
"ai_assistant": 57,
"email": 31,
"referral": 124
}
} referrersholds the top 50 referrer URLs of live pageviews. Visits with no referrer are grouped as(direct).- A row's
typecomes from its referrer only, so it can beai_assistantbut neveremail(Email comes fromutm_medium). summaryis the same six-channel object aschannelsin/channels: live data only, with the same 1,000-group bound.totalalso counts imported pageviews, so on a site with imported history it is larger than the sum ofsummary. In the example, 240 imported pageviews make the difference.conversionsis always0andctralways"0.00".
Event Types
Common event names and their properties:
button_click
klar.track('button_click', {
button: 'signup',
location: 'header'
});form_submit
klar.track('form_submit', {
form: 'contact',
fields: ['name', 'email']
});purchase
klar.track('purchase', {
product: 'Pro Plan',
price: 29,
currency: 'USD'
});video_play
klar.track('video_play', {
video_id: '123',
duration: 45,
provider: 'youtube'
});search
klar.track('search', {
query: 'analytics',
results: 42
});Browser Support
Klar supports all modern browsers:
- Chrome / Edge (last 2 versions)
- Firefox (last 2 versions)
- Safari (last 2 versions)
- Mobile browsers (iOS Safari, Chrome Mobile)
IE11 and older browsers are not supported.