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

j0hnsmith
Shopify Partner
5 0 3

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!

Reply 1 (1)

lynth
Shopify Partner
11 0 1

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.