One Segment track call, two destinations that agree
Your checkout already fires Segment track('Order Completed'). Add a Segment Destination Function that forwards the same payload to Tradly's canonical v1/events - no second SDK, no theme edits, same orderRef dedupe.
When to use this
- Existing Segment stack (GA4 + Mixpanel) adds affiliate payouts without new pixel
- Server-side Segment (no ad-block risk) guarantees Tradly delivery
- One orderId flows to warehouse, analytics, and commission
Field map: Shopify → Tradly
This table is the moat. ChatGPT will hallucinate field names; this is the exact mapping that prevents double-payouts.
| Shopify source | Tradly field | Notes |
|---|---|---|
Segment track('Order Completed') {orderId} | orderRef + eventId | orderId is the primary key for all three. |
Segment context.anonymousId / userId | external_user_id | Map to Tradly external_user_id for LTV joins. |
Segment properties.revenue, currency | properties.revenue + eventValue | Pass through. Tradly commission calc uses eventValue. |
Segment context.ip, userAgent | properties.segment_context | Preserve for fraud review (velocity checks). |
Segment messageId | event_id fallback | If orderId missing, use messageId as idempotent key - but prefer orderId. |
Copy-paste implementation
Both paths use same event_id = order_id so Tradly dedupes. Keep both: pixel for browser session, webhook for durability.
Segment Destination Function (copy-paste) · javascript
// Segment Destination Function -> Tradly v1/events
async function onTrack(event) {
if (event.event !== 'Order Completed') return;
const orderId = event.properties.orderId || event.properties.order_id || event.messageId;
const s = event.properties._tradly_session || event.context.traits?._tradly_session;
await 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: String(orderId),
event_name: 'purchase',
occurred_at: new Date().toISOString(),
session_id: s?.sessionId,
click_id: s?.clickId,
external_user_id: event.userId || event.anonymousId,
properties: { segment_messageId: event.messageId, revenue: event.properties.revenue }
})
});
}Verify in warehouse · sql
-- Warehouse: Segment events joined to Tradly
SELECT s.orderId, s.revenue as segment_revenue, t.commission, t.partner_id
FROM segment.tracks s LEFT JOIN tradly.conversions t ON s.orderId = t.order_ref
WHERE s.event='Order Completed'Troubleshooting (from real tickets)
Problem: Segment fires but Tradly missing sessionId
Fix: Inject _tradly_session into Segment track call at cart/checkout time: analytics.track('Order Completed', {...properties, _tradly_session: JSON.parse(localStorage.affiliate_session)}).
Problem: Duplicate conversions from Segment retries
Fix: Same orderId -> same event_id. Tradly returns 200 with existing event, no double commission.
Problem: Need to backfill historical orders
Fix: Replay Segment events with same orderId. Idempotency ensures safe replay.
FAQ
Do I need to change my checkout code?
One line: add _tradly_session to the track call. The Function handles the rest.
Pixel vs Segment?
Segment is more durable (server-side, not blocked). Keep pixel as fallback for direct web checkout without Segment.
How do refunds work?
Fire track('Order Refunded') with same orderId and negative revenue. Function sends Tradly refund event.
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.