Documentation Index
Fetch the complete documentation index at: https://docs.grainql.com/llms.txt
Use this file to discover all available pages before exploring further.
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:- 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. Useidentify() to associate events with a specific user:
Event Lifecycle
Understanding what happens to events helps you track effectively:- Track: You call
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 Tag ensures events are not lost, even when:- Network is slow or fails temporarily
- User closes the browser
- App crashes
beforeunload, pagehide, and visibilitychange events and uses the Beacon API to flush pending events. You do not need to add your own event listeners for page exit — this is handled for you out of the box.
Common Patterns
Page View Tracking
Grain Tag auto-tracks page views (including SPA navigations via the History API), so you typically do not need to track these manually. If you need custom page view events:Button Click Tracking
Form Submission
Debug Mode
Not sure if events are sending? Enable debug mode in two ways: Via URL parameter — add?grain_debug=1 to any page URL. This enables debug logging in the browser console without changing code.
Via config:
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
These examples use
@grainql/tag. If you’re using @grainql/analytics-web, replace imports with import { createGrainAnalytics } from '@grainql/analytics-web' and use grain.track() on the client instance.