-
The Challenge of Capturing Variant Titles in GTM's DataLayer for Shopify
Hey there! I’ve been working hard trying to fix up the GTM and dataLayer for the non plus store on Shopify. My goal is to make sure the
item_variant
value is correctly updated in the items array for GA4. Here’s an example of what I’ve coded so far in liquid:{% if template contains 'product' %} // Code pushing data to 'view_item' event goes here... {% endif %} {% if template contains 'product' %} // Code pushing data to 'add_to_cart' event goes here... {% endif %}
So here’s the issue.
item_variant
should reflect the variant of the product that was viewed or added to cart – like S,M,XL or 1 pack, 2 pack etc. When a customer just views a product, theitem_variant
value updates correctly. However, if the variant is changed on the product page (which also tweaks the url with a parameter like ?variant=37281560494243), theitem_variant
still retains the old value like the view_item event.But, I noticed something curious. In the native google ads add_to_cart event in the dataLayer, the
item_variant
actually does change to reflect the correct value. Here’s what it looks like:eventModel: { ... items: [{ ... variant: "2 pack" }] }
I’ve tried different tricks to fix it like assigning other variables like {{ product.selected_variant.title }} and {{ item.variant.title }}. But nothing seems to help. Any ideas?
Log in to reply.