Events
Best practices for tracking events effectively.
Event Naming
Use clear, descriptive names that follow a consistent pattern:
- •
user_signup- User actions - •
button_click- UI interactions - •
purchase_completed- Business events
Metadata
Use the metadata field to store custom properties:
import { Serla } from './lib/serla';
const serla = new Serla('YOUR_API_KEY');
// Identify the user
serla.identify('user_123');
// Track with metadata
await serla.send('product_viewed', {
productId: 'prod_456',
category: 'electronics',
price: 299.99,
currency: 'USD'
});User Identification
Always include a userId when possible to track user behavior:
- •Use consistent user IDs across all events
- •Include sessionId to track user sessions
- •For anonymous users, generate a unique identifier
Retrieving Events
Query events programmatically using the GET API. Filter by user ID, event ID, session ID, or event name:
# Get events by user ID
curl -X GET "https://serla.dev/api/events/get?user_id=user_123&limit=10" \
-H "Authorization: Bearer YOUR_API_KEY"
# Get specific event by ID
curl -X GET "https://serla.dev/api/events/get?event_id=evt_abc123" \
-H "Authorization: Bearer YOUR_API_KEY"Query Parameters:
- •
event_id- Get a specific event by ID - •
user_id- Get all events for a specific user - •
session_id- Get all events in a session - •
event_name- Filter by event type - •
from- Start date (ISO 8601) - •
to- End date (ISO 8601) - •
limit- Max results (default: 100, max: 1000)
Note: At least one filter parameter (event_id, user_id, session_id, or event_name) is required.
Revenue Tracking
Track revenue from purchases, subscriptions, and payments. Serla automatically detects revenue in your event metadata:
import { Serla } from './lib/serla';
const serla = new Serla('YOUR_API_KEY');
// Identify user
serla.identify('user_123');
// Track purchase with revenue
await serla.send('purchase_completed', {
revenue: {
amount: 99.99,
currency: 'USD'
},
plan: 'pro',
product: 'analytics-platform'
});Serla supports multiple metadata structures for maximum flexibility:
- •
metadata.revenue.amount+metadata.revenue.currency - •
metadata.amount+metadata.currency - •
metadata.price+metadata.curr - •
metadata.value+metadata.currency
Flexible revenue tracking: When you send revenue events from any payment provider (Stripe, PayPal, Square, or custom), Serla automatically detects and extracts revenue data from multiple metadata structures, so you don't need to use a specific format.