viakite
Dashboard

Guides/Share and alert/Stats on your site

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

  1. In Analytics → Sharing, click New share and set Kind to Share token.
  2. Tick the reports your components need (see the table below), then create it and copy the st_… value. It is shown once.
ComponentReport 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:

src/Stats.tsx
1import { Chart, Compare, DateRangePicker, FilterBar, LiveVisitors, TopPages, ViakiteProvider, VisitorCount } from "@viakite/sdk/react";
2
3export function Stats() {
4 return (
5 <ViakiteProvider share="st_…">
6 <DateRangePicker /> <Compare /> <FilterBar />
7 <p>
8 <VisitorCount /> visitors, <LiveVisitors /> online now
9 </p>
10 <Chart metric="visitors" />
11 <TopPages limit={5} filterable />
12 </ViakiteProvider>
13 );
14}
src/Stats.vue
1<script setup>
2import { Chart, Compare, DateRangePicker, FilterBar, LiveVisitors, TopPages, ViakiteProvider, VisitorCount } from "@viakite/sdk/vue";
3</script>
4
5<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>
src/Stats.svelte
1<script>
2 import { Chart, Compare, DateRangePicker, FilterBar, LiveVisitors, TopPages, ViakiteProvider, VisitorCount } from "@viakite/sdk/svelte";
3</script>
4
5<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.

sharestringrequired
A share token (st_…), or a public page's subdomain or id. A page with a password doesn't work here: the components can't unlock it.
dateRangeobjectdefault { preset: "30d" }
The range shown: { 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.
compareboolean | stringdefault false
Compare from the start: true for the period before, or "previous_year" for the same days a year earlier.
filtersarray
Filters every component applies, in the API's shape: [{ dimension: "country", operator: "is", values: ["IN"] }]. Only dimensions the share allows. <FilterBar /> changes them; a new value here does too.
themestringdefault dark
dark (Viakite's look), light for light sites, or none for no styles at all.
onChangefunction
Called with the new view ({ 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.
regionstringdefault us
The data region of your project.
apistring
Read from another address, such as a proxy. Default: https://us.api.vk.viabio.me.
livestring
The live socket's address for <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.

tsx
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:

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. presets picks which presets to offer, labels renames them, custom={false} hides the custom range and customLabel renames it.
  • <Compare /> is a checkbox that turns the comparison on and off: with the period before, or with period="previous_year" the same days a year earlier. label changes 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. dimensions sets which dimensions it offers (page, source, country, device_type, browser and os by default), labels renames them, limit sets how many values (20) and addLabel the 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.

ComponentElement 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
Listsol.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:

tsx
1import { useReport } from "@viakite/sdk/react";
2
3function Campaigns() {
4 const r = useReport({ measures: ["visitors"], breakdown: ["utm_campaign"], limit: 3 });
5 if (r.state !== "ready") return null;
6 return <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:

js
1import { query } from "@viakite/sdk";
2
3// 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 }
6
7// 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:

js
1import { realtime } from "@viakite/sdk";
2
3const 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}/query

Viewers 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).