# Segment

```javascript
// Eraya + Segment Integration
function initializeErayaSegment() {
  if (window.Eraya && window.analytics) {
    const sdk = window.Eraya.getSDK();
    
    if (sdk) {
      const userManager = sdk.getUserManager();
      const erayaUserId = userManager.getErayaUserId();
      
      // Get all active experiments
      const activeTests = sdk.getActiveExperiments();
      
      if (Object.keys(activeTests).length > 0) {
        // Track experiment exposures
        Object.entries(activeTests).forEach(([testId, variationId]) => {
          analytics.track('Experiment Viewed', {
            experiment_id: testId,
            variant_id: variationId
          });
        });
        
        // Identify user with experiment data
        const traits = {
          eraya_user_id: erayaUserId,
          eraya_visitor_type: userManager.getVisitorType(),
          eraya_country: userManager.getUserCountry()
        };
        
        // Add each test as a trait
        Object.entries(activeTests).forEach(([testId, variationId]) => {
          traits[`eraya_test_${testId}`] = variationId;
        });
        
        analytics.identify(erayaUserId, traits);
      }
    }
  } else {
    setTimeout(initializeErayaSegment, 100);
  }
}

// Initialize on page load
initializeErayaSegment();
```
