# Analytics Platforms (Custom)

Connect Shoplift's powerful A/B testing data with your existing analytics stack to create a unified view of your customer journey and test performance.

### Overview

Shoplift's `getVisitorData` method provides a flexible way to send test participation and variant data to any third-party analytics platform. This enables you to analyze test results alongside your existing metrics, create custom segments based on test exposure, and build comprehensive attribution models that include A/B test data.

### Why Integrate Third-Party Analytics?

While Shoplift provides robust built-in analytics, integrating with your existing analytics tools allows you to:

* **Unify your data**: Combine test data with other customer touchpoints in a single dashboard
* **Create advanced segments**: Build audiences based on test participation and behavior
* **Enhance attribution**: Understand how tests impact downstream metrics not tracked in Shoplift
* **Leverage existing workflows**: Use familiar tools and reports your team already relies on

### Core Integration Pattern

The foundation of any custom integration is the `getVisitorData` method, which returns information about the current visitor's test participation and variant assignments.

#### Basic Implementation

Add this code to your theme's main JavaScript file or in a script tag in your `theme.liquid` file:

```javascript
// Wait for Shoplift to be available
function initializeShopliftTracking() {
  if (window.shoplift && window.shoplift.getVisitorData) {
    sendTestDataToAnalytics();
  } else {
    // Retry if Shoplift isn't ready yet
    setTimeout(initializeShopliftTracking, 100);
  }
}

function sendTestDataToAnalytics() {
  // Get comprehensive visitor data
  const visitorData = window.shoplift.getVisitorData();
  
  if (!visitorData.visitor) {
    // No visitor data available yet
    return;
  }
  
  // Process each test assignment
  visitorData.visitorTests.forEach(test => {
    // Send to your analytics platform
    trackTestExposure({
      // Test information
      testId: test.testId,
      hypothesisId: test.hypothesisId,
      isThemeTest: test.isThemeTest,
      
      // Visitor information
      visitorId: visitorData.visitor.id,
      device: visitorData.visitor.device,
      country: visitorData.visitor.country,
      
      // Attribution data
      utmSource: visitorData.visitor.utmSource,
      utmMedium: visitorData.visitor.utmMedium,
      utmCampaign: visitorData.visitor.utmCampaign,
      
      // Timing
      assignedAt: test.createdAt
    });
  });
}

// Initialize on page load
document.addEventListener('DOMContentLoaded', initializeShopliftTracking);
```

#### Understanding the Visitor Data Structure

The `getVisitorData()` method returns a rich data object:

```javascript
{
  visitor: {
    id: "550e8400-e29b-41d4-a716-446655440000",  // Unique visitor ID
    shopifyAnalyticsId: "sh-123abc...",          // Shopify analytics ID (if available)
    device: "mobile",                             // Device type: 'desktop' or 'mobile'
    country: "US",                                // ISO country code from GeoIP
    createdAt: Date,                              // First visit timestamp
    storedAt: Date,                               // Last updated timestamp
    utmSource: "google",                          // UTM parameters
    utmMedium: "cpc",
    utmCampaign: "summer_sale",
    utmContent: "variant_a",
    referrer: "https://google.com"               // Original referrer
  },
  visitorTests: [
    {
      testId: "test_abc123",                     // Unique test identifier
      hypothesisId: "hyp_def456",                // Specific variant ID
      isThemeTest: false,                        // Whether it's a theme test
      themeId: null,                              // Theme ID if applicable
      createdAt: Date,                            // Assignment timestamp
      isSaved: true                               // Whether assignment is persisted
    }
    // Additional test assignments...
  ]
}
```
