Skip to content

Javascript

Here we'll cover how to collect events from your Javascript application. Our Javascript library is designed to be non-blocking and fast, meaning it won't get in the way of your app's performance. It is compatible with client side Javascript as well as server side Node.js. Here's how to get started:

Install

Firstly, install Trubrics in your project with:

npm i @trubrics/trubrics

Initialise Trubrics

Then, initialize the Trubrics SDK in your app:

import { Trubrics } from "@trubrics/trubrics";

export const trubrics = new Trubrics({ 
    apiKey: TRUBRICS_API_KEY,
    flushInterval: 10000,
    flushAt: 20,
    isVerbose: false
});

Project API key

Your trubrics API key is unique to your project. Login to Trubrics, and copy your project API key from the settings page. This is a write only API key that allows you to push events.

Parameter Type Description Required
apiKey string Your project API key. yes
flushInterval number Time in ms between automatic flushes (default: 10000) no
flushAt number Number of events that trigger a flush (default: 20) no
isVerbose boolean Flag to enable verbose logging (default: false) no

Track user events

Finally, track any user action events with:

trubrics.track({
    event: "Sign Up",
    user_id: "user_id",
    properties: {
        country: "USA",
        company: "Acme",  // Add more properties as needed
    },
    timestamp: new Date()
});
Parameter Type Description Required
event string The name of the event you want to track. yes
user_id string The distinct ID of the user that is signed in to your app. yes
properties object A list of properties of the event. These can be your user properties (e.g. company name) or additional properties of the event (e.g. request latency). Setting properties will allow you to filter and slice analysis in different ways. no
timestamp Date The timestamp of the event. This defaults to the current timestamp no

Track LLM events

In addition to regular events, you may track LLM events (prompts and generations) with:

trubrics.trackLLM({
    user_id: "user_id",
    prompt: "What is Trubrics?",
    assistant_id: "gpt-4o",
    generation: "Trubrics is the leading product analytics platform for AI applications.",
    properties: {
        context: "Chat window",
        $thread_id: "thread_123"  // Trubrics property to group events by conversation thread
    },
    timestamp: new Date(),
    latency: 2
});
Parameter Type Description Required
user_id string The distinct ID of the user that is signed in to your app. yes
prompt string The user's message. yes
assistant_id string The AI assistant's ID, typically the model name. yes
generation string The assistant's response. yes
properties Record<string, any> A list of properties for both prompts and generations. A single reserved property is $thread_id which groups events by conversation thread. Otherwise these can be your custom properties (e.g. cost, number of tokens). Setting properties will allow you to filter and slice analysis in different ways. no
timestamp Date The timestamp of the generation. This defaults to the current timestamp no
latency number The time in seconds between the prompt and generation no

User events grouped by session

Based on every user event, Trubrics generates a session_id. This is a unique identifier for the session that the user is interacting with.

This is different to the $thread_id property, which groups events by conversation thread. A user may have multiple conversations with your AI within a single session, and each conversation will have a unique thread ID.

The session ID is generated based on the events that are tracked for a user. If a user has not interacted with your app for more than 30 minutes, the session will be timed out and a new session ID will be generated.