Character holding a wordpress logo, the character is referenced as GA tracking data

How to add GA4 to WordPress without plugin

Sometimes, you end up with a bunch of plugins or you want to avoid unnecessary plugins due to security issues. Most websites these days have a tracking system to keep an eye on their stats. So, in this tutorial, I’ll show you how to install Google Analytics using the functions.php file. It’s going be easy!


  • Know how to access the functions.php file from your theme
  • Know how to get your GA4 code

Open your theme functions.php file; copy and paste the code below replacing YOURCODE with your Google Analytics code.
To get started, open up your theme’s functions.php file. Once you’ve got it open, simply copy and paste the code provided below.

Make sure to replace the placeholder “YOURCODE” with your actual Google Analytics code.

add_action('wp_head','md_google_analytics', 10);

function md_google_analytics() {
  <script src="" async></script>
    window.dataLayer = window.dataLayer || [];
    function gtag(){dataLayer.push(arguments);}
    gtag('js', new Date());
    gtag('config', 'G-YOURCODE');

Save the file and that’s it.

I hope this tutorial helps you.