Cart Drawer A/B testing not working as expected

Topic summary

A developer is attempting to implement A/B testing for a cart drawer using two snippet variants (cart-drawer and cart-drawer-variant) that should be dynamically rendered based on a randomly assigned variant (‘A’ or ‘B’) stored in localStorage.

Current Status:

  • Session tracking and CleverTap event pushing are working correctly
  • The actual rendering of the cart variants is failing
  • Code includes JavaScript for session ID generation, variant assignment, and Alpine.js integration
  • Two cart drawer divs exist with hidden classes, intended to display based on variant

Key Issue:
The cart variants are not visually rendering despite successful session/variant assignment and analytics tracking.

Response Provided:
A community member suggested:

  • Simplifying the code to create a minimal reproducible example
  • Removing frameworks and excess components to isolate the problem
  • Reviewing Shopify’s section rendering documentation
  • Considering professional services if advanced customization assistance is needed

Status: Unresolved - requires further debugging and potentially professional assistance.

Summarized with AI on November 1. AI used: claude-sonnet-4-5-20250929.

I wanted to do A/B test on my Cart, so i made two variants of cart-drawer, both of them being snippets, i created a section where i can dynamically render my carts,
i have a function in custom.js, attached the code below:
everything is working, sessions are being pushed to clevertap but the actual rendering is not happening.

// Function to initialize the cart drawer monitoring
function initCartDrawerMonitoring() {
console.log(“Initializing cart drawer monitoring…”);

// Generate and retrieve the session ID
function generateSessionID() {
return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0;
const v = c === ‘x’ ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}

// Retrieve or generate session ID
let sessionID = sessionStorage.getItem(‘sessionID’);
if (!sessionID) {
sessionID = generateSessionID();
sessionStorage.setItem(‘sessionID’, sessionID);
console.log(Session ID generated: ${sessionID});
} else {
console.log(Session ID retrieved: ${sessionID});
}

// Retrieve or assign cart variant
const variantKey = ‘cartDrawerVariant’;
let variant = localStorage.getItem(variantKey);
if (!variant) {
// Randomly assign ‘A’ or ‘B’ variant
variant = Math.random() < 0.5 ? ‘A’ : ‘B’;
localStorage.setItem(variantKey, variant);
console.log(Cart variant assigned: ${variant});
} else {
console.log(Cart variant retrieved: ${variant});
}

// Check if Alpine.js is available
if (typeof Alpine === ‘undefined’ || !Alpine.store) {
console.error(“Alpine.js is not initialized or available!”);
return;
}

// Get the xMiniCart store
const miniCartStore = Alpine.store(‘xMiniCart’);
if (!miniCartStore) {
console.error(“xMiniCart store not found!”);
return;
}

// Monitor the ‘open’ property
Alpine.effect(() => {
if (miniCartStore.open) {
console.log(“Cart Drawer is now visible (Alpine.js).”);

// Push CleverTap event with session ID and variant
clevertap.event.push(“Cart Drawer Displayed”, {
sessionID: sessionID,
variant: variant, // Include the cart variant
});

console.log(CleverTap event pushed with sessionID: ${sessionID} and variant: ${variant});
}
});

console.log(“Alpine.js effect attached to xMiniCart.open.”);
}

// Wait for Alpine.js to initialize, even on partial reloads
document.addEventListener(‘alpine:init’, () => {
console.log(“Alpine.js initialized.”);
initCartDrawerMonitoring();
});

// Fallback: Retry initializing the monitoring if Alpine.js is loaded late
setTimeout(() => {
if (typeof Alpine !== ‘undefined’ && Alpine.store) {
console.log(“Retrying Alpine.js initialization…”);
initCartDrawerMonitoring();
}
}, 3000);

Following attached code is for splitting the Cart loading:

{% render 'cart-drawer' %}
{% render 'cart-drawer-variant' %}

Hi @vivekrahul :waving_hand: READ https://stackoverflow.com/help/minimal-reproducible-example

Think about the number of steps your making someone have to go through to try and replicate the issue to do the work of fixing it for you.

Which in turns means your not asking a single scoped question.

Remove hurdles: Strip everything down to a single function no frameworks, no excess theme parts no apps etc etc etc.

Revisit the section rendering docs https://shopify.dev/api/section-rendering

Beyond that is an advanced theme customization if you need this customization fixed and completed then contact me for services.
Contact info in forum signature.
ALWAYS please provide context, examples: store url, theme name, post url(s) , or any further detail in ALL correspondence.