Plugin Support
Hanna
(@hannausercentrics)
Hi @lukafuka ,
Thanks for your message.
As of now, the plugin doesn’t have a built-in feature to exclude WordPress admins (or any logged-in users) from GTM tracking.
The Cookiebot plugin injects GTM into the <head> for all users when enabled.
Recommended Solution Without Editing the Plugin:
Cookiebot adds GTM using:
add_action('wp_head', array($this, 'include_google_tag_manager_js'), -9997);
To prevent GTM from loading for administrators, you need to remove this specific action only for admin users. Since the plugin uses an object instance, we must detect and remove that exact object.
You can safely add the following code to your functions.php:
add_action('init', function() {
// Only disable GTM for admins
if (!current_user_can('manage_options')) {
return;
}
// Get all wp_head hooks
$wp_head = $GLOBALS['wp_filter']['wp_head'] ?? null;
if (!$wp_head) {
return;
}
// Loop through all callbacks
foreach ($wp_head->callbacks as $priority => $callbacks) {
foreach ($callbacks as $id => $callback) {
// Look for Cookiebot's GTM method
if (is_array($callback['function']) && is_object($callback['function'][0]) && method_exists($callback['function'][0], 'include_google_tag_manager_js') && $callback['function'][1] === 'include_google_tag_manager_js') {
// Remove the GTM injection
remove_action('wp_head', array($callback['function'][0], 'include_google_tag_manager_js'), $priority);
}
}
}
});
What this code does:
- Detects if the current visitor is an administrator
- Finds the exact Cookiebot class instance that injects GTM
- Removes only the GTM output method (
include_google_tag_manager_js)
- Does not affect other Cookiebot scripts (Consent Mode, Banner, CMP, etc.)
- Does not modify the plugin itself
- Continues working after plugin updates
As a result, GTM will load for regular visitors but will not load for logged-in admins.
Additionally, I’ve submitted a feature request to our plugin developers to introduce a new option within the Cookiebot plugin’s GTM section that would make this functionality easier to implement.