Google Tag Manager is one of the first tools I install on a new WordPress website. It gives me one place to manage tracking scripts, conversion tags, and analytics tools. After the initial setup, I can add or update most tracking tags without editing website files again.
Many WordPress users install Google Tag Manager through a plugin. I take a different route. I prefer adding the GTM code directly through the functions.php file. This method keeps my website lean and gives me full control over where the code loads.
I have used this setup on business websites, eCommerce stores, lead generation websites, and custom WordPress projects. It works well, requires little maintenance, and removes the need for another plugin.
This guide explains how I install Google Tag Manager through functions.php and why I use this method on most WordPress websites.
What is Google Tag Manager?
Google Tag Manager, often called GTM, is a free tag management platform from Google. It allows website owners to add and manage tracking scripts from a central dashboard.
Without GTM, every new tracking tool requires direct changes to website code. With GTM, I install one container code on the website. After that, I can manage most tracking tags from the GTM interface.
I commonly use GTM to deploy:
- Google Analytics 4
- Google Ads Conversion Tracking
- Meta Pixel
- LinkedIn Insight Tag
- Microsoft Advertising Tracking
- Custom Event Tracking
Google Tag Manager saves time and keeps tracking management organized.
Benefits of Google Tag Manager
Google Tag Manager offers several practical benefits.
- Centralized tag management
- Faster deployment of tracking scripts
- Less dependence on developers
- Better control over analytics tracking
- Support for multiple advertising platforms
- Easier event and conversion tracking
For websites that depend on marketing data, GTM has become a standard tool.
Why Use functions.php for GTM Installation?
This is the main reason I wrote this guide.
WordPress offers many GTM plugins. Most of them perform the same task. They insert Google’s code into the website and provide a few configuration options.
I rarely use those plugins.
Google Tag Manager requires only two snippets of code. Installing an entire plugin for two code snippets feels unnecessary. Every plugin adds files, database entries, settings pages, update notifications, and compatibility checks.
A single plugin does not create a major performance issue. A website with twenty unnecessary plugins often does.
Adding GTM through functions.php keeps the installation simple.
I prefer this method for several reasons:
- No extra plugin to manage
- No plugin update requirements
- No plugin conflicts
- Direct control over code placement
- Cleaner WordPress installation
- Reduced plugin dependency
I also know exactly where the code lives. If I need to troubleshoot tracking issues, I can inspect one function instead of reviewing plugin settings.
Many GTM plugins offer advanced features. Most websites never use those features. They only need the GTM container installed correctly.
For custom WordPress projects, agency websites, and business websites, I find the direct code method easier to maintain over time.
Google Tag Manager Code Structure
Google provides two code snippets for installation.
The first snippet loads the GTM container. This code belongs inside the <head> section.
The second snippet is a fallback version. Google recommends placing it immediately after the opening <body> tag.
I install both snippets on every website. This setup follows Google’s recommended implementation method.
Step 1: Open functions.php File
The first step is opening your active theme’s functions.php file.
Method 1: WordPress Theme Editor
You can access the file from the WordPress dashboard.
Navigate to:
Appearance → Theme File Editor → functions.php
This method works well for small edits.
Method 2: Using FTP or Hosting File Manager
You can access the same file through your hosting account or FTP software.
Locate the file here:
/wp-content/themes/your-theme/functions.php
I usually edit theme files through FTP or a code editor. This method provides better version control and backup options.
Use a child theme whenever possible. Theme updates can overwrite changes made directly to a parent theme.
Step 2: Add GTM Script in Head Section
The first GTM code snippet must load inside the <head> section.
Add the following code to your functions.php file.
// Add GTM script in <head>
function add_gtm_head_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=GTM-YOURID'+dl;
f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-P5ZQ648');
</script>
<!-- End Google Tag Manager -->
<?php
}
add_action('wp_head', 'add_gtm_head_script');
I use the wp_head hook for this task. WordPress inserts the script automatically on every page that loads the standard theme header.
The code stays separate from template files, which makes future maintenance easier.
Step 3: Add GTM Noscript Code After Body Tag
The second GTM snippet belongs immediately after the opening <body> tag.
Add the following function below the previous code.
// Add GTM noscript right after <body>
function add_gtm_body_script() {
?>
<!-- Google Tag Manager (noscript) -->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-YOURID"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
}
add_action('wp_body_open', 'add_gtm_body_script');
The wp_body_open hook inserts the code at the correct location. This follows Google’s installation guidelines and keeps the implementation clean.
Step 4: Verify Theme Support for wp_body_open
Modern WordPress themes support the wp_body_open hook.
I still check for it during installation. Some older themes do not include the hook.
If your theme lacks support, add the following line to header.php immediately after the opening <body> tag.
<?php do_action('wp_body_open'); ?>
This single line allows WordPress to output the GTM noscript code in the correct location.
Step 5: Save Changes
After adding both functions, save the file and clear any cache.
Complete the following tasks:
- Save the updated file.
- Clear your caching plugin cache.
- Clear server-side cache.
- Refresh the website.
The GTM code should now load across the entire website.
Step 6: Verify GTM Installation
I always verify the installation before creating tags or triggers.
A two-minute check can prevent hours of troubleshooting later.
Method 1: View Page Source
Open your website and select:
View Page Source
Search for:
GTM-YOURUD
You should see the GTM script in the head section and the noscript version near the opening body tag.
Method 2: Use Google Tag Assistant
Google Tag Assistant provides a faster verification process.
Install the browser extension and visit your website.
The extension will confirm whether the GTM container loads correctly.
Common Mistakes to Avoid
Most GTM installation issues come from a few common mistakes.
Adding GTM Multiple Times
This problem appears frequently during website audits.
Do not install GTM through multiple methods.
Avoid combining:
- GTM plugin
- Theme settings
- functions.php implementation
Duplicate installations create duplicate page views, duplicate events, and inaccurate reporting.
Editing Parent Theme Directly
Always use a child theme for custom code.
Theme updates can remove modifications made to a parent theme.
Missing wp_body_open Hook
Older themes often miss this hook.
Without it, WordPress cannot output the GTM noscript code at the correct location.
Should You Use a Plugin or functions.php?
Both methods work.
The right choice depends on how you manage your website.
Use functions.php if:
- You prefer a lightweight setup
- You manage custom WordPress websites
- You want direct control over implementation
- You want fewer plugin dependencies
- You are comfortable editing theme files
Use Plugin if:
- You prefer a visual interface
- You do not edit theme files
- You need plugin-specific tracking features
- You manage tracking through non-technical teams
For most custom websites, I choose the functions.php method.
Final Thoughts
I use functions.php for Google Tag Manager installation for one simple reason. It does the job without adding another plugin.
The setup takes only a few minutes. Maintenance requirements stay low. The code remains easy to find and update.
WordPress websites often accumulate plugins over time. Removing unnecessary plugins keeps administration simpler and reduces future maintenance work.
Google Tag Manager already centralizes tracking management. Installing it through functions.php keeps the WordPress side just as clean.
After the container is installed, all future tag management happens inside Google Tag Manager. The website code rarely needs another tracking-related change.
