Add Google Analytics/Tag Manager via functions.php

There are many WordPress plugins that you can use to add your Google Analytics, Google Tag Manager, Facebook Pixel and so much more to your site. But frankly, you don’t need any kind of plugin for that. You can do this quite easily by adding a simple code snippet to the functions.php.

Access your child’s theme functions.php via FTP or
via Appearance > Theme File Editor > Your child theme > functions.php

There are three places where you want to add the tracking code. When performance is important, I’d suggest adding it to wp_footer but this could result in potential data loss.

Carefully read where you need to place your analytical code. Based on the suggested placement, you can change it in the example code.

  1. Inside the head tag (wp_head)
  2. After the opening body tag (wp_body_open)
  3. Before the closing body tag (wp_footer)

Our HEAD code:

function hoolite_add_analytics_head_js() {
	?>
	<!-- Replace this with your Analytical HEAD code -->
	
	<?php
}
add_action( 'wp_head', 'hoolite_add_analytics_head_js', 11 );

Our BODY code:

function hoolite_add_analytics_body_js() {
	?>
	<!-- Replace this with your Analytical BODY code -->
	
	<?php
}
add_action( 'wp_footer', 'hoolite_add_analytics_body_js', 11 );

If you need to add multiple analytical codes, you can simply re-use the code snippet. But don’t forget to give it a unique name.

Thats it!

Support 🐶

If you found this article helpful, got a question or spotted an error/typo... Do well to leave your feedback in the comment section or help spread it by sharing this article. If you're feeling generous (and I hope you do) you can definitely help me by getting me a cup of coffee ☕.


You may like these too!

Leave a Reply

Your email address will not be published. Required fields are marked *