Using Hooks Element
If you have GP Premium, using our Hooks Element module would be the easiest way to add the code.
The first block of code should look similar to this:
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');
Go to Appearance > Elements and click 「Add New」. From the Element dropdown list, choose 「Hook」.Copy and paste the your own code into the content area.Under Settings tab, select wp_head in the Hook dropdown list.Under Display Rules tab, select Entire Site in the Location dropdown list.Click the Publish button then we are half way done.
The second block of code should look similar to this:
Go to Appearance > Elements and click 「Add New」. From the Element dropdown list, choose 「Hook」.Copy and paste the your own code into the content area.Under Settings tab, select wp_body_open in the Hook dropdown list and set the Priority to 0.Under Display Rules tab, select Entire Site in the Location dropdown list.Click the Publish button then we are all done.
Using Functions
If you don』t have GP Premium or prefer to use functions, your code should look similar to below:
add_action( 'wp_head', function() {
?>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXX');
<?php
}, 0 );
The functions above need to be added as PHP.