Shopify’s Web Pixels API for Tracking New vs. Returning Customers

Shopify’s Web Pixels API for Tracking New vs. Returning Customers

EcomMax
Shopify Partner
16 0 8

Hi there,

I’m trying to figure out how to tell if a customer is new or returning when they complete a purchase on my Shopify store. I want to send this info to Google's Data Layer. I looked at Shopify’s guide about checkout completion events here: https://shopify.dev/docs/api/web-pixels-api/standard-events/checkout_completed. I found the checkout_completed event and used it like this:

 

analytics.subscribe("checkout_completed", (event) => {
  window.dataLayer.push({
    event: "checkout_completed",
    timestamp: event.timestamp,
    id: event.id,
    token: event.data?.checkout?.token,
    url: event.context.document.location.href,
    client_id: event.clientId,
    email: event.data?.checkout?.email,
    phone: event.data?.checkout?.phone,
    first_name: event.data?.checkout?.shippingAddress?.firstName,
    last_name: event.data?.checkout?.shippingAddress?.lastName,
    address1: event.data?.checkout?.shippingAddress?.address1,
    address2: event.data?.checkout?.shippingAddress?.address2,
    city: event.data?.checkout?.shippingAddress?.city,
    country: event.data?.checkout?.shippingAddress?.country,
    countryCode: event.data?.checkout?.shippingAddress?.countryCode,
    province: event.data?.checkout?.shippingAddress?.province,
    provinceCode: event.data?.checkout?.shippingAddress?.provinceCode,
    zip: event.data?.checkout?.shippingAddress?.zip,
    orderId: event.data?.checkout?.order?.id,
    currency: event.data?.checkout?.currencyCode,
    subtotal: event.data?.checkout?.subtotalPrice?.amount,
    shipping: event.data?.checkout?.shippingLine?.price?.amount,
    value: event.data?.checkout?.totalPrice?.amount,
    tax: event.data?.checkout?.totalTax?.amount,
  });
});

This code works for getting purchase details, but I can’t figure out how to add whether the customer is new or has shopped with us before.

Does anyone know how to add this info? I really need to know if each purchase is from a new or returning customer for our reports and to plan our ads better.

Thanks for any help!

Replies 4 (4)

lynth
Shopify Partner
124 6 15

Maybe try using customer metafield. Are you able to test the 'checkout_completed' event with Tag Assistant and preview the pushed data? I tried to trigger a data layer push on 'checkout_completed', but it seems like it's not triggering. I changed it to the 'checkout_started' event and it worked.

If my tips are useful, just mark it as the solution. Cheers!
Feeling grateful? Buy me a coffee!
lynth
Shopify Partner
124 6 15

Probably because of that, I'm using checkout extensibility without full migration (thank you page, orders status, etc. are still using checkout.liquid). We have to implement it before August 28, 2025.

If my tips are useful, just mark it as the solution. Cheers!
Feeling grateful? Buy me a coffee!

LukeHertzler
Shopify Partner
1 0 1

I'm also looking for a solution to this. As it stands now Shopify doesn't expose enough customer data to implement. We use customer.order_count to determine a new vs returning customer, however this is only available in liquid so we haven't upgrade our checkout fully.

I've talked to their support and put in a request to include this data for their web pixels release. Maybe if enough people request this they will include it.

EcomMax
Shopify Partner
16 0 8

Hi folks, just a quick update: we adjusted the custom pixel, and it initially worked well, providing accurate data in Google Analytics. However, we noticed that around 20% of orders showed incorrect customer data, marking returning customers as new. Shopify support suggested waiting for full data loading, but that didn't help.

Now, the pixel isn't working at all due to recent Shopify changes affecting data accuracy.

Here’s the custom pixel we created:

// Define dataLayer and the gtag function.
window.dataLayer = window.dataLayer || [];

// Initialize GTM
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');

// Checkout completed event
analytics.subscribe('checkout_completed', event => {
  const checkout = event?.data?.checkout;
  const customer = init?.data?.customer;

  window.dataLayer.push({
    event: "purchase_with_new_client",
    order_id: checkout.order.id,
    new_client: !customer || customer?.ordersCount < 1,
    order_count: customer?.ordersCount || 0,
    fired_from: "custom_pixel"
  });
});