JavaScript API Testing

Overview

The Shoplift JavaScript API enables you to create sophisticated A/B tests directly in your theme code, giving you complete control over test logic and visitor experiences. While Shoplift's template, theme, and URL tests handle many use cases, the JavaScript API is perfect for:

  • Interactive elements (mini carts, navigation menus, popups)

  • Complex tests with custom logic

  • User-triggered experiences that shouldn't load immediately

  • Integration with third-party apps and custom analytics

  • Conditional content based on multiple factors

How the JavaScript API Works

Automatic Script Injection

Shoplift automatically injects and maintains its script in your theme's <head> section. You don't need to add any script tags manually - Shoplift handles this for you. The script:

  • Loads before other scripts to minimize flicker

  • Initializes automatically on page load

  • Provides the global window.shoplift object

  • Updates automatically when we release improvements

The window.shoplift Object

Once your page loads, you have access to three powerful methods:

window.shoplift = {
  isHypothesisActive(hypothesisId),  // Check test variant assignment
  setAnalyticsConsent(consent),      // Manage visitor consent
  getVisitorData()                    // Access visitor test data
}

JavaScript API vs Other Test Types

Understanding when to use the JavaScript API versus other Shoplift test types:

Use JavaScript API Tests When You Need:

  • Custom trigger logic - Show variants based on user actions (clicks, scrolls, form submissions)

  • Complex conditions - Combine multiple factors (device type + location + user behavior)

  • Dynamic content - Modify content that loads after page load (AJAX carts, React components)

  • Third-party integrations - Work with apps that need programmatic control

  • Performance optimization - Load test variations only when needed (manual allocation)

Use Other Test Types For:

  • Template tests - Simple content swaps in Liquid templates

  • Theme tests - Testing entirely different themes

  • URL tests - Redirecting to different pages or URLs

Test Triggers

JavaScript API tests support two test participation strategies (triggers):

Automatic Trigger

  • Visitors are assigned immediately when the page loads

  • Best for always-visible elements

  • Test runs even if JavaScript API isn't called

  • Use for: hero banners, global UI changes, chat widgets

Manual Trigger

  • Visitors are only assigned when isHypothesisActive() is called

  • Perfect for user-triggered interactions

  • Reduces unnecessary test participation

  • Use for: mini carts, dropdown menus, modal popups

You can choose your strategy when creating the test in Shoplift.

Shoplift automatically integrates with Shopify's Customer Privacy API. Only use the manual consent method if you have a custom consent solution:

// Only needed for custom consent managers
function updateConsent(userAccepted) {
  window.shoplift.setAnalyticsConsent(userAccepted)
    .then(() => console.log('Consent updated'))
    .catch(error => console.error('Consent update failed:', error));
}

For more information on how to use the setAnalyticsConsent method with custom consent managers, see Privacy and Compliance

Accessing Test Data

Retrieve visitor test assignments for analytics or debugging:

const visitorData = window.shoplift.getVisitorData();
console.log('Visitor ID:', visitorData.visitor?.id);
console.log('Active tests:', visitorData.visitorTests);

For more information on how to use the getVisitorData method to send Shoplift test data to third-party analytics platforms, see Analytics Platforms (Custom)

Debugging Tools

URL Parameters

  • ?slVariant=hypothesis-id - Forces specific hypothesis to return true

  • ?isShopliftMerchant=true - Disables all Shoplift functionality

Console Commands

// Check current state
window.shoplift.getVisitorData()

// Test specific hypothesis
await window.shoplift.isHypothesisActive('your-id')

Last updated

Was this helpful?