How to add Google Tag Manager in 1 Minute

Guy Eshet
January 19, 2021
Add google tag manager to wordpress in two seconds

Go to Google Tag Manager Setup. You should receive to Javascript code snippet you need to add to your website, one in the header and one just after the body starts.

There are two simple hooks you can use:

add_action('wp_head', 'add_my_code_to_header');
function add_my_code_to_header(){

function add_my_code_right_after_body_starts() {
add_action( 'wp_body_open', 'add_my_code_right_after_body_starts' );

In short, open your theme's functions.php and enter the following code. You can either switch the GTM ID or copy the whole thing:

add_action('wp_head', 'add_google_analytics_code_to_header');
function add_google_analytics_code_to_header(){
<!-- Google Tag Manager -->
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
<!-- End Google Tag Manager -->

function add_right_after_body_starts() {
    <!-- Google Tag Manager (noscript) -->
	<noscript><iframe src=""
	height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
	<!-- End Google Tag Manager (noscript) -->
add_action( 'wp_body_open', 'add_right_after_body_starts' );

Refresh your page and try to verify the installation in Google Tag Manager!

base cta background

The Best of AI Magic

Stay ahead with the latest in Generative AI.
Guy Eshet from Geneartive AI Expert

Guy Eshet

Guy Eshet is an expert in Blockchain technologies, NFTs, and Web development. Guy has been developing software for over 15 years, currently focusing on Web3. Guy is fluent in 5 languages, loves meeting new people, learning about various topics, and improving his skills.
Comment & Share Your Thoughts
Your email will not be published. Required fields are marked *

Recommended For You

footer astronaut  |  All Rights Reserved © 2022Privacy PolicyTerms & Conditions
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.