Skip to content

Javascript (Web)

Here we'll cover how to collect events from your client side web application. Our Javascript SDK is designed to be non-blocking and fast, meaning it won't get in the way of your app's performance. Here's how to get started:

Install

Firstly, install Trubrics in your project with:

npm i @trubrics/trubrics

Getting started in Javascript

Initialize the Trubrics SDK in your app:

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

export const trubrics = new Trubrics({apiKey: TRUBRICS_API_KEY});

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.

Now track your events with:

trubrics.track({
    event: "Prompt",
    user_id: "user_id",
    properties: {
        $text: "tell me a story",
        $thread_id: "1234",
        a_custom_property: "a custom value",
    },
});
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. For example, a "Generation" event could have properties "Cost of generation" or "Prompt template". Trubrics properties are prefixed with a $. no

If you have not implemented auth, use a UUID to assign events to an anonymous user.

Trubrics properties

Any property that begins with a $ is a property recognised by Trubrics. This allows us to perform certain actions in the ingestion pipeline and backend. Here are a list of Trubrics properties:

  • $text: events that contain a textual element should use this property, as it allows Trubrics to tag the event with text attributes during ingestion. Text attributes can be managed directly in Trubrics.

  • $thread_id: to identify when a user is interacting with an AI assistant within their session. This allows you to distinguish between non-AI assistant interactions such as "Sign In" and events within a thread such as "Prompt" and "Generation". With an LLM powered assistant, this ID should be unique for each different conversation.

  • $assistant_id: events that are made by an assistant, such as "Generation" or "Query database". Typically for LLM assistants this can be set to the model name, such as "GPT-4" or "GPT-3.5-turbo".