How to Log Events in Firebase Analytics: Simple Guide
To log an event in Firebase Analytics, use the
logEvent method on the Firebase Analytics instance, specifying the event name and optional parameters as key-value pairs. This records user actions for analysis in the Firebase console.Syntax
The logEvent method requires two parts: the event name as a string, and an optional object with parameters describing the event details.
- eventName: A string identifying the event, like
purchaseorlogin. - params: An optional object with key-value pairs giving extra info, such as
item_idorvalue.
javascript
firebase.analytics().logEvent(eventName, params);
Example
This example shows how to log a purchase event with details about the item bought and its price.
javascript
import { initializeApp } from 'firebase/app'; import { getAnalytics, logEvent } from 'firebase/analytics'; const firebaseConfig = { apiKey: "YOUR_API_KEY", authDomain: "YOUR_AUTH_DOMAIN", projectId: "YOUR_PROJECT_ID", storageBucket: "YOUR_STORAGE_BUCKET", messagingSenderId: "YOUR_SENDER_ID", appId: "YOUR_APP_ID", measurementId: "YOUR_MEASUREMENT_ID" }; const app = initializeApp(firebaseConfig); const analytics = getAnalytics(app); logEvent(analytics, 'purchase', { item_id: 'sku1234', item_name: 'T-shirt', value: 19.99, currency: 'USD' });
Output
Event 'purchase' logged with parameters {item_id: 'sku1234', item_name: 'T-shirt', value: 19.99, currency: 'USD'}
Common Pitfalls
Common mistakes when logging events include:
- Using event names or parameter keys that are too long or contain invalid characters. Event names must be under 40 characters and use only letters, numbers, and underscores.
- Not initializing Firebase Analytics before calling
logEvent. - Passing parameters in the wrong format, such as non-string keys or unsupported value types.
- Logging too many events or parameters, which can exceed Firebase limits.
javascript
/* Wrong: event name with spaces and special chars */ logEvent(analytics, 'purchase item', {value: 20}); /* Right: valid event name and parameters */ logEvent(analytics, 'purchase_item', {value: 20});
Quick Reference
| Item | Description | Example |
|---|---|---|
| Event Name | String identifier for the event, max 40 chars, letters/numbers/underscores only | purchase_item |
| Parameters | Optional object with key-value pairs describing event details | {item_id: 'sku1234', value: 19.99} |
| Initialization | Firebase app and analytics must be initialized before logging events | const analytics = getAnalytics(app); |
| Limits | Max 500 different event types and 25 parameters per event | Avoid excessive custom events |
Key Takeaways
Always initialize Firebase Analytics before logging events using logEvent.
Use valid event names with letters, numbers, and underscores only, max 40 characters.
Pass parameters as an object with simple key-value pairs for extra event details.
Avoid logging too many events or parameters to stay within Firebase limits.
Check Firebase console to verify events are recorded correctly.