JavaScript
@viakite/sdk from npm
@viakite/sdk is the SDK as an npm package, for browser apps built with a bundler. You call init() once, and it sends page views, follows page changes in single-page apps, and measures engaged time, scroll depth, outbound links and file downloads. Five functions cover everything else.
Install
1npm install @viakite/sdk
1pnpm add @viakite/sdk
1yarn add @viakite/sdk
The package ships ES modules and CommonJS, with types. It has no dependencies. Using Next.js, React, Vue or Svelte? Use the component for your framework instead of calling init() yourself: Next.js, React, Vue, Svelte.
Start it
Call init() once, as early as possible, in your entry file:
1import { init } from "@viakite/sdk";23init({ project: "pk_live_us_…" });
That's the whole setup. Your key is on the dashboard's Install page. Then send what matters to your business:
1import { track } from "@viakite/sdk";23track("Purchase", { revenue: 499, currency: "INR", plan: "pro" });
The five functions
1init(options: Options): void2track(name: string, props?: Props): void3identify(userId: string, traits?: Props): void4reset(): void5pageview(options?: PageviewOptions): void
| Function | Does |
|---|---|
| init() | Starts tracking on this page. Call it once. |
| track() | Sends a custom event, with up to 20 properties, or a sale with revenue and currency. |
| identify() | Ties this tab's next events to your own user id. |
| reset() | Forgets the identified user in this tab. Call it on sign-out. |
| pageview() | Sends a page view by hand, when you turn automatic page views off. |
All five return nothing and never throw. Calls to track(), identify(), reset() and pageview() made before init() are kept (up to 100) and run once init() does, so a component that tracks something while your app starts loses nothing. Every option of init() is on Options, with its types.
Single-page apps
The SDK follows history.pushState, history.replaceState and the popstate event, which every common router uses. It sends a new page view only when the path changes:
| Change | New page view |
|---|---|
/products → /products/42 | Yes |
/products → /products?sort=price | No: only the query changed |
/products → /products#reviews | No, unless hash is on |
/#/about → /#/pricing with hash: true | Yes |
Before each new page view, the engaged time and scroll depth of the page being left are sent first. The referrer is sent with the first page view only, and only when it is another site.
If your router doesn't use the history API, turn automatic page views off and call pageview() from it.
Server-side rendering
The package is safe to import in code that also runs on the server. On the server there is no window, and every function does nothing: init() returns at once, and nothing is sent. Call init() in code that runs in the browser (an effect, onMount, or your client entry), or use the framework component, which does that for you.
To send events from your server, use a secret key: see Server-side events.
When nothing is sent
init() turns tracking off, without an error, when:
- the key is missing or isn't a public key (
pk_live_…orpk_test_…); - the page is on
localhost,127.0.0.1orfile://, unlesslocalhostis on; - the browser is automated (
navigator.webdriver); - you excluded your own visits in this browser with
localStorage.setItem("viakite_ignore", "true").
Turn debug on to see the reason in the console. See Debugging.
Read your numbers
From @viakite/sdk 0.6.0, query() runs a report with a share token, or with a secret key on your server, and realtime() follows a share's live numbers:
1import { query } from "@viakite/sdk";23const report = await query({ share: "st_…" }, { measures: ["visitors"], date_range: { preset: "7d" } });
See Stats on your site for both, and the components that show them.