Google and Meta have been using AI and machine learning for a while now to automate bidding and target audiences. This means they are heavily reliant on the first-party data you send them to improve performance.
Sending timely customer data keeps your new versus returning customer reporting accurate and tells the platforms who you want them to bid on. Keeping your audiences updated in Meta and Google lets you keep your prospecting campaigns focused on new customers and your retention campaigns limited to existing customers. It also allows you to build highly targeted customer segments or lookalike audiences for testing.
A lot of accounts still rely on manual CSV uploads, perhaps on a weekly or monthly basis, or on third-party apps like Klaviyo. We wanted more control over this, so we could implement RFM analysis, calculate customer lifetime value, or create a custom cohort in BigQuery, before sending the data to Meta and Google.
We built a simple app that copies a Shopify store's customers and orders into BigQuery. It groups customers into segments based on how recently they bought, how often and how much they spent. Every Monday it sends those segments to Meta and Google as customer lists, replacing last week's version.

How it fits together
There are five moving parts, all inside one Google Cloud project.
- A Python connector on Cloud Run pulls data from Shopify into BigQuery every morning.
- Dataform turns the raw data into clean customer tables and audience segments.
- A Meta sync job on Cloud Run replaces each Meta custom audience every Monday.
- A Google sync job on Cloud Run sends a customer list to Google Ads every Monday.
- Cloud Scheduler starts everything, and Cloud Monitoring emails you if anything goes wrong.
We've published the whole thing as two free templates. The connector is in shopify-bigquery-ingest-template, and everything else is in shopify-audience-sync-template. Each has a README, a step-by-step setup guide and a CLAUDE.md for coding assistants. They use a made-up demo client with placeholder IDs, so you swap in your own details and go. The steps below explain what each part does and which script sets it up.
What you need before you start
Most of the effort here is access and approvals, so get these sorted first.
- A Google Cloud project with billing on, and BigQuery, Cloud Run, Cloud Scheduler, Dataform and Secret Manager enabled
- A GitHub repository for the Dataform code and the two Python jobs
- A Shopify app with read access to customers, orders and products, plus the read_all_orders scope (more on that below)
- A Meta app with Marketing API access, a system user in Business Manager, and a token with ads_management permission on the ad account
- Meta's custom audience terms accepted on the ad account
- A Google Ads API developer token, an OAuth client, and an account that meets Google's Customer Match requirements
Step 1. Get the data out of Shopify
The connector is a small Python job that runs on Cloud Run at 05:15 every morning. It pulls customers, orders and products from the Shopify Admin GraphQL API and loads them into BigQuery. One deployment can load several stores, one after another, so a problem with one store doesn't stop the rest.
Use bulk operations
The normal API returns data a page at a time, which means thousands of requests and rate limits for a store with any history. Bulk operations work differently. You send Shopify one query, it builds the whole dataset in the background, and gives you a link to a single file when it's ready.
The job does three things.
- Starts a bulk operation for each object (customers, orders, products).
- Checks back every 10 seconds until Shopify says the file is ready.
- Downloads the file (one JSON object per line) and loads it into BigQuery, replacing yesterday's raw tables in full.
Replacing the tables every day keeps things simple. There's no tracking of what changed since yesterday, and edits to old orders, such as refunds, are always picked up.
There's a safety net too. If a new pull has less than half the rows already in BigQuery, the job refuses to overwrite the table and fails instead. That protects your history if Shopify access ever shrinks back to 60 days. All of this lives in services/bulk_operations.py and gbq/upload_data.py.
Two Shopify details that catch people out
By default an app can only see the last 60 days of orders. To see anything older you need the read_all_orders scope, which you request from Shopify and they approve. Without full history your segments won't mean much, so don't send anything to Meta or Google until it's in.
Customer names, emails, phone numbers and addresses count as protected customer data. Your app has to declare which of these fields it uses and why before a store can install it. We use email, phone, name, postcode and country, because every extra identifier helps Meta and Google match more people.
Set it up
Everything for this step is in shopify-bigquery-ingest-template, and its CLOUD_SETUP.md goes into more detail.
- Create an app in Shopify's Dev Dashboard with custom distribution for your store, the read scopes above and access to name, email, phone and address. Install it on the store.
- Run bash cloud/add_client.sh. It asks for the store and its credentials, checks them against Shopify, warns you if read_all_orders is missing, saves the credentials in Secret Manager and adds the store to cloud/clients.txt.
- Run bash cloud/deploy.sh. It creates a service account that can only write to that store's dataset, builds and deploys the Cloud Run job, and schedules it for 05:15. Say yes at the end to run it once straight away.
- Check BigQuery for three new tables: shopify_Customers, shopify_Orders and shopify_Products.
Step 2. Model the customers in Dataform
Dataform runs SQL in BigQuery on a schedule and reads the code straight from GitHub. At 05:45 it takes the latest code, and at 06:00 it rebuilds everything. Before it builds anything, it checks the Shopify tables were loaded today. If they weren't, nothing is rebuilt, so yesterday's audiences stay as they are and you get an email.
There are two layers.
- A staging layer with one clean table of customers and one of orders. This is the only place readable personal data lives, and access is limited to the job and named people.
- An audiences layer with one table per audience. It only ever holds hashed identifiers.
The main job of the staging layer is deciding what counts as a purchase and what each customer is worth. Keep it simple. Drop cancelled orders and £0 orders, treat fully refunded orders as not a purchase, and work out each customer's spend after refunds, tax and shipping. Multiply that by the brand's margin and you have a value per customer. The logic is in definitions/staging/stg_orders.js, and every setting it uses, from the margin to the order tags to exclude, lives in one file, includes/clients.js.
It doesn't need to be perfect. Meta and Google only use the values relative to each other, so being consistent matters more than being precise.
Set it up
From here on, everything is in shopify-audience-sync-template. Its SETUP.md covers each step in more detail.
- Copy the repo and change the demo client in includes/clients.js to your own store's settings.
- Connect Dataform to your GitHub repo, with a token in Secret Manager and a service account to run the queries. SETUP.md section 12 lists exactly what to create.
- Run bash scripts/schedule_client.sh demo. It creates the datasets, gives the service account access, schedules the 05:45 and 06:00 runs, and adds an alert that emails you if a day goes by without a successful run.
- Run bash scripts/run_cloud_now.sh demo to test it.
Step 3. Sort customers into segments with simple RFM
RFM stands for recency, frequency and monetary value. In plain terms, when did someone last buy, how often do they buy, and how much are they worth? Answer those three and you can put every customer into a group.
We use four lifecycle segments to match Meta's audiences.

Every buyer lands in exactly one segment. A check in Dataform fails the run if anyone lands in none or in two.
Set the windows from the brand's own data rather than rules of thumb. Three quick queries do it. What's the typical gap between purchases? How soon do repeat purchases happen? And how likely is someone to come back after six months, a year or two years without buying? For one of our clients, 80% of repeat purchases happened within a year and only one in eight people came back after two years, which is where the 12 and 24 month cut-offs come from.
The three queries are in SETUP.md section 11, and the thresholds go in the rfm block of includes/clients.js. The values in the template are only examples, so run the queries on your own order history before you go live. The segment logic itself is in definitions/staging/customer_rfm.js.
Four more audiences sit alongside the four segments.
- A value seed of every buyer with their value attached, used for value-based lookalikes
- A seasonal audience of anyone who bought between 1 November and 31 December, for Q4 campaigns
- Subscribers who have never bought, for nudging people towards a first order
- Every customer who has ever bought, which is the list we send to Google
Step 4. Normalise and hash the identifiers
Meta and Google both expect every identifier cleaned up and SHA-256 hashed before it arrives, and a match only works if the clean-up is exactly right.
We do both steps in SQL in the audiences layer.

Each clean value is then hashed with BigQuery's SHA256 function and stored as hex. A Dataform check fails the run if anything in an audience table isn't a 64-character hash.
Missing values go through as blanks rather than dropping the row. Sending phone and name alongside email is worth the effort, because it gives the platforms a second route to a match when someone signed up with a different email.
The normalisation rules are in includes/sql.js, and they're applied in definitions/staging/stg_customers.js.
Step 5. Send the audiences to Meta
The Meta sync is a Python job on Cloud Run, one per client, that runs at 06:30 every Monday after the morning's Dataform build. It reads only the hashed audience tables. The code is in sync/sync.py.
Replace the whole list each week
We use the Marketing API's replace endpoint, which swaps the old list for the new one. Adding people is simpler, but nobody ever leaves the list, so a lapsed customer would sit in High value for ever. Meta says a replace doesn't reset the learning phase of ad sets using the audience.
Send it in batches
Meta takes up to 10,000 people per request. Bigger audiences go as numbered batches inside one upload session, with the last batch flagged, and the session has to finish within 90 minutes. Temporary errors like rate limits are retried with a growing wait.
Map each segment to Meta's labels
Each RFM segment becomes its own custom audience, labelled when it's created so it slots into Meta's audience definitions (High value customers, At risk and so on). The value seed is created as a value-based customer list, with each person's value in the LOOKALIKE_VALUE field. You can only choose value-based when the audience is first created, so get that right first time.
Before it touches an audience, the sync checks it really is one of its own. The name has to start with a set prefix, it has to belong to the right ad account, and any lists you made by hand can go on a protected list it will never touch.
Cover the gap with the pixel
Weekly is plenty for segments measured in months. The gap is people who bought since Monday, so the pixel's purchase audience sits alongside the synced lists in exclusions and the account's existing customer definition.
Set it up
- In Business Manager, create a system user, give it your ad account, and generate a token with ads_management. Save it in Secret Manager as meta-token.
- Put your ad account ID in sync/config/demo.yaml.
- Run python create_audiences.py --client demo from the sync folder to preview the audiences, then again with --yes to create them empty, with their labels. Paste the IDs it prints into the config.
- Run bash scripts/setup_sync.sh demo. It creates the service accounts, the run log table, the Cloud Run jobs, the alerts and the weekly schedules, which start paused.
- Run bash scripts/run_sync_now.sh --dry-run, then a live sync. When you're happy, turn the schedules on (SETUP.md section 13.6).
Step 6. Send the audiences to Google
A separate Cloud Run job sends the hashed audiences to Google Ads as Customer Match lists on the same Monday morning. The hashing rules from step 4 work for both platforms, so there's no extra modelling.
We send them through Google's Data Manager API rather than the older offline user data jobs in the Google Ads API. An Explorer-level developer token can't use the older route, but it can use the Data Manager API, so you don't have to wait for a higher access level.
- It finds the Customer Match list in each Google Ads account by name, and creates it the first time.
- It uses an OAuth refresh token with both the Google Ads and Data Manager scopes, stored in Secret Manager.
- Each week it sends the hashed emails and phone numbers to the Data Manager ingest endpoint in batches of 10,000.
- It retries on rate limits, and runs the same safety checks as the Meta sync before anything is sent.
Two things work differently from Meta. The Google job adds people to the list rather than replacing it, so the list grows over time and lapsed customers stay on it. And the all-customers list skips the consent filter the other audiences use, so check that against your own consent position and Google's Customer Match policy before you switch it on. SETUP.md section 14 shows how to apply the same rule as the Meta audiences.
We send Google one list per account, all customers. It works well as an exclusion in Search and PMax, and as customer data for PMax's new customer acquisition goal. The RFM segments are used on Meta, where they drive targeting.
Set it up
- Save your Google Ads OAuth credentials in Secret Manager as google-ads-oauth-credentials. SETUP.md section 14.2 shows the format.
- Put your Google Ads account ID and manager account ID in sync/google_sync/google_config/demo.yaml.
- Run bash scripts/deploy_google_sync.sh.
- Test it with bash scripts/run_google_sync_now.sh --client=demo --dry-run.
The code is in sync/google_sync/sync_google.py.
Step 7. Schedule it and add safety checks
The rule we built around is simple. If anything looks wrong, upload nothing and tell someone, because a bad upload is worse than a late one.

Checks not passed today, or any audience shrinking by more than half since last week
The sync won't run unless that morning's checks have all passed. A drop of more than half almost always means something upstream broke, and a genuine one can be pushed through once with an override. A dry-run mode runs every check without uploading, which is how we test changes.
Cloud Monitoring emails us when a job fails, when Dataform hasn't run in 25 hours, or when the sync is blocked. A small watchdog job also checks on Tuesday morning that a sync actually happened, because the worst failure is the one that never starts and so never errors.
Every sync writes a log row per audience with rows sent, rows rejected and the platform's response.
You don't need to build any of this yourself. The setup scripts in the previous steps create every check, alert and schedule, and SETUP.md sections 5 and 13.3 list each alert, what it means and what to do about it.
Building it with Claude Code
If you're handing this to a coding assistant, point it at the two template repos (each has a CLAUDE.md written for exactly this) and give it one step at a time rather than the whole article at once, and check each step works before moving on. A good order is the Shopify connector first, then the Dataform staging models, then the segments and hashing, and the sync jobs last.
A few things to tell it up front will save you a lot of back and forth.
- Keep all secrets in Secret Manager and never in code
- Use Shopify bulk operations and replace the raw tables daily
- Keep readable personal data in the staging dataset only, and hash everything in SQL
- Build a dry-run mode into the sync before it ever talks to Meta or Google
The parts it can't do for you are the approvals. The read_all_orders scope, the protected customer data declaration, Meta's custom audience terms and Google's developer token all need a person to request them, so start with those.
Build it or bring us in
None of this is exotic. It's a connector, some SQL and two sync jobs, and the value comes from getting the details right. The templates have those details built in, so if you have the skills in-house, we hope they save you a lot of time.
If you'd rather have it built and run as part of your paid media, that's what we do. For the marketing side of the story, read Why you should be updating your Meta custom audiences every week, or get in touch and we'll take a look at your account.
Three of Us is a growth marketing consultancy for founder-led e-commerce brands with purpose in their DNA who are ready to scale. We build connected systems across acquisition, conversion and retention, and we measure our success by yours.