Home Page Blog How to Add GTM Code in WordPress (GTM or GA4) How to Add GTM Code in WordPress (GTM or GA4) WordPress Last Updated: 24/04/2025 Share on Social Media: 1,090 11 min. No matter how low- or no-code your website builder is, learning how to add code to WordPress is often essential for the design and customizability of your website. Setting up tracking features can be done in just a few clicks, making it accessible even for those without technical expertise. So, let us help you a bit with this task through developer insights. We’ll focus mainly on how to add Google Tag Manager code in WordPress, as well as Google Analytics. Introduction to Tag Management Tag management is the process of organizing and managing tags, which are small pieces of code used to track user interactions on a website. Google Tag Manager (GTM) is a popular tag management tool that allows website owners to efficiently manage and track user behaviors, such as clicks, scrolls, and form submissions, on their WordPress website. By using Google Tag Manager, website owners can gain valuable insights into user behaviors and make data-driven decisions to improve their website’s performance. To get started with tag management, you need to create a Google Tag Manager account and install Google Analytics or Google Tag Manager code on your WordPress site. This setup enables you to track user interactions and gather analytical data without the need to manually add tracking codes to your site. Google Tag Manager simplifies the process of managing multiple tags, ensuring that your website remains organized and efficient. Preparing Your WordPress Site for Code Integration Before integrating Google Tag Manager (GTM) or Google Analytics 4 (GA4) into your WordPress site, it’s essential to prepare your site to ensure the process goes smoothly and without any issues. Here are the key steps to get your site ready: Using tools like Google Tag Manager allows you to manage tracking without needing to edit code directly. Ensuring Your WordPress Site is Updated and Backed Up Make sure your WordPress site is running the latest version. This not only ensures compatibility with the latest tools but also keeps your site secure. Check that your theme and all plugins are up-to-date. Outdated themes or plugins can cause compatibility issues or conflicts with Google Tag Manager or Google Analytics. Using plugins like Google Site Kit can help keep your site updated and secure. Before learning how to add code to a header in WordPress (or to any other space), it’s crucial to have a recent backup. Use a reliable backup plugin or your hosting provider’s feature to create a full backup of your site, including the database, themes, plugins, and media files. Checking for Theme and Plugin Compatibility If possible, test on a staging site first. This will help you identify any conflicts or issues before going live. Some themes or plugins, especially those related to SEO or analytics, might have specific settings or features that can conflict with Google Tag Manager or GA4. Check their documentation or reach out to the developers for any known compatibility issues. Also, linking Google Ads accounts with Google Analytics is required for form conversion tracking. If you’re looking to add custom code, knowing how to change WordPress pages is crucial. This is not just for adding tracking codes like GTM or GA4, but also for overall site customization. Make sure the plugins you plan to use for the integration are compatible with your WordPress version, theme, and other plugins. By preparing your WordPress site well, you can create a stable and compatible environment to learn how to add Google Analytics code in WordPress. This proactive approach will avoid potential problems and ensure your Google Tag Manager and Google Analytics account integration gives you accurate and reliable data for your website analytics. Step Up Your Web Game – Hire ProCoders for State-of-the-Art WordPress Development! Hire Now! technologies Which technologies do you use? Angular Laravel React Symfony Node.js Vue.js Ruby on Rails React Native Ionic .NET Python Web3 Solidity Or just type your tech stack here: expertiese Please choose the experience level: Intermediate Senior Finish Enter your email and check how fast our responses are;)* I'm just checking the prices/options and don't have a need right now. Please don't try to reach out. I have read and agree to the Website Terms of Use and Privacy Policy. Previous Next Setting Up Google Tag Manager Setting up Google Tag Manager is a few easy steps, and you’ll be up and running in no time. Here’s how: Create a Google Tag Manager Account Go to the Google Tag Manager website and click on the “Create Account” button. Fill in the required information, such as your account name, country, and container name (usually your website’s URL). Click on the “Create” button to finish the account setup. Install the Google Tag Manager Code on Your WP Website After creating your account, you will be given a container code snippet. You can install the Google Tag Manager code manually by adding it to your website’s header and body sections. To do this, go to your WordPress dashboard, navigate to ‘Appearance’ > ‘Theme Editor’, and add the Google Tag Manager code in the header.php and body.php files. Or you can use a plugin like “Insert Headers and Footers” or the “Google Tag Manager” plugin to add the Google Tag Manager code without editing theme files. Just install and activate the plugin, then paste the GTM code in the appropriate fields in the plugin settings. Now you have Google Tag Manager set up on your WP site and ready to use tracking tools for user interactions and gather data. Method 1: Adding Elements Directly to WordPress Theme Files Adding Google Tag Manager (GTM) or Google Analytics 4 (GA4) elements directly to your WordPress theme files is easy. But you have to learn how to add tracking code to WordPress to avoid any issues. Here’s a step-by-step guide: It’s important to place the Google Tag Manager tag right after the opening body tag to ensure proper tracking functionality and compliance with GTM’s setup requirements. Step-by-Step Guide on How to Add Snippets to header.php or footer.php Get the GTM or GA4 snippet from your Google account. You can find the code snippets in the Google Tag Manager tab. In your WordPress dashboard, go to ‘Appearance’ > ‘Theme Editor’. Select the active theme from the list on the right. Find header.php or footer.php in the theme files list. Depending on where you are instructed to place the code (usually in the header). Click on the file to open the editor. Add the Google Tag Manager or GA4 code snippet as instructed by Google. Usually, GA4 codes go right after the opening <head> tag, and GTM codes just after the opening <body> tag. Be careful not to overwrite or remove any existing content. Save your changes by clicking ‘Update File’. Why Use a Child Theme to Prevent Losses on Theme Updates When you update a WordPress theme, it overwrites the theme files. If you add the code directly to the parent theme’s header.php or footer.php, you will lose the changes when the theme updates. To prevent this, use a child theme. A child theme inherits the functionality of the parent theme but allows you to make changes to theme files without losing them on updates. Using a child theme also ensures your Google Tag Manager container remains intact during theme updates. If you don’t have a child theme, you can create one or use a child theme generator. Once set up, replicate the header.php or footer.php file in your child theme and add the elements there. By following these steps and using a child theme, you can safely learn how to add code to the header in WordPress. This method ensures your tracking and analytics setup remains intact when you update your theme, and you get continuous data collection for your site analytics. Craft the Perfect Online Experience – Partner with ProCoders for WordPress Mastery! Let’s Get Started! Let’s get started Method 2: Using Plugins for Code Insertion Plugins are the juice! You can integrate Contact Form 7 with Salesforce for automation and learn how to integrate Hubspot with WordPress with one, and learn how to add widgets to WordPress with another. If you don’t want to edit theme files directly, WordPress has plugins specifically designed for inserting snippets like Google Tag Manager or GA4. They provide a user-friendly interface and ensure your code persists through theme updates. Plus, using a WordPress analytics plugin can simplify the process of adding tracking codes. Introduction to Plugins that Facilitate Code Insertion Plugins like “Insert Headers and Footers” are popular choices for adding snippets to your WP site. They allow you to add any code or script, including Google Analytics or Google Tag Manager, without editing your theme files directly. These plugins make adding code snippets to your WP site easy. How to Install and Use These Plugins to Add GTM or GA4 Elements In your WordPress dashboard, go to ‘Plugins’ > ‘Add New’. Search for “Insert Headers and Footers” or a similar plugin. Click ‘Install Now’ and then ‘Activate’ the plugin. Once activated, go to the plugin settings. This can usually be found under ‘Settings’ in your WordPress dashboard. In the plugin settings, you will find fields to add scripts in the header, body, or footer of your site. Copy and paste your GTM or GA4 snippet into the appropriate section as instructed by Google. For example, GA4 code goes in the header, while Google Tag Manager might require the code in both the header and body sections. Save or update the settings to apply the changes. These plugins allow you to manage tags without editing the website’s code directly. After adding the code, check if it’s working. You can use Google’s tools or check the page source to see if the changes appear in the site’s HTML. Using a plugin to add Google Analytics to WordPress is a more efficient and less technical way than editing theme files. It’s ideal for users who aren’t comfortable with coding or for those who want a quick and straightforward way to manage scripts on their WP site. Additionally, this method protects your changes from being overwritten by theme updates, ensuring the longevity of your tracking and analytics setup. Method 3: Using Google Tag Manager to Manage Snippets Using Google Tag Manager (GTM) makes managing and deploying marketing and analytics tags on your WordPress site without editing site code. Setting Up Google Tag Manager: Create a GTM Account: Sign in at the Google Tag Manager website with your Google account, create an account with your company’s name, and set up a container for your tags, naming it after your website. Adding Google Tag Manager to WordPress: Direct Insertion: Add the GTM container code to your theme’s header.php and body.php files, or use a child theme. Using a Plugin: Alternatively, use plugins like “Insert Headers and Footers” to insert the GTM code without editing theme files. Managing Tags in GTM: Verifying Your Insertion: After installing Google Tag Manager (GTM) or Google Analytics 4 (GA4) code on your WP site, make sure to verify that the installation is correct and the element is working. This ensures you’re tracking correctly and gathering data. Add and Configure Tags: In the GTM dashboard, add new tags (like Google Analytics) and set triggering rules. Testing and Publishing: Test tags in GTM’s preview mode and publish them to go live on your site. Make sure to publish tags to ensure proper tracking and integration with other marketing tools. Ongoing Management: GTM allows for easy addition, updating, and disabling of tags. GTM is a streamlined, error-reducing solution for managing multiple tags, keeping your website setup clean and efficient. Make Your Website a Market Leader – Rely on ProCoders’ WordPress Expertise! Get Started! Let’s get started Google Tag Manager Configuration Once you have Google Tag Manager installed on your WordPress site, the next step is to configure it by setting up tags, triggers, and variables. Here’s how: Tags: Tags are used to track specific user interactions, like clicks, form submissions, and page views. To create a new tag, go to the Google Tag Manager dashboard, click on “Tags” in the left-hand menu, and then click on the “New” button. Choose the type of tag you want to create, like a Google Analytics tag for page views or a Google Ads tag for conversions. Triggers: To create a trigger, go to the “Triggers” section in the GTM dashboard, click on “New”, and define the conditions under which the tag should be triggered. Variables: Variables store values that can be used in tags and triggers. They can be built-in or custom variables that you create. To set up variables, go to the “Variables” section in Google Tag Manager and either enable built-in variables or create new ones as needed. By setting up tags, triggers, and variables, you can customize Google Tag Manager to track a wide range of user interactions on your website. This setup will help you gather detailed analytics data to make informed decisions to improve your website’s performance and user experience. Tools and Methods to Verify GTM or GA4 Code is Installed and Running Use Google Tag Assistant, a Chrome extension, to verify the installation of GTM and GA4. It shows if the tags are firing on your site. Go to your site and click the Tag Assistant icon in your browser to see the status of your tags. The Google Tag Manager dashboard is the key to managing and installing tracking codes effectively. In GA4, go to the ‘Real-time’ report to see if the analytics code is collecting data. Browse your site after opening this report and look for active users, which means it’s working. Also, use GTM’s built-in preview mode to test your tags. This mode allows you to simulate tag firing and troubleshoot any issues before publishing the tags live. Troubleshooting Common Issues with Code Installation Code Not Firing If tags are not firing, make sure you’ve placed the GTM container code correctly in your WordPress site, either directly in theme files or via a plugin. Tracking tags correctly is crucial for data collection. Check for any typos or errors in the snippet. Conflicting Plugins Some WordPress plugins, especially caching or security plugins, may interfere with GTM or GA4 code. Try disabling such plugins and cleaning the server and website cache to see if that solves the issue. Incorrect Tag Configuration in GTM In GTM, make sure your tags are set up correctly with the right trigger conditions. Incorrect triggers can prevent tags from firing. Browser Issues Try your site in different browsers to rule out browser issues. Check if any browser extensions are blocking the code. Console Errors Use your browser’s developer tools to check the console for any JavaScript errors that might be affecting the GTM or GA4. Security Considerations When Adding Third-Party Elements to Your Site When adding third-party code like Google Tag Manager or custom HTML to your site, remember these: Source Validation: Use code, scripts, or plugins from trusted sources, updated and maintained. Access and Permissions: Limit editing to trusted admins and grant minimal permissions. Regular Updates and Audits: Keep all third-party code up-to-date and do periodic reviews to remove or replace outdated scripts. Web Application Firewall (WAF): Use a WAF for protection and active monitoring. Regular Backups: Do frequent backups to restore your site in case of security issues. Staging Environment Testing: Test new elements in the staging environment before going live to identify potential issues. Caution with Script Permissions: Understand each script’s capabilities, especially those interacting with the database or modifying site content. Secure Coding Practices: Make sure custom code follows secure practices, data validation, and sanitization. Accepting Data Processing Terms: When setting up Google Tag Manager, make sure you accept the data processing terms as part of the Terms of Service Agreement required by GDPR. Following these guidelines will help you minimize risks and keep your site’s integrity and trustworthiness while adding third-party functionalities. Leading Edge WordPress Development – Hire ProCoders to Outshine the Competition! Let’s Hire! technologies Which technologies do you use? Angular Laravel React Symfony Node.js Vue.js Ruby on Rails React Native Ionic .NET Python Web3 Solidity Or just type your tech stack here: expertiese Please choose the experience level: Intermediate Senior Finish Enter your email and check how fast our responses are;)* I'm just checking the prices/options and don't have a need right now. Please don't try to reach out. I have read and agree to the Website Terms of Use and Privacy Policy. Previous Next FAQ Where do I put custom HTML code in WordPress? For small HTML snippets use the ‘Custom HTML’ widget under ‘Appearance’ > ‘Widgets’. For HTML in your content use the ‘Custom HTML’ block in the Gutenberg page or post editor. For advanced placement edit theme files like header.php or footer.php, preferably in a child theme so you won’t lose changes during theme updates. How to add Google Tag Manager code in WordPress? To add Google Tag Manager elements in WordPress you can either manually insert the code snippets in your theme files (the first part after the opening <head> tag in header.php, and the second part after the opening <body> tag) or use plugins like “Insert Headers and Footers” or “WP Insert Code” which allows you to add elements without editing theme files. How to add code to body tag in WordPress? To add code to body tag in WordPress, either edit the header.php file of your theme and add the code after the opening <body> tag or use a plugin like “Insert Headers and Footers” to add everything without editing theme files. How to add schema code in WordPress? To add schema in WordPress, either manually insert it in the HTML using the ‘Custom HTML’ block in the page or post editor or add it to the header.php file for site-wide schema. Alternatively use SEO plugins like ‘Yoast SEO’, ‘Rank Math’, or ‘Schema Pro’ to manage schema markup without manual editing. Conclusion When adding third-party tools like Google Tag Manager or custom HTML to your site, prioritize security. Choose trusted sources for elements, limit access permissions, update and audit third-party scripts, and use security measures like Web Application Firewalls. Understanding user behavior is key to website performance and user experience. Test new elements in the staging environment, backup your site regularly, and follow secure coding practices. These are crucial for your site’s security, user trust, and performance, so you can add more to your site without compromising security and stability. WordPress 1,090 Posted: 2/01/2024 Last Updated: 24/04/2025 Previous postHow To Unpublish WordPress Site: Step by Step Guide Next postWhat is Elementor WordPress: Definition, Pros, and Cons and Why Should We Use It Write a Reply or Comment Cancel replyYour email address will not be published. Required fields are marked * This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.Post