⚠️ Developer article. This change is made in your storefront code, so it is for your development team or agency. Your team owns the integration and Measmerize can't make this change for you.
Symptoms
The Size Hub works, and Size recommendations issued in the Measmerize Analytics dashboard is counting up.
Purchases with a size recommendation, Sales with Size Recommendation and Share of Sales show N/A or 0, even though the store has real orders.
Your store is headless or has a custom checkout, and checkout lives on a different hostname from the product pages (for example the storefront on
www.example.comand checkout oncheckout.example.com).
Why it happens
Measmerize links an order to a recommendation in two steps.
On the product page, the Size Hub saves each recommendation in the browser's local storage, under the key
_measmerize_user_recommendations.At checkout, the Measmerize Shopify pixel runs when the order completes, reads those saved recommendations, and records them with the order.
Browsers keep local storage separate for each origin, and www.example.com and checkout.example.com are different origins. When the pixel runs on the checkout domain, local storage there is empty. The order is recorded with no recommendation, so it never counts as a purchase with a size recommendation.
Setup | Product page and checkout share an origin? | Orders linked automatically? |
Standard Shopify theme (App Block) | Yes | Yes |
Headless storefront with checkout on the same domain | Yes | Yes |
Headless or custom storefront with checkout on a separate domain or subdomain | No | No, needs the change below |
The fix is to stop depending on local storage at checkout. When the shopper adds a product to the bag, your storefront copies the recommendation onto the cart line as a line attribute. Cart lines travel with the cart into checkout, whatever domain it runs on, and the pixel reads the attribute there.
Before you start
The Size Hub is already integrated and issuing recommendations on your storefront. If not, start with Integration for Headless Storefronts.
You can edit the code that runs your add-to-bag action (the Storefront API call, or the form action in your framework).
You can read local storage from that code, in the browser.
What to implement
At the moment the shopper adds a product to the bag, add one attribute to that cart line.
Field | Value |
Where | The cart line's |
Key |
|
Value | A JSON string: an array holding only the most recent entry for the product being added, in the same format as the local storage item |
⚠️ Use the key exactly as written: lowercase, with the leading underscore. It is the same name as the local storage item you read from.
The value
Local storage holds a JSON array of entries. Each entry has a productCode, which is the numeric Shopify product ID (not the variant ID, and not the gid://shopify/Product/... form). Find the entries whose productCode matches the product being added, take the most recent one, and pass it unchanged. Passing it unchanged means any fields Measmerize adds later come through without a code change on your side.
An entry looks like this:
{
"id": "4e259d5f-26e2-45fe-8b72-2988a0299f13",
"productCode": "6981189861448",
"orders": [],
"recommendedSize": "26"
}
The cart line attribute value is that entry wrapped in an array, serialized as a string:
[{"id":"4e259d5f-26e2-45fe-8b72-2988a0299f13","productCode":"6981189861448","orders":[],"recommendedSize":"26"}]
If there is no matching entry, or local storage can't be read, add nothing and let the add to bag continue as normal.
Steps
1. Read the recommendation when the shopper adds to bag
Read local storage at the moment of the add to bag action, not earlier and not cached, so you get the latest value. This example returns the attribute, or null when there is nothing to attach.
const MEASMERIZE_KEY = '_measmerize_user_recommendations';
function getMeasmerizeAttribute(productId) {
try {
const entries = JSON.parse(localStorage.getItem(MEASMERIZE_KEY) || '[]');
const matches = entries.filter(
(entry) => String(entry.productCode) === String(productId)
);
if (matches.length === 0) return null;
const latest = matches[matches.length - 1];
return { key: MEASMERIZE_KEY, value: JSON.stringify([latest]) };
} catch (error) {
return null; // storage blocked or unreadable: add nothing
}
}
productId is the numeric Shopify product ID. If your code holds the global ID (gid://shopify/Product/6981189861448), use only the trailing number.
2. Pass it on the cart line
With the Storefront API, add it to the attributes of the line in cartLinesAdd (or in cartCreate when there is no cart yet). The attribute value must be a string, which is why the example above uses JSON.stringify.
const attribute = getMeasmerizeAttribute(productId);
const line = {
merchandiseId: variantGid, // gid://shopify/ProductVariant/...
quantity: 1,
attributes: attribute ? [attribute] : [],
};
// variables for the cartLinesAdd mutation
const variables = { cartId, lines: [line] };
mutation cartLinesAdd($cartId: ID!, $lines: [CartLineInput!]!) {
cartLinesAdd(cartId: $cartId, lines: $lines) {
cart {
id
lines(first: 50) {
nodes {
id
attributes { key value }
}
}
}
userErrors { field message }
}
}
If you use Hydrogen, put the same attributes array on the line you pass to CartForm with the LinesAdd action.
If your storefront adds to bag from several places (product page, quick add, a sticky bar), call the same helper from each one.
3. Keep the attribute through cart edits
If your cart code rebuilds lines (for example when the shopper changes size or quantity), carry the attribute across to the new line. A line that loses the attribute reaches checkout without it.
Check it worked
On a product page of your storefront, open the Size Hub and get a size recommendation.
In the browser console, run
localStorage.getItem('_measmerize_user_recommendations'). You should see an array with an entry whoseproductCodematches the product.Add the product to the bag.
Read the cart (the
cartLinesAddresponse above, or a cart query). The line should have an attribute with the key_measmerize_user_recommendationsand your JSON string as the value.Complete a test order in the same browser, then email [email protected] with your store URL and the time of the order. We will confirm that the order is linked to the recommendation.
Place the first test order only after the change is live on your production storefront, and tell us before you do, so we know which order to look for.
What to expect
Only new orders are linked. Orders placed before your change went live stay without a recommendation.
Recommendation first, then add to bag. If the shopper gets a recommendation after the product is already in the bag, it isn't attached to that line. This is the normal flow, so most orders are unaffected.
Same browser. The recommendation and the purchase have to happen in the same browser. A shopper who gets a recommendation on their phone and buys on their laptop is not linked.
Dashboard timing. The dashboard updates once linked orders arrive. If you switch between stores or date ranges and see the same numbers, change the date grouping (for example from Week to Day or Month) to refresh the view.
Common problems
The attribute is missing on the cart line. The shopper got the recommendation after adding to bag, local storage was blocked or empty, or the line was rebuilt without the attribute. Check step 3.
The attribute is there but orders still don't link. Check that the key is exactly
_measmerize_user_recommendations, that the value is a JSON string (not an object), and thatproductCodeis the numeric product ID, not a variant ID or global ID.Add to bag fails after the change. The value must be a string and the whole call must not throw. Keep the
try/catchin the helper so a storage error can't block the purchase.Local storage is empty on the product page. The shopper hasn't completed a recommendation in this browser, or local storage is blocked (private browsing, a consent tool). Nothing is attached, which is expected.
Related articles
Still not working?
Email [email protected] with your store URL, the product URL you're testing, and a copy of the cart line attributes from step 4.