viakite
Dashboard

SDKs/Web/JavaScript

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

terminal
1npm install @viakite/sdk
terminal
1pnpm add @viakite/sdk
terminal
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:

src/main.ts
1import { init } from "@viakite/sdk";
2
3init({ 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:

src/checkout.ts
1import { track } from "@viakite/sdk";
2
3track("Purchase", { revenue: 499, currency: "INR", plan: "pro" });

The five functions

ts
1init(options: Options): void
2track(name: string, props?: Props): void
3identify(userId: string, traits?: Props): void
4reset(): void
5pageview(options?: PageviewOptions): void
FunctionDoes
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:

ChangeNew page view
/products → /products/42Yes
/products → /products?sort=priceNo: only the query changed
/products → /products#reviewsNo, unless hash is on
/#/about → /#/pricing with hash: trueYes

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_… or pk_test_…);
  • the page is on localhost, 127.0.0.1 or file://, unless localhost is 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:

js
1import { query } from "@viakite/sdk";
2
3const report = await query({ share: "st_…" }, { measures: ["visitors"], date_range: { preset: "7d" } });

See Stats on your site for both, and the components that show them.

Next steps