Page cover

Quick Start

Getting started with Telemetry is straightforward! Once installed, you can immediately use its powerful features with just a few lines of code. Below, you'll find step-by-step instructions and code examples to help you integrate and leverage the package effectively in your project.

import { YofiTelemetry, LogLevel, AvailableSensors} from '@yofi-ai/telemetry-web-sdk'

const publicToken = 'YOUR-PUBLIC-TOKEN';

YofiTelemetry.initialize({
    publicToken: publicToken, 
    // journeyIdSalt used to generate journey id. If you have different websites, you can use different Salts.
    journeyIdSalt: 'yofiscakes.myshopify.com'
});

// Initial labels if any
const initLabels = {
    'some-label': 'some-value'
};

const session = YofiTelemetry.startSession({
    duration: 900 * 1000, // 15mins
    labels: initLabels,   // Optional
    networkTelemetryConfig: {
        ip: true
    }
});

For single page application(SPA), please use this to stopAllSessions to terminate all existing sessions and initiate a new one when route changes, set the stopAllSessions parameter of startSession to true.

const session = YofiTelemetry.startSession(yourSessionConfig, true);

Starting a New Journey

To start a new journey, follow the steps below. You can set labels and other configuration options via yourSessionConfig.

Note: Before calling startNewJourney, make sure you have already called YofiTelemetry.initialize.

const session = await YofiTelemetry.startNewJourney(yourSessionConfig);

Session Labels

You can add labels to a session, the value of each label must be string.

const existingLabels = session.getLabels();

const newLabels = {
    'new-label': 'new-value'
};
// Note: Starting from v0.0.41, addLabels returns a Promise object.
// If an exception occurs, it will throw an Error object.
await session.addLabels(newLabels);

You can wait for the result of addLabels using the following approachs.


// Async/await approach
async function asyncApproach(newLabels) {
    try {
        await session.addLabels(newLabels);
        // Label addition completed successfully
    } catch(error) {
        // Handle error thrown during label addition
    }
}

// Promise approach
function promiseApproach(newLabels) {
    session.addLabels(newLabels)
    .then(() => {
        // Label addition success
    })
    .catch((error) => {
        // Handle error from label addition
    });
}

Connect Your User to Journey

To connect your user to Yofi Journey, you can simply add a userId label

Default bundleGenerationInterval is 2s

client_id, journey_id and __page_url are labels reserved by the SDK, please do not use them.

Last updated

Was this helpful?