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
1function init(options: Options): void;
Parameters
The settings. Only project is required:
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
1import { init } from "@viakite/sdk";23init({ project: "pk_live_us_…" });
1<script defer src="https://us.api.vk.viabio.me/v1/a.js" data-project="pk_live_us_…"></script>
With options:
1import { init } from "@viakite/sdk";23init({4project: "pk_live_us_…",5title: true,6params: ["plan", "coupon"],7outbound: false,8});
1<script defer src="https://us.api.vk.viabio.me/v1/a.js"2data-project="pk_live_us_…"3data-title4data-params="plan,coupon"5data-outbound="false"></script>
What happens when it runs
- It reads the options and fills in the defaults.
- It checks the key. A missing key, or one that isn't a public key, turns the SDK off.
- It checks the visit. Nothing is sent from an automated browser (
navigator.webdriver), from a browser where you excluded your own visits (localStoragekeyviakite_ignoreset to"true"), or fromlocalhost,127.0.0.1andfile://unlesslocalhostis on. - It starts watching clicks, for outbound links, file downloads and class events.
- With
autoPageviewson (the default), it sends the page view and starts following page changes. - It runs the calls to
track(),identify(),reset()andpageview()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 line | Means |
|---|---|
[viakite] init twice | init() 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: localhost | The page is on localhost or file:// and localhost is off. |
[viakite] off: webdriver | The browser is automated. |
[viakite] off: viakite_ignore | Your 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.