How to Add Google Analytics to WordPress

Home WordPress How to Add Google Analytics to WordPress

February 21, 2022

In this WordPress tutorial I will show you how I like to add Google Analytics code to all our websites.

Code Snippets Link:
https://wordpress.org/plugins/code-snippets/

Timestamps:

  • 0:00 Introduction
  • 1:00 Add code to Code Snippets
  • 3:15 Frontend testing

Code Snippet:

				
					add_action( 'wp_head', function () { ?>

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-X"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-X');
</script>

<?php }, 1 );
				
			

*Some of the links on this page are affiliate links, meaning we may receive a commission if you follow them. This allows us to continue providing free content and educational resources for you. Thank you for supporting our small business!

More Free Resources

Join Our Community

Join now to receive helpful resources, tips, and tutorials in your inbox.

Is it time for a website upgrade?

Is it time for a website upgrade?

Are you giving off the right first impression? Take the quiz to find out how your site measures up.