Skip to main content
Before you can start your A/B tests within the Order Protection app, you need to configure a few settings.
1

Enable a New Custom Pixel

From Settings, click on Customer Events.Once in the Customer Events settings, click Add custom pixel. Name it “Order Protection A/B Testing” or something similar to know the testing is originating in the OP app.
2

Add the Code Script

Once the pixel is added, add the custom script below.
analytics.subscribe('checkout_completed', async event => {

const STORE_URL = 'kysonteststore4.myshopify.com';
const API_BASE = 'https://api.production.orderprotection.com/v1';

function transformSettings(settings) {
return {
variants: settings.variants,
settings: {
default*op: settings.automaticallyAddToCart,
dynamic_price: true,
dynamic_price_range: settings.rules.map(rule => {
const term = rule.terms.customer || rule.terms.brand;
return {
value: rule.min * 100,
max: rule.max \_ 100,
insuranceCost: term.type === 'percentage' ? term.amount : term.amount \* 100,
isPercentage: term.type === 'percentage',
paidBy: rule.terms.customer ? 'customer' : 'brand',
};
}),
splitTestSettings: settings.splitTestSettings || null,
},
};
}

function extractSplitTestFromWidgetConfig(widgetConfig) {
const configArray = Array.isArray(widgetConfig) ? widgetConfig : [widgetConfig];
const configWithSplitTest = configArray.find(c => c?.splitTestSettings?.token);
return configWithSplitTest?.splitTestSettings || null;
}

async function fetchWidgetConfig(widgetType) {
const response = await fetch(`${API_BASE}/widget/config?id=${STORE_URL}&type=${widgetType}`);
if (!response.ok) return null;
const widgetConfig = await response.json();
return extractSplitTestFromWidgetConfig(widgetConfig);
}

async function checkWidgetViewed(testId, token) {
const response = await fetch(`${API_BASE}/tests/${testId}/${token}`);
return response.ok;
}

async function getWidgetStyleSplitTest(testId) {
const widgetTypes = ['CHECKOUT', 'CART'];
const results = await Promise.allSettled(widgetTypes.map(fetchWidgetConfig));
const splitTestSettings = results.find(r => r.status === 'fulfilled' && r.value)?.value || null;

if (!splitTestSettings?.token) return null;

const wasViewed = await checkWidgetViewed(testId, splitTestSettings.token);
if (!wasViewed) return null;

return splitTestSettings;

}

async function getNonWidgetStyleSplitTest(testId) {
const storeResponse = await fetch(`${API_BASE}/quote/insurance?store_url=${STORE_URL}`);
if (!storeResponse.ok) return null;

const rawStoreSettings = await storeResponse.json();
const store_settings = transformSettings(rawStoreSettings);
const splitTestFromStore = store_settings?.settings?.splitTestSettings;

if (!splitTestFromStore?.token) return null;

const viewedWidgetResponse = await fetch(`${API_BASE}/tests/${testId}/${splitTestFromStore.token}`);
if (!viewedWidgetResponse.ok) return null;

return splitTestFromStore;

}

function sendTestEvent(splitTestSettings, orderId, purchasedOp) {
fetch(`${API_BASE}/tests/event`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
type: 'checkout:completed',
testId: splitTestSettings.id,
sessionId: splitTestSettings.token,
metadata: {
isOriginal: splitTestSettings.isOriginal,
settingsType: splitTestSettings.type,
orderId,
purchasedOp,
},
}),
}).catch(err => console.error('Failed to send test event:', err));
}

async function initSplitTest() {
try {
const testResponse = await fetch(`${API_BASE}/tests?store=${STORE_URL}`);
if (!testResponse.ok) return;

const hasSplitTest = await testResponse.json();
if (!hasSplitTest?.active || !hasSplitTest?.test) return;

const isWidgetStyleTest = hasSplitTest.test.type === 'WIDGET_STYLE';
const testId = hasSplitTest.test.id;

const splitTestSettings = isWidgetStyleTest ? await getWidgetStyleSplitTest(testId) : await getNonWidgetStyleSplitTest(testId);

if (!splitTestSettings?.token) return;

const purchasedOp = event.data.checkout.lineItems.some(
item => item.variant.product.vendor === 'Order Protection',
);
const orderId = event.data.checkout.order.id;

sendTestEvent(splitTestSettings, orderId, purchasedOp);
} catch (error) {
console.error('Error in initSplitTest:', error);
}

}

initSplitTest();
});

It should look something like this.Before you save the file, you need to swap out the following in quotations const STORE_URL = ‘kysonteststore4.myshopify.com’; with your Shopify store URL. The store URL can be found at the top of the left navigation in settings as well as at the top of the current custom pixel page you are populating. This URL must be your myshopify URL for it to work properly. The Shopify URL will be something similar to: {store_name}.myshopify.com. Save the custom pixel and you’re done! 🙌
3

Starting an A/B Test in the Order Protection App

Now that you have configured your custom pixel, you’re ready to start A/B testing. Log back in to app.orderprotection.com, navigate to to the A/B Tests tab in the left navigation and click on Start New Test to get started. Session data will start rolling into the test analytics within a few minutes. Expect 1-2 weeks for results to reach statistical significance depending on your store traffic volume.