Eventvisor

SDKs

JavaScript SDK

Eventvisor's JavaScript SDK (~8kB) is universal, meaning it works in both Node.js and browser environments.

Installation

Install with npm in your application:

Command
$ npm install --save @eventvisor/sdk

Initialization

The SDK can be initialized by passing the datafile to it directly:

your-app/index.js
import { createInstance } from "@eventvisor/sdk";
const DATAFILE_URL = "https://cdn.yoursite.com/eventvisor-tag-web.json";
const datafile = await fetch(DATAFILE_URL).then(res => res.json());
const eventvisor = createInstance({
datafile: datafile,
});

Attributes

Attributes are the key-value pairs that are used to describe the environment your application runs in.

Setting attribute value

eventvisor.setAttribute("deviceId", "device-123");

Getting attribute value

If you wanted to get the value of an already set attribute:

const deviceId = eventvisor.getAttribute("deviceId");

Getting all attribute values

const attributes = eventvisor.getAttributes();

Check if attribute is set

const isSet = eventvisor.isAttributeSet("deviceId");

Remove an attribute

Remote the already set value of an attribute:

eventvisor.removeAttribute("deviceId");

Events

Events are the structured data that we want to track in our application.

Tracking an event

eventvisor.track("pageView", {
url: "https://www.yoursite.com/home",
});

Modules

Modules are what helps expand the capabilities of the SDK by adding additional features to it, which ranges from transports to handlers, and also lookups.

Installing a module

Unless you are creating one manually, you will most likely be installing one from npm:

Command
$ npm install --save @eventvisor/module-console

Setting up a module

And then set it up when initializing the SDK:

your-app/index.js
import { createInstance } from "@eventvisor/sdk";
import { createConsoleModule } from "@eventvisor/module-console";
const eventvisor = createInstance({
modules: [
createConsoleModule(),
],
});

Registering a module later

You can also register a module after the SDK instance is created:

eventvisor.registerModule(createConsoleModule());

Remove a module

You can remove a module by passing the module name to the removeModule method:

eventvisor.removeModule("console");

Setting datafile

You may periodically fetch the latest datafile from your CDN without having to restart the application, and then set it to the existing SDK instance:

eventvisor.setDatafile(newlyFetchedDatafile);

You may do it either:

  • periodically like every 10 minutes, or
  • upon receiving some notification via, let's say, a WebSocket connection

Logging

By default, Eventvisor SDK will print out its own warnings and errors to the console.

Levels

These are all the available log levels for the SDK itself:

  • error
  • warn
  • info
  • debug

Customizing levels

If you choose debug level to make the logs more verbose, you can set it at the time of SDK initialization.

Setting debug level will print out all the logs, including info, warn, and error levels.

your-app/index.js
import { createInstance, createLogger } from "@eventvisor/sdk";
const eventvisor = createInstance({
logger: createLogger({
level: 'debug',
})
})

Or, you can set the logLevel directly:

your-app/index.js
import { createInstance } from "@eventvisor/sdk";
const eventvisor = createInstance({
logLevel: 'debug',
});

You can also set log level from SDK instance afterwards:

eventvisor.setLogLevel("debug");

Revision

To know the revision of the current datafile that is being used by the SDK:

const revision = eventvisor.getRevision();
Previous
SDKs