Data Layer Scripts for Collection Page Views

Data Layer Scripts for Collection Page Views

aaosborn313
Visitor
1 0 0

Long time GTM user whose looking to get my ecommerce legs under me. As such, I've built a demo store that I'm using as a proving grounds. Appreciate any time invested in providing guidance in advance. Here's the scenario:

 

I've built out all requisite ecommerce trigger events in GTM using a regex matching custom event that fires when the corresponding event and data is passed to the Data Layer. Triggering the custom events from the shopify side has proved challenging however. Specifically, I'm looking to trigger a data layer push when site visitors visit my collection pages.. I've written the following script that ***I think*** should trigger the pass to the data layer and fire the corresponding custom event tag but I'm not sure where and how to install. Any assistance would be greatly appreciated.

 

<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({ ecommerce: null });
{% if first_time_accessed %}
dataLayer.push({
  event: "view_item_list",
  ecommerce: { 
      items: [
       {% for line_item in line_items %}{
        item_id: "{{ line_item.product_id }}",
        item_name: "{{ line_item.title | remove: "'" | remove: '"' }}",
        currency: "{{ order.currency }}",
        price: {{ line_item.final_price | times: 0.01 }},
      },{% endfor %}
 ]
  }
});
{% endif %}
</script>

 

Shopify store: https://andrews-test-store-4037.myshopify.com/

Reply 1 (1)

jordanholmes
Shopify Partner
152 28 34

"Specifically, I'm looking to trigger a data layer push when site visitors visit my collection pages"

 

The code you have does not belong in the theme or for tracking page views - it is for tracking sales on the order status page.

 

You could just use Google Analytics to track site page views

Jordan Holmes
Shopify Expert and Ruby on Rails Developer
Shopify Partner Directory