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?