Appearance
JavaScript Event Tracking API Documentation
This document provides instructions on how to integrate our event tracking service into your JavaScript application. We provide two main functions: identifyCustomer for user identification, and trackEvent for tracking specific events.
Setup
First, ensure you have the necessary authentication token. This token should be securely stored as an environment variable in your application.
javascript
// In your .env file or environment configuration
EVENT_TRACKING_AUTH_TOKEN = your_auth_token_here;API Functions
1. identifyCustomer
Use this function when a user signs in, signs up, or completes onboarding. It helps to associate user information with subsequent events.
javascript
/**
* Identifies a customer with their details and additional properties.
*
* @param {string} customerId - The unique identifier of the customer.
* @param {string} name - The name of the customer.
* @param {string} email - The email address of the customer.
* @param {Object} properties - An object containing additional properties of the customer.
* @returns {Promise<void>} A promise that resolves when the customer is successfully identified.
* @throws {Error} Will throw an error if the API request fails.
*/
export async function identifyCustomer(customerId, name, email, properties) {
try {
const IDENTIFY_API_URL = "https://events.obsidianlaunch.co/customer/test";
const token = process.env.EVENT_TRACKING_AUTH_TOKEN;
if (!token) {
console.error("Event tracking auth token is not set");
return;
}
const initOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
};
const body = {
customer_id: customerId,
name: name,
email: email,
properties: properties,
};
initOptions.body = JSON.stringify(body);
const res = await fetch(IDENTIFY_API_URL, initOptions);
console.log(await res.json());
if (!res.ok) {
const message = `An error has occurred: ${res.status} - ${res.statusText}`;
throw new Error(message);
}
} catch (err) {
console.error("Failed to identify customer:", err);
throw err;
}
}Usage
javascript
import { identifyCustomer } from "./your-event-tracking-module";
// When a user signs up or logs in
try {
await identifyCustomer("user123", "John Doe", "[email protected]", {
plan: "premium",
signupDate: "2023-06-25",
});
console.log("Customer identified successfully");
} catch (error) {
console.error("Failed to identify customer:", error);
}2. trackEvent
Use this function to track specific events anywhere in your code.
javascript
/**
* Tracks an event for a specific customer.
*
* @param {string} customerId - The unique identifier of the customer.
* @param {string} eventName - The name of the event being tracked.
* @returns {Promise<void>} A promise that resolves when the event is successfully tracked.
* @throws {Error} Will throw an error if the API request fails.
*/
export async function trackEvent(customerId, eventName) {
try {
const TRACKING_API_URL = "https://events.obsidianlaunch.co/events/test";
const token = process.env.EVENT_TRACKING_AUTH_TOKEN;
if (!token) {
console.error("Event tracking auth token is not set");
return;
}
const initOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
};
const body = {
customer_id: customerId,
event: eventName,
};
initOptions.body = JSON.stringify(body);
const res = await fetch(TRACKING_API_URL, initOptions);
console.log(await res.json());
if (!res.ok) {
const message = `An error has occurred: ${res.status} - ${res.statusText}`;
throw new Error(message);
}
} catch (err) {
console.error("Failed to track event:", err);
throw err;
}
}Usage
javascript
import { trackEvent } from "./your-event-tracking-module";
// Tracking a specific event
try {
await trackEvent("user123", "button_click");
console.log("Event tracked successfully");
} catch (error) {
console.error("Failed to track event:", error);
}Best Practices
User Identification: Always call
identifyCustomerwhen a user signs up, logs in, or completes onboarding. This ensures that all subsequent events are associated with the correct user profile.Consistent Naming: Use consistent naming conventions for your events across your application. This will make analysis easier later on.
Relevant Data: When tracking events, include all relevant information in the event name. For the
identifyCustomerfunction, use thepropertiesobject to include any additional user information that might be useful for analysis.Error Handling: Both functions will throw an error if the API request fails. Always wrap the function calls in a try-catch block to handle potential errors gracefully.
Asynchronous Operations: Both functions return a Promise. Use
async/awaitor.then()/.catch()to handle the asynchronous operations properly.Security: Never expose your authentication token in client-side code. If you're using these functions in a browser environment, consider setting up a proxy server to handle the API calls and token management securely.
Troubleshooting
If you encounter any issues:
- Ensure your authentication token is correctly set and valid.
- Check your network connection.
- Verify that you're passing the correct parameters to the functions.
- Review the error messages logged to the console for more information.
For any additional help or questions, please contact our support team at [email protected]
