Web (Browser)

Track your data via the Web (Browser) SDK for Vemetric.


The Web (Browser) SDK is the preferred way to integrate Vemetric into your modern web app with first-class TypeScript support.

Below you can see how to initialize and configure the SDK, as well as all the available functions.

Installation

Install the Vemetric NPM package

In your project directory, run:

npm install @vemetric/browser

Initialize Vemetric

Import and initialize Vemetric in your application’s startup code (for example, in your main JavaScript entry file or within a React/Angular initialization):

import { vemetric } from '@vemetric/browser';

vemetric.init({
  token: 'YOUR_PROJECT_TOKEN',
});

Replace YOUR_PROJECT_TOKEN with the unique token for your project (found in the project settings).

Configuration

You can pass additional options to the init function to configure Vemetric.

Look at the different options below for more details.

Example:

import { vemetric } from '@vemetric/browser';

vemetric.init({
  token: 'YOUR_PROJECT_TOKEN',
  host: 'https://hub.yourdomain.com',
  trackPageViews: true,
  trackOutboundLinks: true,
  trackDataAttributes: true,
  maskPaths: ['/project/*', '/project/*/users'],
});

Replace YOUR_PROJECT_TOKEN with the unique token for your project (found in the project settings).


host

Description

Here you can specify a different host for the Vemetric API.

Necessary if you want to use a proxy for tracking your data via your own domain.

Type
string
Example
"https://hub.yourdomain.com"

trackPageViews

Description

Whether to track page views automatically.

Listens to any kind of changes of the URL (hash, pathname, search, etc.) and records a page view event. If disabled, you need to track page views manually.

Type
boolean
Default
"true"

trackOutboundLinks

Description
Whether to track outbound link clicks.
Type
boolean
Default
"true"

trackDataAttributes

Description

Whether to listen to data-vmtrc attributes on elements for sending custom events.

Type
boolean
Default
"true"

maskPaths

Description

An array of paths to mask from the URL. Helpful if you mask sensitive information in the URL (e.g. user IDs) or group page views by a specific path (e.g. /project/*).

Type
"string[]"
Example
"['/project/*', '/project/*/users']"

Methods

Make sure to call the init function before calling any of the following methods.

trackEvent

The main method to track any kind of event.

import { vemetric } from '@vemetric/browser';

await vemetric.trackEvent('MyCustomEvent', {
  eventData: { key: 'value' },
});

The first argument is the event name. The second argument is optional and can be an object with the following properties (also both optional):

  • eventData: An object containing the meta data of the event so you can later filter by
  • userData: An object to update the user data. Only relevant for identified users.
    • set: An object containing the user data to set, overriding existing values.
    • setOnce: An object containing the user data to set once, only if the keys do not exist yet.
    • unset: An array of keys to remove from the user data.

Checkout the tracking custom events section to learn more.

identify

With this function you can identify a user. All following events will be associated with this user. Checkout the docs about User identification to learn more about how it works.

import { vemetric } from '@vemetric/browser';

await vemetric.identify({
  identifier: 'your-user-id', // we recommend to use the user's primary id in your database
  displayName: 'John Doe', // optional, will be used to display the users' name in Vemetric

  // If you have the consent of your authenticated users, you can allow cookies to ensure smoother tracking
  allowCookies: true,
});

The function takes the following parameters:

  • identifier: (Required): The unique identifier for the user. We recommend to use the user’s primary id in your database.
  • displayName: Optional: will be used to display the users’ name in Vemetric
  • allowCookies: Optional: Whether to use cookies to ensure smoother tracking.
  • data: Optional: An object to update the user data.
    • set: An object containing the user data to set, overriding existing values.
    • setOnce: An object containing the user data to set once, only if the keys do not exist yet.
    • unset: An array of keys to remove from the user data.

getUserIdentifier

Returns the identifier of the currently identified user. Returns null if no user is identified.

import { vemetric } from '@vemetric/browser';

const identifier = vemetric.getUserIdentifier();

updateUser

Updates the data of the currently identified user.

import { vemetric } from '@vemetric/browser';

vemetric.updateUser({
  set: { key1: 'value1' },
  setOnce: { key2: 'value2' },
  unset: ['key3']
});

The first parameter is an object with the following properties:

  • set: An object containing the user data to set, overriding existing values.
  • setOnce: An object containing the user data to set once, only if the keys do not exist yet.
  • unset: An array of keys to remove from the user data.

resetUser

Should be called when the user logs out. From then on, all events will be tracked as anonymous.

import { vemetric } from '@vemetric/browser';

await vemetric.resetUser();

enableTrackPageViews

A function to enable automatic page view tracking after initialization. Only needed if trackPageViews was disabled.

import { vemetric } from '@vemetric/browser';

vemetric.enableTrackPageViews();

A function to enable outbound link tracking after initialization. Only needed if trackOutboundLinks was disabled.

import { vemetric } from '@vemetric/browser';

vemetric.enableTrackOutboundLinks();

enableTrackDataAttributes

A function to enable data attribute tracking after initialization. Only needed if trackDataAttributes was disabled.

Learn more about tracking custom events via data attributes.

import { vemetric } from '@vemetric/browser';

vemetric.enableTrackDataAttributes();

trackPageView

A function to manually track a page view from the current URL. Only needed if trackPageViews was disabled.

import { vemetric } from '@vemetric/browser';

await vemetric.trackPageView();

trackPageLeave

A function to manually track a page leave. Only needed if trackPageViews was disabled.

import { vemetric } from '@vemetric/browser';

vemetric.trackPageLeave();

Resources

View the NPM Package of the Web (Browser) SDK.

View the source code of the Web (Browser) SDK on GitHub.

Track your data via the HTML Script for Vemetric.

Track your data via the Node.js SDK for Vemetric.

Pricing About Documentation Changelog Blog