Skip to content
Google Analytics

Google Analytics

If you want to correctly track AddToCarts with your gtag script, you have to listen to the Smootify Add To Cart event and send the event using the gtag utility of Google, as example you can use the snippet below:

document.addEventListener('smootify:added_to_cart', (event) => {
const {lines, cart} = event.detail;
if (window.gtag) {
for (let line of lines) {
const cartLine = cart.lines.nodes.find(e => e.merchandise.id === line.merchandiseId);
if (cartLine) {
gtag('event', 'add_to_cart', {
'items': [{
'id': line.merchandiseId.split("/").pop(),
'quantity': line.quantity,
'price': Number(cartLine.cost.totalAmount.amount),
'currency': cartLine.cost.totalAmount.currencyCode
}]
});
}
}
}
});

This is the correct way to track the add to cart, since a click on the button will not ensure you that the product is purchasable or not.

Naturally, be sure you have the consent to track!