All articles

Shopify attribution

Shopify affiliate link tracking with cart attributes: a theme.liquid recipe

Read a ?ref parameter, save it as a Cart AJAX attribute, and let Shopify preserve it to the order so link visits attribute without a discount code.

Published · Last reviewed

Written by Attribloom Engineering · Reviewed by Attribloom Product · Last reviewed

When you need link attribution

Link attribution credits a visit that came from an affiliate URL without asking the shopper to enter a discount code. It is the optional second path in a Shopify program: more reach than codes alone, at the cost of one small storefront change. This recipe summarizes the mechanism. The exact, current snippet lives in the Shopify guide, which is the canonical copy to paste.

The mechanism

A landing URL carries the affiliate's referral code as ?ref=<code>. A small script in theme.liquid reads that parameter and writes it as a Cart AJAX attribute through Shopify's cart update endpoint. Shopify then preserves that cart attribute to the order as a note attribute, which Attribloom reads from the paid-order webhook to credit the affiliate.

  • Read ?ref from the URL and validate it before trusting it.
  • Write it as a cart attribute named ref via cart/update.js.
  • Shopify carries the cart attribute to the order note attributes.
  • Attribloom reads the preserved ref from the signed paid-order webhook.

Validation and conflict handling

Validate the ref before saving it. The shipped recipe accepts only a conservative character set and length, so a malformed or overlong value is ignored rather than written to the cart. This keeps junk and injection attempts out of the order.

A discount code and a preserved ref can disagree. When they identify different affiliates, Attribloom fails closed: the validated discount code takes precedence, and if the two still conflict the order is left unattributed rather than credited to the wrong partner. Note that Attribloom's own /r/<code> redirect does not append ?ref=, so link attribution needs a landing URL that already carries the parameter.

Sources and verification

Primary documentation last checked on 2026-07-20. Each source below links the exact page and its own last-checked date.

Next steps

Frequently asked

Where does the ref value end up on the order?

Shopify copies the Cart AJAX attribute to the order's note attributes, and Attribloom reads it from the signed paid-order webhook to credit the affiliate.

What happens if the discount code and the ref disagree?

Attribloom fails closed. A validated discount code takes precedence, and if the two still identify different affiliates the order is left unattributed to avoid crediting the wrong partner.

Where is the exact snippet to paste?

In the Shopify guide. It is the canonical, current copy, so use it rather than reproducing the script from memory.

In the glossary

Continue reading

Get startedSee pricingMore articles