Search code examples
google-tag-managergoogle-analytics-firebasegoogle-datalayer

dataLayer proper configuration in GTM


How should the event trigger be called, based in the example given bellow for generating the dataLayer variable?

Right now my Custome Event name is 'purchase', Should I tell the developers just to generate the dataLayer as in the code example in the last part or something must be changed according to my GTM configuration?

As far as I can understand the backend developer shoud be the responsible for the correct implementation of the dataLayer variable (that is a Javascript Array). But how he/she calls the elements in the dataLayer should match with the Event Name we use in our Custom Event Trigger.

This question arise because a couple of days ago our Ecommerce tracking implementation stop registering transactions, revenue et. al. I did not make the implemention my self, but somehow I'm in charge of fixing it.

The date our GA profile stops recording transaction was 2th of August, at 4 am. That's date there was a implemention in our production server.

Our Thank you page:

I was checking the code for our thank you page, I'm noticing dataLayer variable is NOT being populated with the transaction and item(s) information.

enter image description here

How it should look like: what I've found in OptimizeSmart blog

enter image description here

GTM UA- Purchase Order TAG & Custom Event Trigger:

enter image description here

Custom Event Trigger:

enter image description here

How to generate the dataLayer with PHP:

I've found in another answer how the dataLayer can be dynamically populated:

<script>
      dataLayer.push({
          'ecommerce': {
            'currencyCode': '<?php echo $order->get_order_currency(); ?>',
            'purchase': {
              'actionField':{
                'id': '<?php echo $order->get_order_number(); ?>',
                'affiliation': 'WooCommerce',
                'revenue': <?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>,
                'tax': <?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>,
                'shipping': <?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>,
                <?php if($order->get_used_coupons()): ?>
                    'coupon': '<?php echo implode("-", $order->get_used_coupons()); ?>'
                <?php endif; ?>
              },
              'products': [
                  <?php
                    foreach($order->get_items() as $key => $item):
                      $product = $order->get_product_from_item( $item );
                      $variant_name = ($item['variation_id']) ? wc_get_product($item['variation_id']) : '';
                  ?>
                      {
                        'name': '<?php echo $item['name']; ?>',
                        'id': '<?php echo $item['product_id']; ?>',
                        'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',
                        'brand': '',
                        'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
                        'variant': '<?php echo ($variant_name) ? implode("-", $variant_name->get_variation_attributes()) : ''; ?>',
                        'quantity': <?php echo $item['qty']; ?>
                      },
                  <?php endforeach; ?>
                ]
            }
          }
      });
    </script>

Solution

  • Your datalayer push in the PHP code is missing the event variable, which is set in 'EV - Order Purchase' trigger. This is the reason, why your transaction is not being recorded, as 'UA - Order Purchase' tag is never run by its trigger.

    You should use:

    <script>
      dataLayer.push({
        'event': 'purchase', // same event name as in your trigger 
        'ecommerce': {
          // ecommerce data comes here
        }
      });
    </script>