Skip to content

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:

  1. Type safety for the properties
  2. Centralized state management
  3. 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 ​

  1. Keep it Minimal: Only include properties that are truly needed for every event
  2. Use functions for Dynamic Values: Set properties as functions to be re-evaluated for every event
  3. Consider Performance: Avoid expensive computations for global properties
  4. Use Meaningful Names: Choose clear, descriptive names for global properties