How to Build a Custom Attribution System for Shopify
A technical guide to building a custom marketing attribution system for your Shopify store, covering data collection, cookie management, server-side tracking, and multi-touch modeling.
Three platforms, three answers, one budget to defend. Your GA4 export already holds the answer, and the first finding is free and never leaves your browser. Get your first finding free
By Joris van Huët, Founder & CEOUpdated 3 min read
How to Build a Custom Attribution System for Shopify
Building a custom attribution system for Shopify means creating your own infrastructure to collect touchpoint data, stitch user journeys together, and assign conversion credit across marketing channels.
The Five Core Components
1. Data Collection Layer
You need to capture every interaction a visitor has with your marketing and your store:
- Click data: UTM parameters, click IDs (gclid, fbclid, ttclid), referral URLs
- Session data: Pages viewed, cart actions, checkout steps
- Conversion data: Order ID, revenue, products, customer email (hashed)
- Ad platform data: Campaign spend, impressions, and clicks from Meta Ads, Google Ads, and TikTok Ads APIs
On Shopify, click data is captured through JavaScript on your storefront. When a visitor lands, a script reads URL parameters and stores them in a first-party cookie.
2. Identity Resolution Layer
Identity resolution connects multiple sessions from the same person into a single profile. Common identifiers include:
- First-party cookie ID: Works within a single browser but fails across devices
- Hashed email: Available once the customer enters their email at checkout or via Klaviyo signup
- Shopify customer ID: Assigned after account creation or first purchase
- Click IDs: Platform-specific identifiers matchable to platform data
The most reliable approach combines cookie-based tracking with deterministic matching on hashed email. When a customer identifies themselves, you backfill their anonymous sessions with their known identity.
3. Data Storage Layer
Store raw touchpoint events in an append-only table. Each row represents one touchpoint: timestamp, user identifier, channel, campaign, and metadata. Conversion events go in a separate table linked by user ID.
4. Attribution Modeling Layer
Standard attribution models include:
- Last-click: Simple but undervalues upper-funnel channels
- First-touch: Useful for acquisition analysis but ignores conversion paths
- Linear: Equal credit to all touchpoints, a reasonable default
- Time-decay: More credit to touchpoints closer to conversion
Start with linear or time-decay. Moving to data-driven models requires expertise in causal inference or marketing mix modeling, which is where build-vs-buy becomes serious.
5. Reporting Layer
Build dashboards answering three questions: What is the true ROAS of each channel? Where should spend increase or decrease? How do customer acquisition costs vary by channel?
Implementation on Shopify
Step 1: Deploy server-side cookie tracking. Use a Cloudflare Worker or Shopify App Proxy to set first-party cookies via HTTP headers.
Step 2: Capture events via Shopify webhooks. Subscribe to orders/create and orders/paid. When an order fires, retrieve the customer's touchpoint history and run your attribution model.
Step 3: Pull ad platform spend data. Schedule daily jobs to pull spend from Meta, Google, and TikTok APIs. Match spend to attributed revenue for true ROAS.
Step 4: Build the attribution pipeline. Create a batch job that processes raw events into attributed conversions nightly.
Step 5: Validate against platform data. Your attributed conversions per channel should be lower than platform self-reports but sum to actual revenue.
Build vs. Buy
Browser privacy changes, API updates, and Shopify's evolving checkout architecture all require continuous adaptation.
Common Mistakes
- Relying solely on client-side JavaScript: Browser restrictions degrade data within months. Use server-side tracking as the foundation.
- Ignoring view-through conversions: Click-only tracking undervalues social and video channels.
- Not testing incrementality: Multi-touch models still assign credit to touchpoints that may not have been necessary. Without incrementality testing, you cannot distinguish correlation from causation.
- Underestimating maintenance: The hardest part is keeping it accurate as browsers, platforms, and Shopify evolve.
Go deeper: Causal attribution, explained.
Sixty-second versions of these ideas: Causality Engine on YouTube Shorts.
Have an audience of ecommerce operators? Earn 20% recurring commission by recommending the €99 read.
Keep reading
Terms in this article
- Attribution ModelAn Attribution Model defines how credit for conversions is assigned to marketing touchpoints. It dictates how marketing channels receive credit for sales.
- Attribution ModelingAttribution Modeling is a framework for assigning credit for conversions to various touchpoints in the customer journey. It helps marketers understand and improve campaign effectiveness.
- Causal InferenceCausal inference is the set of methods for estimating how much an outcome changes because of a cause, rather than alongside it. It works by comparing what happened with an estimate of what would have happened without the cause.
- Customer acquisitionCustomer acquisition attracts new customers to a business. For e-commerce, this means driving the right traffic to the website.
- First-Party CookieA First-Party Cookie is a cookie set by the website a user visits. These cookies provide essential website functionality, such as remembering user preferences and login information.
- Identity ResolutionIdentity Resolution connects and matches customer data from various sources. It creates a single, unified view of each customer.
- Incrementality TestingIncrementality Testing measures the additional impact of a marketing campaign. It compares exposed and control groups to determine causal effect.
- Marketing Mix ModelingMarketing Mix Modeling (MMM) is a statistical analysis that estimates the impact of marketing and advertising campaigns on sales. It quantifies each channel's contribution to sales.