How to Add Google Tag Manager or Google Analytics to WordPress Without a Plugin

By: Irina Shvaya | September 5, 2025

Key Takeaways

  • You can add Google Tag Manager or Google Analytics to WordPress without a plugin by editing your child theme's functions.php.
  • Grab the GA4 gtag.js snippet or GTM's header and body snippets from your Google account, then swap in your own tracking or container ID.
  • Use the wp_head hook to inject header scripts and wp_footer for the GTM noscript body snippet.
  • Always work in a child theme so WordPress updates don't erase your custom tracking code.
  • Verify tracking by viewing your page source, using Google Tag Assistant, checking GA real-time reports, and clearing any caching plugins.
Adding Google Tag Manager (GTM) or Google Analytics (GA) to your WordPress website is crucial for tracking visitors and improving your site. You don’t need a plugin—just a few lines of code in your theme’s functions.php. Here’s how to do it safely.
Step 1: Get Your Scripts from Google
Google Analytics 4 (GA4) Example
  1. Go to Google Analytics → Admin → Data Streams → Web → Tagging Instructions → Global Site Tag (gtag.js).
  2. Copy the script that looks like this:
<!-- Global site tag (gtag.js) - Google Analytics -->
<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>
Replace G-XXXXXXX with your tracking ID.
Google Tag Manager (GTM) Example
GTM provides two snippets:1. Header snippet (inside <head>):
<!-- Google Tag Manager -->
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-XXXXXXX');
</script>
<!-- End Google Tag Manager -->
2. Body snippet (immediately after opening <body>):
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
Replace GTM-XXXXXXX with your container ID.
Step 2: Add Scripts to Your WordPress Theme
Open your child theme’s functions.php (never edit the parent theme directly) and paste the following code:
<?php
// Add header scripts (GA or GTM header)
function custom_load_header_scripts() {
?>
<!-- Paste Google Analytics or GTM Header Script Here -->
<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', 'custom_load_header_scripts' );
// Add footer scripts (GTM noscript)
function custom_load_footer_scripts() {
?>
<!-- Paste GTM Body/NoScript Here -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<?php
}
add_action( 'wp_footer', 'custom_load_footer_scripts' );
?>
How to Add Google Tag Manager or Google Analytics to WordPress Without a Plugin Notes:
  • Replace G-XXXXXXX or GTM-XXXXXXX with your own IDs.
  • wp_head adds scripts to <head>.
  • wp_footer adds scripts just before </body>.
  • Use a child theme so updates don’t erase your code.
Step 3: Test Your Setup
  1. Visit your site and view page source to confirm the scripts are there.
  2. Use Google Tag Assistant Chrome extension or GA real-time reports to ensure tracking works.
  3. Clear caching plugins if you don’t see changes immediately.
Bonus Tips
  • Conditional Loading: Only load scripts on specific pages if needed:
if ( is_home() || is_single() ) {
// Insert script only on homepage or single posts
}
  • Performance: Loading scripts asynchronously (async) ensures they don’t slow down your site.
  • Future-Proof: Using hooks instead of editing theme files directly is safer than pasting code in header.php or footer.php.
Step 4: Get Professional Help with eSEOspace
If you want to make sure your Google Analytics or Google Ads are set up correctly and tracking accurately, eSEOspace can help.
We provide full Google Analytics and Google Ads management, including:
  • Accurate setup of GTM and GA on your WordPress site
  • Conversion tracking and event setup
  • Google Ads account configuration and optimization
  • Insights and recommendations to grow your website traffic
Schedule a free consultation today to have our experts handle the setup for you and ensure your analytics are tracking properly from day one.
Conclusion
Adding Google Analytics or Tag Manager without a plugin is fast, clean, and safe. By inserting scripts through wp_head and wp_footer in your child theme, you maintain control over your tracking codes while keeping your site lightweight.
And if you want to save time and ensure accuracy, eSEOspace is here to help with full Google Analytics and Google Ads management.

Get a FREE Audit

We'll perform a comprehensive SEO, AEO, GEO & CRO audit of your website — completely free — and show you exactly how to outrank your competitors.

Don't have a site yet? Get in touch →

Frequently Asked Questions

Do I need a plugin to add Google Analytics to WordPress?
No. You can add Google Analytics or Tag Manager without any plugin by pasting a few lines of code into your child theme's functions.php. Using the wp_head and wp_footer hooks keeps your site lightweight, gives you direct control over your tracking codes, and avoids the extra overhead a dedicated plugin would add.
Where do I get the tracking scripts from Google?
For GA4, go to Google Analytics, then Admin, Data Streams, Web, Tagging Instructions, and copy the Global Site Tag (gtag.js). For Tag Manager, GTM provides two snippets: a header snippet for the head section and a body snippet placed after the opening body tag. Replace the placeholder ID with your own.
Why should I use a child theme instead of editing the main theme?
Editing the parent theme directly is risky because a theme update will overwrite your changes and erase your tracking code. Placing your scripts in a child theme's functions.php keeps them safe through updates. Using hooks like wp_head and wp_footer is also safer than pasting code directly into header.php or footer.php.
How do I confirm my tracking is actually working?
Visit your site and view the page source to confirm the scripts appear. Use the Google Tag Assistant Chrome extension or check GA4 real-time reports to verify data is flowing. If you don't see changes right away, clear any caching plugins, since cached pages can serve the old version without your new code.
Can I load the tracking scripts only on certain pages?
Yes. You can wrap the script in a conditional so it only loads where needed, for example using is_home() or is_single() to target the homepage or single posts. Loading scripts asynchronously with the async attribute also helps ensure your tracking code doesn't slow down your site's performance.

Put this into action with eSEOspace

We help businesses grow with website development that actually performs. Explore the services behind this guide:

Book a free strategy call →

Get a FREE GEO/AEO/SEO Audit

We'll analyze your site's SEO, GEO, AEO & CRO — completely free — and show you exactly how to get found across Google and AI answers.

Don't have a site yet? Get in touch →

You Might Also like to Read