
This guide is part of our WordPress resource hub: costs and hosting, the block editor, plugins, SEO and speed, security, maintenance, WooCommerce, development, comparisons and migrations.
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 → your stream → View tag instructions → Install manually.
- Copy the script that looks 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>
<!-- 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' );
?>

- 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











