How to add Google Analytics in WordPress site without plugin?

Step 1 : Get Your Google Analytics Tracking Code

  1. Go to Google Analytics
  2. Sign in with your Google account.
  3. Click Admin → Data Streams → Web.
  4. Add your website URL and name (example: https://yourdomain.com).
  5. Copy the Measurement ID (looks like G-XXXXXXX).
  6. Under “View tag instructions” → “Install manually”, copy the global site tag (gtag.js) code snippet.

Your code will look like this

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

Step 2. Create a child theme

If you have not created a child theme yet then first create a child theme

Visit to know : How to create child theme in WordPress

If you have already created a child theme then ignore step 2.

Step 3. Add this tracking code to your child themes functions.php

function add_google_analytics_tracking_code() { ?>
    <!-- Google tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', 'G-XXXXXXX');
    </script>
<?php }
add_action('wp_head', 'add_google_analytics_tracking_code');

Replace G-XXXXXXX with your actual Measurement ID.
This will automatically load the tracking script in the <head> of every page.

Step 4: Verify Installation

  1. Visit your website in a new tab.
  2. Go back to your Google Analytics dashboard → Reports → Real-Time.
  3. If tracking is working, you’ll see active users on your site in real-time.

Tip: Keep It Lightweight

  • Avoid using heavy plugins like “Site Kit” if you only need Analytics.
  • Always add code via functions.php in a child theme to prevent loss on theme update.

Adding Google Analytics manually gives you more control, better site speed, and no dependency on plugins.
It’s a one-time setup that works reliably even after theme changes (if done via functions.php).

Leave a Reply

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