Event Tracking Overview
Countly's event tracking system captures user interactions with flexible data structures. Track any action with counts, sums, durations, and custom segmentation.
Basic Event Tracking
Simple Events
Track occurrences of an action:
// Track a button click
Countly.add_event({
key: 'button_click',
count: 1
});
// Track with segmentation
Countly.add_event({
key: 'button_click',
count: 1,
segmentation: {
button_name: 'signup',
page: 'homepage'
}
});
Event with Sum
Track values like revenue or points:
Countly.add_event({
key: 'purchase',
count: 1,
sum: 49.99,
segmentation: {
product: 'Premium Plan',
currency: 'USD'
}
});
Event with Duration
Track time-based events:
Countly.add_event({
key: 'video_watch',
count: 1,
dur: 125, // Duration in seconds
segmentation: {
video_id: 'intro_tutorial',
quality: '1080p'
}
});
Timed Events
Automatic Duration Tracking
Start and end events to automatically calculate duration:
// Start timing
Countly.start_event('video_watch');
// ... video plays ...
// End and record with calculated duration
Countly.end_event({
key: 'video_watch',
segmentation: {
video_id: 'product_demo',
completed: true
}
});
Canceling Timed Events
// Start timing
Countly.start_event('checkout_flow');
// User cancels - don't record
Countly.cancel_event('checkout_flow');
Segmentation
Adding Context to Events
Segmentation provides dimensions for analysis:
Countly.add_event({
key: 'search',
count: 1,
segmentation: {
query: 'analytics tools',
results_count: 15,
filters_used: 'category,price',
page: 1
}
});
Standard Segments
Common segmentation patterns:
// E-commerce
segmentation: {
product_id: 'SKU123',
category: 'Electronics',
brand: 'TechBrand',
price_range: '100-500'
}
// Content
segmentation: {
content_type: 'article',
author: 'Jane Smith',
topic: 'Analytics',
word_count: 1500
}
// Features
segmentation: {
feature: 'export',
format: 'pdf',
success: true
}
E-commerce Tracking
Product Views
function trackProductView(product) {
Countly.add_event({
key: 'product_view',
count: 1,
segmentation: {
product_id: product.id,
product_name: product.name,
category: product.category,
price: product.price
}
});
}
Add to Cart
function trackAddToCart(product, quantity) {
Countly.add_event({
key: 'add_to_cart',
count: quantity,
sum: product.price * quantity,
segmentation: {
product_id: product.id,
product_name: product.name,
category: product.category
}
});
}
Purchase
function trackPurchase(order) {
Countly.add_event({
key: 'purchase',
count: order.items.length,
sum: order.total,
segmentation: {
order_id: order.id,
payment_method: order.payment,
coupon_used: order.coupon || 'none',
shipping_method: order.shipping
}
});
}
User Engagement Tracking
Feature Usage
function trackFeatureUse(feature, context) {
Countly.add_event({
key: 'feature_use',
count: 1,
segmentation: {
feature_name: feature,
context: context,
user_plan: getUserPlan()
}
});
}
Content Engagement
// Article reading
Countly.start_event('article_read');
// On completion or exit
Countly.end_event({
key: 'article_read',
segmentation: {
article_id: articleId,
scroll_depth: getScrollDepth(),
completed: didReachEnd()
}
});
Form Interactions
// Form started
Countly.add_event({
key: 'form_start',
segmentation: { form_name: 'signup' }
});
// Form submitted
Countly.add_event({
key: 'form_submit',
segmentation: {
form_name: 'signup',
fields_filled: 5,
time_spent: 45
}
});
// Form error
Countly.add_event({
key: 'form_error',
segmentation: {
form_name: 'signup',
error_field: 'email',
error_type: 'validation'
}
});
Mobile SDK Events
iOS (Swift)
// Simple event
Countly.sharedInstance().recordEvent("button_click")
// With segmentation
Countly.sharedInstance().recordEvent("purchase",
segmentation: ["product": "Premium", "price": "49.99"],
count: 1,
sum: 49.99)
Android (Kotlin)
// Simple event
Countly.sharedInstance().events().recordEvent("button_click")
// With segmentation
val segmentation = mapOf(
"product" to "Premium",
"price" to "49.99"
)
Countly.sharedInstance().events().recordEvent("purchase", segmentation, 1, 49.99)
React Native
Countly.recordEvent({
eventName: 'purchase',
eventCount: 1,
eventSum: 49.99,
segments: {
product: 'Premium',
price: '49.99'
}
});
Event Analysis
Viewing Events in Dashboard
- Navigate to Events section
- Select the event to analyze
- View count, sum, and duration metrics
- Break down by segmentation values
- Apply date ranges and filters
Creating Event-Based Funnels
- Go to Funnels
- Create new funnel
- Add events as steps:
- Step 1: product_view
- Step 2: add_to_cart
- Step 3: checkout_start
- Step 4: purchase
- Analyze conversion between steps
Best Practices
Event Naming
// Good: descriptive, hierarchical
'product_view'
'cart_add'
'checkout_complete'
'feature_export_pdf'
// Avoid: vague, inconsistent
'click'
'action1'
'UserDidSomething'
Segmentation Guidelines
- Use consistent property names
- Keep values categorical when possible
- Avoid high-cardinality values (use ranges instead)
- Don't include PII in segmentation
Performance
- Batch events when possible
- Avoid tracking every minor interaction
- Use timed events for duration tracking
- Queue events during offline periods