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

Make GA4 and affiliate attribution agree

GA4 counts by client_id. Tradly counts by affiliate sessionId. When they disagree, finance blames attribution. This integration joins them on one key so a GA4 purchase and a Tradly conversion are queryable by the same orderRef.

When to use this

  • GA4 purchase in BigQuery joined to Tradly commission by orderRef
  • DebugView shows purchase but Tradly missing -> diagnose blocked pixel
  • Attribution window comparison: GA4 vs Tradly last-click

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
GA4 gtag: client_id (get, then set)properties.ga4_client_idRead via gtag('get', id, 'client_id'). Store alongside sessionId.
GA4 session_id (automatic)properties.ga4_session_idGA4 _s param. Pass as property for BigQuery join.
GA4 event: purchase {transaction_id}orderRef + eventIdtransaction_id must equal Tradly orderRef. This is the join key.
GA4 Measurement Protocol: purchasePOST /v1/events event_name=purchaseSend MP and Tradly v1/events with same transaction_id in same callback.
GA4 DebugViewTradly Dashboard > ConversionsBoth should show same transaction_id within seconds. If not, check ad blocker.

Copy-paste implementation

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

1) Capture GA4 IDs alongside Tradly session · javascript

// On purchase success page - fire GA4 + Tradly with same transaction_id
gtag('get', 'G-XXXX', 'client_id', (client_id) => {
  const ga4_session_id = new URLSearchParams(location.search).get('_s') || '';
  const s = JSON.parse(localStorage.getItem('affiliate_session')||'{}');
  const transaction_id = document.querySelector('[data-order-id]')?.dataset.orderId;
  // GA4
  gtag('event', 'purchase', { transaction_id, value: 120, currency: 'USD', client_id });
  // Tradly - same transaction_id = orderRef
  fetch('https://attribution.tradly.dev/api/v1/events', {
    method: 'POST', headers: {'Content-Type':'application/json'},
    body: JSON.stringify({
      conversion_key: 'ck_YOUR_VENDOR_KEY',
      event_id: transaction_id,
      event_name: 'purchase',
      occurred_at: new Date().toISOString(),
      session_id: s.sessionId,
      properties: { ga4_client_id: client_id, ga4_session_id, source: 'ga4-checkout' }
    })
  });
});

2) BigQuery: join GA4 and Tradly · sql

-- BigQuery: GA4 events_YYYYMMDD joined to Tradly export by transaction_id
SELECT g.event_date, g.transaction_id, g.value as ga4_value, t.commission
FROM `project.analytics_XXXX.events_*` g
LEFT JOIN `project.tradly.conversions` t USING (transaction_id)
WHERE g.event_name='purchase' AND _TABLE_SUFFIX BETWEEN '20260201' AND '20260228'
-- Rows where t.transaction_id IS NULL = GA4 counted, Tradly didn't (blocked pixel / missing sessionId)

Troubleshooting (from real tickets)

Problem: GA4 shows purchase, Tradly shows 0

Fix: Missing sessionId - localStorage was blocked or user came direct. Check affiliate_session on that order. If null, attribute as direct.

Problem: BigQuery join returns duplicates

Fix: transaction_id not unique across retries. Ensure event_id = transaction_id on Tradly side so retries are idempotent.

Problem: DebugView shows purchase but value mismatch

Fix: Currency/rounding: GA4 value is rounded, Tradly amount from order.total_price may differ. Send same source value to both.

FAQ

Does GA4 replace Tradly?

No. GA4 is analytics, Tradly is payout truth. GA4 deduplicates differently and can't store commission. Join on transaction_id, pay from Tradly.

Client_id vs sessionId?

GA4 client_id is device; Tradly sessionId is click. Store both as properties so BigQuery can group by either dimension.

Do I need Measurement Protocol?

Only if you want server-side parity. Browser gtag + Tradly v1/events with same transaction_id is enough for most stores.

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.