# Heap

Track Shoplift test variants in Heap to leverage automatic event capture and retroactive analysis capabilities.

### Quick Start

Add this code after your Heap snippet:

```javascript
// Heap + Shoplift Integration
function initializeShopliftHeap() {
  if (window.shoplift && window.heap) {
    const visitorData = window.shoplift.getVisitorData();
    
    if (visitorData.visitor && visitorData.visitorTests.length > 0) {
      // Add user properties (works on both anonymous and identified users)
      const properties = {
        shoplift_visitor_id: visitorData.visitor.id,
        shoplift_device: visitorData.visitor.device,
        shoplift_first_seen: visitorData.visitor.createdAt.toISOString()
      };
      
      // Only add country if present
      if (visitorData.visitor.country) {
        properties.shoplift_country = visitorData.visitor.country;
      }
      
      // Add test assignments as properties
      visitorData.visitorTests.forEach(test => {
        properties[`test_${test.testId}`] = test.hypothesisId;
        properties[`test_${test.testId}_type`] = test.isThemeTest ? 'theme' : 'element';
        properties[`test_${test.testId}_assigned`] = test.createdAt.toISOString();
      });
      
      heap.addUserProperties(properties);
      
      // Track test exposure events
      visitorData.visitorTests.forEach(test => {
        heap.track('Shoplift Test Exposure', {
          test_id: test.testId,
          variant_id: test.hypothesisId,
          is_theme_test: test.isThemeTest
        });
      });
    }
  } else {
    setTimeout(initializeShopliftHeap, 100);
  }
}

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

### Retroactive Analysis

Heap's automatic event capture means you can:

#### Define Events After the Fact

Create new events in Heap's UI and immediately analyze them by test variant without requiring code changes.

#### Build Test-Specific Funnels

1. Go to **Analyze > Funnels**
2. Add your conversion steps
3. Filter by `test_[test_id]` property
4. Compare conversion rates across variants

#### Create Variant Cohorts

1. Navigate to **Users > Segments**
2. Create segments based on test properties
3. Analyze long-term behavior differences

### Visual Labeling

Use Heap's visual labeling to:

1. Tag elements involved in your tests
2. Create events specific to test interactions
3. Track engagement with variant-specific elements

### Best Practices

1. **Use consistent property naming** (`test_` prefix for all test properties)
2. **Include assignment timestamps** to analyze time-based effects
3. **Track both automatic and manual allocation** tests
4. **Create saved reports** for each major test
5. **Export data via API** for custom analysis
