In checkout_completed, we have access to customer.id
when the user explicitly logged in at checkout. But for guest checkout, customer.id
is empty even though a customer profile (and id
) are created at time of checkout.
It would be very helpful to know customer.id
in checkout_completed for guest checkouts. Here’s why:
We send customer.id
as user_id over to Google Analytics 4 (GA4) so we can understand attribution tied back to a specific customer’s lifetime behavior. This works great when they are logged in to Shopify but does not work when doing guest checkout.
Hello Rob,
If you’re referring to the init.data.customer
object, it is initialized on pixel load and will not update throughout the session.
Inside the checkout_completed
event payload, there is the event.data.checkout.order.customer
object which includes the id
of the customer, even for guest checkouts.
If the customer decides to log in during the checkout process, the page will eventually reload and the init.data.customer
object should then be available anyway.
Guest checkout
console.log(init.data.customer); // => null
analytics.subscribe("checkout_completed", (event) => {
console.log(init.data.customer); // => null
console.log(event.data.checkout.order.customer.id); // => "1234"
});
Authenticated checkout
// If already logged in, the customer object should be available right away
console.log(init.data.customer);
// => {
// email: "...",
// firstName: "test",
// id: "1234",
// lastName: "test",
// ordersCount: 1,
// phone: null,
// }
analytics.subscribe("checkout_completed", (event) => {
console.log(event.data.checkout.order.customer.id); // => "1234"
});
Hope this helps!
1 Like
@emileber you just made me so happy I could cry. Thank you for this. It works great!
2 Likes