Appearance
Every Event Properties ​
Learn how to add properties that should be included with every event tracked.
Using Groups for Global Properties ​
Groups are an excellent way to manage properties that should be included with every event. They provide:
- Type safety for the properties
- Centralized state management
- Automatic inclusion with all events
Implementation Example ​
typescript
import { createAnalyticsTracker } from 'voltage-schema';
import { AnalyticsTracker, TrackerEvents, trackingConfig } from './__analytics_generated__/analytics';
// Create a group for global properties in your schema
// groups/global.volt.yaml
/*
groups:
- name: Global
description: Properties included with every event
properties:
- name: Page Url
description: Current URL of the page
type: string
- name: Environment
description: Runtime environment
type: [production, staging, development]
*/
const tracker = createAnalyticsTracker<TrackerEvents>(trackingConfig, {
onEventTracked: (eventName, { properties, groups }) => {
// Global properties are automatically included
const eventData = {
...properties,
...groups.Global
};
// Send to your analytics service
analytics.track(eventName, eventData);
}
});
// Set global properties once
tracker.setProperties('Global', {
// Function are re-evaluated for each event
'Page Url': () => window.location.href,
Environment: 'production'
});
// Every subsequent event will include these properties
tracker.track('page_view', {
'Page Name': 'Home'
}); // Will include Page Url and Environment
Best Practices ​
- Keep it Minimal: Only include properties that are truly needed for every event
- Use functions for Dynamic Values: Set properties as functions to be re-evaluated for every event
- Consider Performance: Avoid expensive computations for global properties
- Use Meaningful Names: Choose clear, descriptive names for global properties