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

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
- 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>
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' );
?>
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
- 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.
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 →
Make Your Website Competitive.
Leverage our expertise in Website Design + SEO Marketing, and spend your time doing what you love to do!






