Home
Integration · Defensible: ChatGPT can't run this code

Shopify affiliate tracking that survives theme changes

Shopify's Order Status Page wipes scripts on every theme update. This integration gives you two paths that both resolve to the same Tradly session: a pixel for speed, and a server webhook for durability. Includes exact field map and idempotent order_ref handling.

When to use this

  • Influencer + coupon codes per partner (slug per channel)
  • Post-purchase upsells without double-counting
  • Refunds / partial refunds that void commission correctly

Field map: Shopify → Tradly

This table is the moat. ChatGPT will hallucinate field names; this is the exact mapping that prevents double-payouts.

Shopify sourceTradly fieldNotes
Shopify checkout.liquid: checkout.order_idorderRefUse as orderRef + event_id. Prevents double-count on reload.
Shopify checkout.liquid: checkout.total_priceeventValue / amountMinor units (cents). Divide by 100. Send currency from checkout.currency.
affiliate_session.sessionId (localStorage)sessionIdRead from localStorage.affiliate_session. Fallback to order note attribute if cookie blocked.
Shopify webhook: orders/createeventId = order.idIdempotent retry key. Tradly returns 200 with existing event on duplicate.
Shopify webhook: orders/refundedeventName=refund, amount=-refund_totalSend negative amount with same orderRef to void or adjust commission.

Copy-paste implementation

Both paths use same event_id = order_id so Tradly dedupes. Keep both: pixel for browser session, webhook for durability.

1) Order Status Page pixel (checkout.liquid / Custom Pixel) · liquid

<!-- Shopify: Settings > Checkout > Order status page -->
<script>
(function() {
  var s = localStorage.getItem('affiliate_session');
  var sessionId = s ? JSON.parse(s).sessionId : null;
  if (!sessionId) return;
  var img = new Image();
      img.src = 'https://attribution.tradly.dev/api/public/pixel.gif'
    + '?conversion_key=ck_YOUR_VENDOR_KEY'
    + '&event_name=purchase'
    + '&session_id=' + encodeURIComponent(sessionId)
    + '&order_ref=' + encodeURIComponent('{{ checkout.order_id }}')
    + '&amount=' + encodeURIComponent('{{ checkout.total_price | money_without_currency }}')
    + '&currency={{ checkout.currency }}'
    + '&event_id={{ checkout.order_id }}';
})();
</script>

2) Durable S2S: Shopify webhook -> your worker -> Tradly · javascript

// Your Cloudflare Worker / backend handling Shopify orders/create webhook
export default {
  async fetch(req) {
    const order = await req.json();
    const sessionId = order.note_attributes?.find(a => a.name === '_tradly_session')?.value;
    const res = await fetch('https://attribution.tradly.dev/api/public/conversions', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        conversionKey: 'ck_YOUR_VENDOR_KEY',
        amount: Number(order.total_price),
        currency: order.currency,
        orderRef: String(order.id),
        eventId: String(order.id), // idempotent
        sessionId: sessionId || undefined,
        partnerId: undefined // resolved via sessionId -> last-click
      })
    });
    return new Response(null, { status: res.ok ? 200 : 500 });
  }
}

3) Verify: curl · bash

curl -X POST https://attribution.tradly.dev/api/public/conversions \
  -H 'Content-Type: application/json' \
  --data '{"conversionKey":"ck_YOUR_VENDOR_KEY","amount":120,"currency":"USD","orderRef":"gid://shopify/Order/123","eventId":"gid://shopify/Order/123","sessionId":"sess_from_click"}'

Troubleshooting (from real tickets)

Problem: Pixel fires twice on reload

Fix: Both requests send same event_id (= order_id). Tradly returns existing event, no double commission. Verify in Dashboard > Conversions.

Problem: sessionId is null on Order Status Page

Fix: Safari ITP blocks localStorage cross-domain. Also write sessionId into checkout note_attributes at cart time via Storefront API, then read it in webhook.

Problem: Refund still shows commission

Fix: Send refund webhook with same orderRef and negative amount. Earnings adjust and payout CSV reflects void.

FAQ

Pixel or webhook - which to keep?

Keep both. Pixel captures sessionId in browser; webhook guarantees delivery even if pixel blocked. Same event_id dedupes them.

Do I need a Shopify app?

No for basic. Yes if you need note_attributes at cart time or to handle ITP. The webhook path works with Shopify's built-in webhook UI.

How do refunds reconcile?

Shopify fires orders/refunded. Post a refund event with same orderRef and negative amount - Tradly adjusts earnings, visible in /vendors/:id/stats.

Next: estimate payout with Commission Calculator (exports CSV) or see Attribution feature for last-click rules.

More integrations: shopify, ga4, segment — add GA4/Segment next with same field-map pattern.