How to Prevent GTM4WP from Loading on Specific Pages or URLs
Content
Many WordPress site administrators using the GTM4WP plugin seek to optimize their site's performance and tracking accuracy by controlling where the Google Tag Manager scripts are loaded. A common request is to prevent GTM from firing on certain pages, such as landing pages, admin areas, or checkout pages, to avoid duplicate tracking, improve page speed, or comply with privacy regulations.
Why Control GTM Script Loading?
There are several reasons you might want to prevent GTM4WP from loading on specific pages:
- Performance: Reducing unnecessary script loads can improve page speed scores, especially on content-heavy pages where every millisecond counts.
- Duplicate Tracking: Some setups, particularly those with external landing pages integrated into the main site, can cause GTM scripts to load twice, skewing analytics data.
- Privacy Compliance: You may need to exclude tracking on sensitive pages like checkout or user account pages.
- Staging Environments: Preventing tracking on development or staging sites avoids polluting live analytics data with test information.
Common Solutions to Prevent GTM Loading
1. Using the Built-in User Role Exclusion
The GTM4WP plugin includes a built-in feature to exclude tracking based on user roles. This is found in the plugin's Advanced Settings tab. While this doesn't directly address URL-based exclusion, it's useful for preventing tracking of logged-in administrators, editors, and other specific user types.
2. Custom PHP Code for Conditional Loading
For more control over which pages load GTM, you can add custom code to your theme's functions.php file or a custom plugin. The most straightforward method is to check your conditions early and then modify the global plugin options variable.
Here's an example that prevents GTM loading on pages with '/landingpage/' in the URL:
function custom_disable_gtm4wp_on_specific_urls() {
// Check if the current URL contains your target path
if ( strpos( $_SERVER['REQUEST_URI'], '/landingpage/' ) !== false ) {
global $gtm4wp_options;
// Clear the GTM container code to prevent output
$gtm4wp_options[ GTM4WP_OPTION_GTM_CODE ] = '';
}
}
add_action( 'init', 'custom_disable_gtm4wp_on_specific_urls', 11 );
You can modify the condition to target different criteria, such as specific post types, page IDs, or even country codes based on geolocation.
3. Using the gtm4wp_get_the_gtm_tag Filter (Legacy Approach)
Note: In GTM4WP version 1.16, several filters were deprecated. While the gtm4wp_get_the_gtm_tag filter was mentioned in some older solutions, it's important to check the current plugin documentation for supported methods, as this filter may no longer be the recommended approach.
4. Completely Manual GTM Implementation
For maximum control, you can configure GTM4WP to only generate the data layer while handling the GTM container code insertion manually:
- Go to GTM4WP settings → General → Container Placement
- Set the placement option to "Off"
- Manually insert your GTM container code in your theme header or using another plugin
This approach is particularly useful when integrating with cookie consent plugins that require script management based on user consent.
Important Considerations
- Always test changes in a staging environment before deploying to production.
- Be aware that excluding GTM from certain pages means you won't track any user interactions on those pages.
- For WooCommerce sites, consider which pages are essential for tracking (e.g., product views, add to cart events, purchases) before excluding them.
- When using performance optimization plugins that delay JavaScript, ensure they don't interfere with the timing of critical GTM functions.
By implementing these techniques, you can gain precise control over where your GTM container loads, helping to optimize performance while maintaining accurate tracking where it matters most.
Related Support Threads Support
-
Manual loading with shortcodehttps://wordpress.org/support/topic/manual-loading-with-shortcode/
-
GTM and GTMetrixhttps://wordpress.org/support/topic/gtm-and-gtmetrix/
-
Delay or Deferhttps://wordpress.org/support/topic/delay-or-defer/
-
Asset CleanUPhttps://wordpress.org/support/topic/asset-cleanup-5/
-
Inline scripting breaks content-security-policyhttps://wordpress.org/support/topic/inline-scripting-breaks-content-security-policy/
-
Disable tracking on certain pages or other conditionshttps://wordpress.org/support/topic/disable-tracking-on-certain-pages-or-other-conditions/
-
How to set up for multisite usehttps://wordpress.org/support/topic/how-to-set-up-for-multisite-use/
-
Avoid staging site trackinghttps://wordpress.org/support/topic/avoid-staging-site-tracking/
-
Does this host the scripts locally on the website rather than remotely?https://wordpress.org/support/topic/does-this-host-the-scripts-locally-on-the-website-rather-than-remotely/
-
Is there a filter to stop noscript element firinghttps://wordpress.org/support/topic/is-there-a-filter-to-stop-noscript-element-firing/
-
Filter to change add_options_page() capabilityhttps://wordpress.org/support/topic/filter-to-change-add_options_page-capability/
-
Disable GTM4WP for testing via filter or something similarhttps://wordpress.org/support/topic/disable-gtm4wp-for-testing-via-filter-or-something-similar/
-
Plugin is adding UTM parameters to external links.https://wordpress.org/support/topic/plugin-is-adding-utm-parameters-to-external-links/
-
Placing scripts on the footer with Elementorhttps://wordpress.org/support/topic/placing-scripts-on-the-footer-with-elementor/
-
AdSense script in the gtm.js filehttps://wordpress.org/support/topic/adsense-script-in-the-gtm-js-file/
-
Remove Facebook Pixelhttps://wordpress.org/support/topic/remove-facebook-pixel-2/
-
Tag Manager load only in one country?https://wordpress.org/support/topic/tag-manager-load-only-in-one-country/
-
Server Side custom loader within GTM4WPhttps://wordpress.org/support/topic/server-side-custom-loader-within-gtm4wp/
-
Exclude specific order statuseshttps://wordpress.org/support/topic/exclude-specific-order-statuses/
-
Idea – Measure users that blocked GTMhttps://wordpress.org/support/topic/idea-measure-users-that-blocked-gtm/
-
Devo excluir o gtm instalado manualmentehttps://wordpress.org/support/topic/devo-excluir-o-gtm-instalado-manualmente/
-
Website speedhttps://wordpress.org/support/topic/website-speed-22/
-
Load pluginhttps://wordpress.org/support/topic/load-plugin/
-
Prevent GTM/Analytics from running on checkout pagehttps://wordpress.org/support/topic/prevent-gtm-analytics-from-running-on-checkout-page/
-
How can I disable the plugin from inserting GTM code?https://wordpress.org/support/topic/how-can-i-disable-the-plugin-from-inserting-gtm-code/
-
Disable _gl=1* on link to the site for Mutidomain controlhttps://wordpress.org/support/topic/disable-_gl1-on-link-to-the-site-for-mutidomain-control/
-
exclude GTM from admin pagehttps://wordpress.org/support/topic/exclude-gtm-from-admin-page/
-
Exclude certain uris from loadinghttps://wordpress.org/support/topic/exclude-certain-uris-from-loading/
-
Block IPhttps://wordpress.org/support/topic/block-ip-11/
-
1.16 deprecated filtershttps://wordpress.org/support/topic/1-16-deprecated-filters/
-
Prevent GTM scripts to be loaded on certain pages / urlshttps://wordpress.org/support/topic/prevent-gtm-scripts-to-be-loaded-on-certain-pages-urls/
-
User roles to excludehttps://wordpress.org/support/topic/user-roles-to-exclude/
-
YouTube iframe_api + widgetapi.jshttps://wordpress.org/support/topic/youtube-iframe_api-widgetapi-js/
-
tracking non-essential pageshttps://wordpress.org/support/topic/tracking-non-essential-pages/
-
Filter for gtm_auth and gtm_preview for site-specific config fileshttps://wordpress.org/support/topic/filter-for-gtm_auth-and-gtm_preview-for-site-specific-config-files/
-
WebSite Performance and GTM4WPhttps://wordpress.org/support/topic/website-performance-and-gtm4wp/