Learn business growth with Google Analytics 4 Forums Google Analytics 4 The Challenge of Capturing Variant Titles in GTM's DataLayer for Shopify Reply To: The Challenge of Capturing Variant Titles in GTM's DataLayer for Shopify

  • Ethan

    Member
    25 July 2022 at 9:37 pm

    Good news, I figured it out on my own! For keeping the variant title or id of the item updated on a ‘cart addition’, the trick is to add an iteration on product.variants within the ecommerce variable. Then point it to the suitable liquid variable – this could be variant.title or variant.id.

    
    {% if template contains 'product' %}
     dataLayer.push({
       'ecommerce': null
     });
     var ecommerce = {
       'items': [{
         'item_id': {{product.id | json}},                 
         {% for variant in product.variants %}
         'item_variant': {{variant.title | json}},
         {% endfor %}               
         'item_name': {{product.title | json}},
         'price': {{product.price | divided_by: 100.0 | json}},
         'item_brand': {{product.vendor | json}},
         'item_category': {{product.type | json}},
         'item_list_name': {{collection.title | json}},
         'description': {{product.description | strip_newlines | strip_html | json}},
         'imageURL': 'https:{{product.featured_image.src|img_url:'grande'}}', 
         'productURL' : '{{product.url}}'
       }]
     };              
     $(__DL__.cartTriggers).click(function(){
       dataLayer.push({                    
         'event': 'add_to_cart',
         ecommerce
       });                  
     });              
    {% endif %}
    

    So there you have it! This code should take care of keeping the variant updated when adding to the cart.