# 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: 2026-07-20 · Last reviewed: 2026-07-20

## 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.

## 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.

## Primary sources

- [Shopify: Cart Ajax API (cart attributes)](https://shopify.dev/docs/api/ajax/reference/cart) · checked 2026-07-20
- [Shopify: app webhooks](https://shopify.dev/docs/apps/build/webhooks) · checked 2026-07-20

## Next steps

- [Shopify affiliate software guide](/guides/shopify)
- [Run a Shopify affiliate program without editing your theme](/learn/shopify-affiliate-program-without-editing-theme)
