Step 1 : Get Your Google Analytics Tracking Code
- Go to Google Analytics
- Sign in with your Google account.
- Click Admin → Data Streams → Web.
- Add your website URL and name (example:
https://yourdomain.com). - Copy the Measurement ID (looks like
G-XXXXXXX). - 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
- Visit your website in a new tab.
- Go back to your Google Analytics dashboard → Reports → Real-Time.
- 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.phpin 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