Stats on your site
Visitor counts, charts, filters and top pages in React, Vue and Svelte
Show your own numbers on your own site: a visitor count in the footer, a chart on an "open startup" page, the pages people read most, with a date picker and filters for your visitors. The components in @viakite/sdk for React, Vue and Svelte read a share token or a public page, so they only ever see the reports you chose to share, and you can revoke them at any time.
The Vue and Svelte components, <FilterBar />, the custom date range, the previous_year comparison, the themes and the query helpers need @viakite/sdk 0.6.0 or later.
Before you start
- In Analytics → Sharing, click New share and set Kind to Share token.
- Tick the reports your components need (see the table below), then create it and copy the
st_…value. It is shown once.
| Component | Report the share must allow |
|---|---|
<VisitorCount />, <Chart /> | Overview, or any report a query can read |
<TopPages /> | Pages |
<TopSources /> | Sources |
<Countries /> | Locations |
<LiveVisitors /> | Realtime |
<FilterBar /> | The report of each dimension it offers: Pages for page, Sources for source, Locations for country, Devices for device_type, browser and os |
A share token is read-only and made to be seen in the browser: it's fine to put it in your site's code. Never use a secret key (sk_…) here.
Add the components
Install the SDK as for React, Vue or Svelte, then put the components inside one ViakiteProvider:
1import { Chart, Compare, DateRangePicker, FilterBar, LiveVisitors, TopPages, ViakiteProvider, VisitorCount } from "@viakite/sdk/react";23export function Stats() {4return (5<ViakiteProvider share="st_…">6<DateRangePicker /> <Compare /> <FilterBar />7<p>8<VisitorCount /> visitors, <LiveVisitors /> online now9</p>10<Chart metric="visitors" />11<TopPages limit={5} filterable />12</ViakiteProvider>13);14}
1<script setup>2import { Chart, Compare, DateRangePicker, FilterBar, LiveVisitors, TopPages, ViakiteProvider, VisitorCount } from "@viakite/sdk/vue";3</script>45<template>6<ViakiteProvider share="st_…">7<DateRangePicker /> <Compare /> <FilterBar />8<p><VisitorCount /> visitors, <LiveVisitors /> online now</p>9<Chart metric="visitors" />10<TopPages :limit="5" filterable />11</ViakiteProvider>12</template>
1<script>2import { Chart, Compare, DateRangePicker, FilterBar, LiveVisitors, TopPages, ViakiteProvider, VisitorCount } from "@viakite/sdk/svelte";3</script>45<ViakiteProvider share="st_…">6<DateRangePicker /> <Compare /> <FilterBar />7<p><VisitorCount /> visitors, <LiveVisitors /> online now</p>8<Chart metric="visitors" />9<TopPages limit={5} filterable />10</ViakiteProvider>
The components read their data in the browser after they mount. On the server (Next.js, Nuxt, SvelteKit) they render empty, in the loading state, so they are fine in server-rendered pages. In the Next.js App Router they are client components already. The Svelte components ship as source and are compiled by your app's Svelte 5.
ViakiteProvider
Holds the share and the view every component inside it shows: the date range, the comparison and the filters. The controls change the view for all of them at once.
st_…), or a public page's subdomain or id. A page with a password doesn't work here: the components can't unlock it.{ preset } with today, yesterday, 7d, 30d, 90d, this_month, last_month, this_year or all, or { from, to } as dates (2026-11-01, to included). Days are your project's days, in its time zone. A new value later changes the range too.true for the period before, or "previous_year" for the same days a year earlier.[{ dimension: "country", operator: "is", values: ["IN"] }]. Only dimensions the share allows. <FilterBar /> changes them; a new value here does too.dark (Viakite's look), light for light sites, or none for no styles at all.{ range, compare, filters }) each time a control changes it, to keep it in your page's address, for example. In Vue listen to @change; in Svelte pass onchange.https://us.api.vk.viabio.me.<LiveVisitors />. Default: wss://us.live.vk.viabio.me.Components that ask the same question share one request, and an answer is reused for 60 seconds. The API keeps answers for 60 seconds too, so numbers may be a minute old.
Display components
VisitorCount
One total for the range: unique visitors, or another measure with metric (visits, pageviews, bounce_rate, visit_duration, …). With comparison on, it adds the change against the period before, such as +12%, in green or red.
1<VisitorCount />2<VisitorCount metric="pageviews" locale="en-IN" />
LiveVisitors
People on your site now: visitors with any event in the last 5 minutes, updated every 2 seconds over a WebSocket, after a pulsing orange dot. It needs the Realtime report, and it doesn't work for a public page with a password. If the connection drops, the last number stays, the dot turns grey, and it reconnects on its own.
Chart
A measure over time as an SVG line (metric, visitors by default), with the period before as a dashed second line when comparison is on. The time step follows the range's length; set granularity to hour, day, week or month to fix it. With a theme it is 100% wide and 160 px high; change that in your CSS:
1.vk-chart { height: 240px; }
TopPages, TopSources, Countries and TopList
Ranked lists of the range's pages (by path), sources and countries (as two-letter codes such as IN), highest first, each row on a bar as long as its share of the top row. limit sets how many rows (10 by default) and metric what they're ranked by. <TopList dimension="…" /> ranks any other dimension the share allows, such as browser or utm_campaign. With filterable, clicking a row filters every component by its value.
Controls
<DateRangePicker />offers date presets and Custom range, which shows two date inputs; the range changes once both are set.presetspicks which presets to offer,labelsrenames them,custom={false}hides the custom range andcustomLabelrenames it.<Compare />is a checkbox that turns the comparison on and off: with the period before, or withperiod="previous_year"the same days a year earlier.labelchanges its text.<FilterBar />shows the filters as chips with × to remove them, and + Filter adds one: pick a dimension, then one of its top values for the range, with their visitors. Picking another value of the same dimension matches either.dimensionssets which dimensions it offers (page,source,country,device_type,browserandosby default),labelsrenames them,limitsets how many values (20) andaddLabelthe button's text. A dimension the share doesn't allow shows Not available.
Not here yet
There is no goal count yet, and the filter bar adds only "is" filters. Other operators can be set on the provider with filters.
Style them
With the default dark theme, or light, the components follow Viakite's design: numbers and lists in Geist Mono (your site's monospace font when Geist isn't loaded), thin borders, chips and selects like the dashboard's. They draw no background or card, so they sit on your page. The styles are added once, at the start of <head>, so your own CSS for the same classes wins. Set theme="none" to style everything yourself.
| Component | Element and classes |
|---|---|
<VisitorCount /> | span.vk-count with .vk-value and, when comparing, .vk-change with data-trend up, down or flat |
<LiveVisitors /> | span.vk-live with .vk-live-dot |
<Chart /> | svg.vk-chart with .vk-chart-line and .vk-chart-compare, drawn in currentColor |
| Lists | ol.vk-top with data-dimension; each li has .vk-top-label and .vk-top-value (in a button.vk-top-row when filterable) and sets --vk-share, its number against the top row's from 0 to 1 |
<DateRangePicker /> | span.vk-range-picker with select.vk-range and, for a custom range, two input.vk-date |
<Compare /> | label.vk-compare with its checkbox |
<FilterBar /> | div.vk-filters with span.vk-chip (.vk-chip-dimension, .vk-chip-operator, .vk-chip-value, button.vk-chip-remove), select.vk-filter-add, select.vk-filter-value and button.vk-filter-clear |
A themed component also has the class vk-dark or vk-light. data-state is loading until the first answer, then ready, or error. On an error, data-error holds the API's code: not_shared when the share doesn't allow that report or dimension, stats_paused when the share reached its viewer limit, share_expired once it has expired. <LiveVisitors /> shows retrying while it reconnects. Every component also takes a className (React) or class (Vue, Svelte).
Your own components
useReport (React and Vue) runs any query the share allows and gives you the answer, inside the provider's range, comparison and filters:
1import { useReport } from "@viakite/sdk/react";23function Campaigns() {4const r = useReport({ measures: ["visitors"], breakdown: ["utm_campaign"], limit: 3 });5if (r.state !== "ready") return null;6return <ul>{r.report.data.map((row) => <li key={row.utm_campaign}>{row.utm_campaign}: {row.visitors}</li>)}</ul>;7}
In Vue, useReport(() => ({ measures: ["visitors"] })) returns a ref with the same states.
From code: query and realtime
The core package reads your numbers without any component. query runs one report and returns its rows:
1import { query } from "@viakite/sdk";23// In the browser, or anywhere, with a share token:4const report = await query({ share: "st_…" }, { measures: ["visitors", "pageviews"], date_range: { preset: "7d" } });5console.log(report.data[0]); // { visitors: 1840, pageviews: 5312 }67// On your server only, with a secret key (scope analytics.view):8const pages = await query(9{ key: process.env.VIAKITE_SECRET_KEY, project: "prj_…" },10{ measures: ["visitors"], breakdown: ["page"], date_range: { preset: "30d" }, limit: 10 },11);
With a share, it asks the public query, limited to what the share allows. With { key, project } it asks your project's query endpoint, on the region the key names; in a browser it refuses before sending anything, so a secret key never leaves your server. An error answer rejects with a PublicError that has the API's status and code (not_shared, unauthorized, rate_limited, …).
realtime follows a share's live numbers and returns the function that stops it:
1import { realtime } from "@viakite/sdk";23const stop = realtime({ share: "st_…" }, (rt) => console.log(rt.online_now, rt.pages));
The query and the report have the shape of the query endpoints:
POST/v1/public/{share}/queryViewers and billing
Every different person whose browser runs a report through the components or query with a share counts as one monthly viewer of the share, the same as for a public page. <LiveVisitors /> and realtime alone don't count viewers. See Viewers and billing for limits and what happens when a share reaches one. A query with a secret key is an API request instead, within the key's rate limit (10 a second by default).