0

I am trying to add DataLayer JavaScript code in Thankyou page in <head> tag, just before Google Tag Manager (GTM) Container.

I placed the DataLayer JavaScript in the end of thankyou.php file. It seems that the data is pushed to Google Analytics/Ecommerce section. But some data (transactions) are messing). Also, the GTM developer guide recommends to place any dataLayer just before GTM container in tag of the page.

I have tried to place the following code to function.php file:

function addDataLayerToHead(){
  ?>
   <script>
  dataLayer.push({
  'event':'transaction',
      'ecommerce': {
        'currencyCode': '<?php echo $order->get_order_currency(); ?>',
        'purchase': {
          'actionField':{
    // my variables 
            ]
        }
      }
  });
 </script>
 <?php
 }
 add_action( 'wp_head', 'addDataLayerToHead', 10 );

But this code does not seem working, and I do not want to add the datalayer in all pages. I only want to add the dataLyaer in Thankyou page.

Note that I use WordPress and WooCommerce. Also, I am trying to not use the plugin for such a task.

Any help?

  • Take a look at this https://stackoverflow.com/questions/52235441/how-to-check-woocommerce-thank-you-page this can help you with part of your question – 7uc1f3r Mar 23 '20 at 15:22
  • You cannot push to the datalayer before it is declared. The datalayer is an array of objects. If the datalayer is not declared yet, then it will be undefined. Undefined does not have a push method, hence the code above will not work. – Eike Pierstorff Mar 23 '20 at 21:56

0 Answers0