Node Client
Learn how to use Oppla’s Node.js client for server-side tracking and analytics.
Installation
Install the Oppla Node.js client:
npm install @oppla/node
# or
yarn add @oppla/node
Basic Usage
Initialize the Client
const Oppla = require('@oppla/node');
const oppla = new Oppla({
projectId: 'YOUR_PROJECT_ID',
apiKey: 'YOUR_API_KEY'
});
Track Events
// Track a simple event
await oppla.track({
event: 'Order Completed',
userId: 'user_123',
properties: {
orderId: 'order_123',
amount: 99.99
}
});
// Track with additional context
await oppla.track({
event: 'Payment Processed',
userId: 'user_123',
timestamp: new Date(),
context: {
ip: '192.168.1.1',
userAgent: 'Mozilla/5.0...'
},
properties: {
paymentId: 'pay_123',
amount: 99.99,
currency: 'USD'
}
});
User Management
Identify Users
await oppla.identify({
userId: 'user_123',
traits: {
name: 'John Doe',
email: 'john@example.com',
plan: 'premium',
signupDate: new Date()
}
});
Update User Properties
await oppla.identify({
userId: 'user_123',
traits: {
lastLogin: new Date(),
loginCount: 5
}
});
Batch Operations
Track Multiple Events
await oppla.batch([
{
event: 'Page Viewed',
userId: 'user_123',
properties: { path: '/products' }
},
{
event: 'Product Viewed',
userId: 'user_123',
properties: { productId: 'prod_123' }
}
]);
Identify Multiple Users
await oppla.batch([
{
identify: {
userId: 'user_123',
traits: { name: 'John Doe' }
}
},
{
identify: {
userId: 'user_456',
traits: { name: 'Jane Smith' }
}
}
]);
Error Handling
try {
await oppla.track({
event: 'Order Completed',
userId: 'user_123'
});
} catch (error) {
console.error('Failed to track event:', error);
// Handle specific error types
if (error.code === 'RATE_LIMIT') {
// Implement retry logic
}
}
Best Practices
- Use Environment Variables: Store API keys securely
- Implement Retry Logic: Handle rate limits and network issues
- Batch When Possible: Reduce API calls
- Validate Data: Ensure required fields are present
Next Steps