php – Adding DataLayer in <head> Tag just before Google Tag Manager (GTM) Container

0
php – Adding DataLayer in <head> Tag just before Google Tag Manager (GTM) Container

php – Adding DataLayer in Tag just before Google Tag Manager (GTM) Container

I am trying to add DataLayer JavaScript code in Thankyou page in 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(){ ?> dataLayer.push({ ‘event’:’transaction’, ‘ecommerce’: { ‘currencyCode’: ‘get_order_currency(); ?>’, ‘purchase’: { ‘actionField’:{ // my variables ] } } }); <?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?

We will be happy to hear your thoughts

Leave a reply