viakite
Dashboard

SDKs/Web/init()

init()

Start tracking

init() starts Viakite Analytics on the page. It sends the first page view, then follows page changes, engaged time, scroll depth and clicks. Call it once, as early as you can; the script tag and the framework components call it for you.

Signature

ts
1function init(options: Options): void;

Parameters

optionsOptionsrequired

The settings. Only project is required:

options.projectstringrequired

Your project's public key, pk_live_<region>_… or pk_test_<region>_…. The region in the key decides where events go: pk_live_us_… sends to https://us.e.vk.viabio.me/v1/events.

Every other option, with its type, default and script-tag attribute, is on Options.

Examples

src/main.ts
1import { init } from "@viakite/sdk";
2
3init({ project: "pk_live_us_…" });
index.html
1<script defer src="https://us.api.vk.viabio.me/v1/a.js" data-project="pk_live_us_…"></script>

With options:

src/main.ts
1import { init } from "@viakite/sdk";
2
3init({
4 project: "pk_live_us_…",
5 title: true,
6 params: ["plan", "coupon"],
7 outbound: false,
8});
index.html
1<script defer src="https://us.api.vk.viabio.me/v1/a.js"
2 data-project="pk_live_us_…"
3 data-title
4 data-params="plan,coupon"
5 data-outbound="false"></script>

What happens when it runs

  1. It reads the options and fills in the defaults.
  2. It checks the key. A missing key, or one that isn't a public key, turns the SDK off.
  3. It checks the visit. Nothing is sent from an automated browser (navigator.webdriver), from a browser where you excluded your own visits (localStorage key viakite_ignore set to "true"), or from localhost, 127.0.0.1 and file:// unless localhost is on.
  4. It starts watching clicks, for outbound links, file downloads and class events.
  5. With autoPageviews on (the default), it sends the page view and starts following page changes.
  6. It runs the calls to track(), identify(), reset() and pageview() that were made before it, in order.

Page views and custom events are sent at once. Engaged time and scroll depth are sent when the page is hidden or left. A failed send is retried after 1, 5 and 15 seconds, and waits for the network when the browser is offline. The queue lives in memory only, so events still waiting when the tab closes are lost.

Once per page

init() runs once per page load. A second call does nothing, whatever its options. This makes it safe in React's strict mode and in code that may run twice. To change options, change them in the one call; options can't be changed after it.

Calls made before init() are kept, up to 100, and run when it starts. If init() turns the SDK off (a bad key, localhost, an excluded browser), those calls and every later one are dropped.

On the server

On the server there is no document, and init() returns without doing anything. You can import and call it in code that renders on the server; tracking starts when the same code runs in the browser.

Warnings in debug mode

init() never throws. With debug: true (data-debug) it writes a [viakite] line to the console for each problem, and sends nothing:

Console lineMeans
[viakite] init twiceinit() was called again (logged when that call has debug on); it is ignored.
[viakite] bad key: …The key is missing or not a public key; tracking is off.
[viakite] off: localhostThe page is on localhost or file:// and localhost is off.
[viakite] off: webdriverThe browser is automated.
[viakite] off: viakite_ignoreYour own visits are excluded in this browser.
[viakite] send {…}An event that would be sent, with its payload.

In debug mode the SDK keeps running when the visit would be blocked, so you can see the events on localhost. Without debug mode it is silent. See Debugging.

Test keys

Events sent with a pk_test_… key are stored apart from live data and left out of every report. They show only in the event debugger on the Install page, marked test, so you can check your setup there.

Next steps