getVisitorData

Access Visitor and Test Information

The getVisitorData() method provides comprehensive information about the current visitor and their test assignments. This synchronous method is essential for analytics integration, debugging, and understanding visitor behavior across your A/B tests.

Method Signature

getVisitorData(): VisitorData

Parameters

None - this method takes no parameters.

Returns

Type
Description

VisitorData

Object containing visitor information and test assignments (returned synchronously)

Return Structure

interface VisitorData {
  visitor: Visitor | null;
  visitorTests: TestRelation[];
}

interface Visitor {
  id: string;                        // Unique Shoplift visitor ID
  shopifyAnalyticsId: string | null; // Shopify's analytics visitor ID
  device: 'desktop' | 'mobile';      // Device type
  country: string | null;             // Two-letter country code (e.g., 'US', 'CA')
  createdAt: Date;                    // First visit timestamp
  storedAt: Date;                     // Last updated timestamp
  utmSource: string;                  // UTM source parameter
  utmMedium: string;                  // UTM medium parameter
  utmCampaign: string;                // UTM campaign parameter
  utmContent: string;                 // UTM content parameter
  referrer: string;                   // Referring URL
}

interface TestRelation {
  testId: string;                     // Unique test identifier
  hypothesisId: string;               // Hypothesis (variant) ID
  isThemeTest: boolean;               // Whether this is a theme test
  themeId?: number;                   // Shopify theme ID (if theme test)
  createdAt: Date;                    // Assignment timestamp
  isSaved: boolean;                   // Whether assignment is persisted
}

Basic Usage

// Get visitor data
const data = window.shoplift.getVisitorData();

// Access visitor information
if (data.visitor) {
  console.log('Visitor ID:', data.visitor.id);
  console.log('Device:', data.visitor.device);
  console.log('Country:', data.visitor.country);
}

// Access test assignments
data.visitorTests.forEach(test => {
  console.log('Test ID:', test.testId);
  console.log('Hypothesis ID:', test.hypothesisId);
  console.log('Assigned at:', test.createdAt);
});

Last updated

Was this helpful?