What Are Events?
Events represent actions that happen in your application. Every time a user clicks a button, views a page, or completes a purchase, you can track it as an event. Think of events as a timeline of what users do in your app. By tracking these actions, you can:- Understand which features users engage with most
- Identify where users drop off in your funnel
- Measure the impact of changes to your product
Basic Event Tracking
The simplest way to track an event is with a name and properties:- Event name (
button_clicked): What happened - Properties (
button_name,page): Context about the event
Event Properties
Properties add context to events. They answer questions like “which button?” or “how much?”:- Include relevant context (IDs, names, amounts)
- Use consistent naming (snake_case or camelCase)
- Avoid sensitive data (passwords, credit cards)
- Keep property names simple and clear
Automatic Batching
Here’s where Grain saves you effort: events are automatically batched for efficiency. What is batching? Instead of sending each event immediately (slow and expensive), Grain collects events and sends them in groups.- Events batch every 5 seconds
- Or when 50 events accumulate
- Whichever comes first
Manual Flushing
Sometimes you need to send events immediately:- Before page navigation
- Critical events (purchases, signups)
- Before app closes or logs out
- In serverless functions
flush: true to track():
User Association
Events are more valuable when you know who performed them. Set a user ID to associate events with a specific user:Event Lifecycle
Understanding what happens to events helps you track effectively:- Track: You call
grain.track() - Queue: Event added to internal queue
- Batch: Queue accumulates until batch size or interval
- Send: Batch sent to Grain API
- Retry: If failed, retry with exponential backoff
- Success: Event stored in Grain analytics
Reliable Delivery
Grain ensures events aren’t lost, even when:- Network is slow or fails temporarily
- User closes the browser
- App crashes
Common Patterns
Page View Tracking
Button Click Tracking
Form Submission
Debug Mode
Not sure if events are sending? Enable debug mode:Next Steps
User Identification
Learn to track users across sessions
User Properties
Add attributes to user profiles
Template Events
Use pre-built event methods
API Reference
See all tracking methods
View your events in real-time at grainql.com/dashboard