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

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.
- Go to Google Analytics → Admin → Data Streams → Web → Tagging Instructions → Global Site Tag (gtag.js).
- 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>
<!-- 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 -->
<!-- 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) -->
<?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' );
?>
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.
- Visit your site and view page source to confirm the scripts are there.
- Use Google Tag Assistant Chrome extension or GA real-time reports to ensure tracking works.
- Clear caching plugins if you don’t see changes immediately.
- 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.
- 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
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?
Where do I get the tracking scripts from Google?
Why should I use a child theme instead of editing the main theme?
How do I confirm my tracking is actually working?
Can I load the tracking scripts only on certain pages?
Put this into action with eSEOspace
We help businesses grow with website development that actually performs. Explore the services behind this guide:
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 →
Great — your audit is on the way!
We'll send your free SEO/GEO/AEO/CRO audit within the next few hours. Where should we send it?
You're all set! ✓
Your free audit is being prepared — check your inbox in the next few hours. Talk soon!






