Analytics and Tracking

Analytics and Tracking

Once you have an instance of a prompt, you can also use it for tracking arbitrary data tied to the prompt:

prompt.track('answer-displayed', {
  success: true,
  userId: 123,
});

The data is available for analysis on your dashboard by selecting a specific prompt and then switching to the Events tab.

By default, every prompt execution tracks the following information:

  • Input: stores the value for each template variable used on this execution.
  • Render: stores the prompt name or id.
  • Completion: stores the execution configuration and the resulting output.

Full listing

import { z } from 'zod';
import { Promptd } from '@promptd-js/client';
import { OpenAIChatApi } from 'llm-api';
 
const promptdApiKey = 'xxx'; // Grab this in your Promptd project settings page.
const promptSlug = 'say-hello-to-user'; // Find this on the prompt page in the Promptd dashboard.
 
const promptd = new Promptd({ apiKey: promptdApiKey });
const template = promptd.template(promptSlug); // `template` is a Mustache template
 
// We're using Open AI's GPT4 model in this example, but we're LLM-agnostic:
const openAiKey = 'yyy'; // Grab this in your Open AI account.
const ai = new OpenAIChatApi({ apiKey: openAiKey },{ model: 'gpt-4-turbo' });
 
export const sayHello = async(name: string) => {
  // For example, the prompt body might be: "Say hello to {{ name }} in a cheerful way."
  const prompt = await template.render({ name });
 
  // If we called sayHello('Alice'), prompt would be "Say hello to Alice in a cheerful way."
  const result = await prompt.generate({
    schema: z.object({
        message: z.string().describe("The message to be sent to user."),
    }),
  }, ai);
 
  // Track data message-created
  prompt.track(
    promptSlug,
    'message-created',
    {
      success: true,
      userId: 123,
    }
  );
 
  // The result might now contain something akin to "Hello, Alice, what a pleasure to meet you!"
  return result.data.message;
};