Tracking Approaches
Countly supports both client-side and server-side tracking. Choose based on your requirements.
Client-Side Tracking
When to Use
- Web and mobile applications
- Real-time session tracking
- Automatic device/browser detection
- Crash reporting
- Push notifications
Implementation
// Browser
Countly.init({
app_key: 'YOUR_APP_KEY',
url: 'https://your-server.com'
});
Countly.track_sessions();
Countly.add_event({ key: 'button_click' });
Advantages
- Automatic device info collection
- Session management built-in
- Crash/error reporting
- Heatmaps and scrollmaps
- Real-time capabilities
Disadvantages
- Can be blocked by ad blockers
- Requires JavaScript
- Client-side code is visible
Server-Side Tracking
When to Use
- Backend events (API calls, webhooks)
- Ad blocker immunity
- Sensitive data processing
- Batch processing
- IoT/embedded devices
HTTP API
curl -X POST "https://your-countly-server.com/i" \
-d "app_key=YOUR_APP_KEY" \
-d "device_id=server_device_123" \
-d "events=[{\"key\":\"api_call\",\"count\":1}]"
Node.js SDK
const Countly = require('countly-sdk-nodejs');
Countly.init({
app_key: 'YOUR_APP_KEY',
url: 'https://your-server.com',
device_id: 'server_instance_1'
});
Countly.add_event({
key: 'backend_process',
count: 1,
segmentation: {
status: 'completed',
duration_ms: 150
}
});
Python
import requests
def track_event(device_id, event_key, count=1, segmentation=None):
payload = {
'app_key': 'YOUR_APP_KEY',
'device_id': device_id,
'events': json.dumps([{
'key': event_key,
'count': count,
'segmentation': segmentation or {}
}])
}
requests.post('https://your-countly-server.com/i', data=payload)
track_event('user_123', 'purchase', 1, {'product': 'premium'})
Advantages
- Cannot be blocked by browsers
- Full control over data
- Better for sensitive operations
- Works without JavaScript
Disadvantages
- No automatic session tracking
- Manual device info required
- More implementation effort
- No crash reporting
Hybrid Approach
Combine both for complete coverage:
// Client-side: User interactions
Countly.track_sessions();
Countly.add_event({ key: 'page_view' });
// Server-side: Critical events
app.post('/api/purchase', (req, res) => {
// Process purchase...
// Server-side tracking (guaranteed)
serverCountly.add_event({
key: 'purchase_confirmed',
sum: order.total,
segmentation: { order_id: order.id }
});
});
Recommendations
| Use Case | Approach |
|---|---|
| Web analytics | Client-side |
| Mobile apps | Client-side SDK |
| API events | Server-side |
| E-commerce conversions | Hybrid |
| Crash reporting | Client-side |
| Backend processes | Server-side |
| Privacy-sensitive | Server-side |