Skip to content
Facebook Twitter Github RSS
Google Tag Manager for WordPress
  • Home
  • FeaturesExpand
    • Basic features of GTM4WP

      You can use many post or page attribute in you Google Tag Manager setup even for custom post types and taxonomies.

      Basic data

      Page / post attributes

      Site search

      User data

      Multisite data

      3rd party data

      Browser attributes

      Device attributes

      Operating system attributes

      Weather conditions

  • SetupExpand
    • Setup advanced GTM4WP features

      Integrate your measurement with WooCommerce, setup Google Analytics 4, Google Ads remarketing or conversion tracking maximizing capabilities.

      WooCommerce related

      Google Analytics 4

      Google Ads – Dynamic Remarketing

      Google Ads – Conversion tracking

      Google Ads – Enhanced Conversions

      Other setup articles

      Exclude WordPress admins

      Embedded media players

  • DevelopersExpand
    • Information for developers and WP managers

      There are several tools in GTM4WP to support your special needs and to adjust plugin behavior to your website or WooCommerce shop.

      Do you want to contribute with a bugfix or a new feature? Visit the GTM4WP’s GitHub page!

      For Developers

      Actions and filters

      WP themes + Proper ecommerce tracking

      For WP managers

      Setup GTM Environments

      Hard code GTM parameters

  • Blog
  • GDPR
Download
Google Tag Manager for WordPress
Home / GTM4WP For Developers / Actions and filters in GTM4WP for developers

Actions and filters in GTM4WP for developers

GTM4WP filters and actions for developers

WARNING!

This page might include outdated information or missing hook names. It will be updated soon to list everything from the latest plugin version.

As a developer you can change the behavior of GTM4WP in many ways. On this page, you can read more about all WordPress actions and filters provided by GTM4WP. If you are new to WordPress hooks, read the official documentation first.

  • gtm4wp_soundcloud filter
  • gtm4wp_vimeo filter
  • gtm4wp_youtube filter
  • gtm4wp_integrate-wpcf7 filter
  • gtm4wp_event-form-move filter
  • gtm4wp_scroller-enabled filter
  • gtm4wp_integrate-woocommerce-track-classic-ecommerce filter (deprecated)
  • gtm4wp_integrate-woocommerce-track-enhanced-ecommerce filter
  • gtm4wp_get_the_gtm_tag filter
  • gtm4wp_add_global_vars filter
  • gtm4wp_compile_datalayer filter
  • gtm4wp_after_datalayer filter
  • gtm4wp_eec_product_array filter
  • gtm4wp_eec_cart_item filter
  • gtm4wp_eec_order_item filter

gtm4wp_soundcloud filter

By default, the JavaScript file necessary to track embeded Soundcloud players is loaded in the footer of the page source. You can change this by creating a filter function and returning a boolean false.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return false; // false => script loader will be placed in the <head> section instead of the footer
}

gtm4wp_vimeo filter

By default, the JavaScript file necessary to track embeded Vimeo players is loaded in the footer of the page source. You can change this by creating a filter function and returning a boolean false.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return false; // false => script loader will be placed in the <head> section instead of the footer
}

gtm4wp_youtube filter

By default, the JavaScript file necessary to track embeded YouTube players is loaded in the footer of the page source. You can change this by creating a filter function and returning a boolean false.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return false; // false => script loader will be placed in the <head> section instead of the footer
}

gtm4wp_integrate-wpcf7 filter

By default, the JavaScript file necessary to track Contact Form 7 forms is loaded in the footer of the page source. You can change this by creating a filter function and returning a boolean false.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return false; // false => script loader will be placed in the <head> section instead of the footer
}

gtm4wp_event-form-move filter

By default, the JavaScript file necessary to track form field moves is loaded in the footer of the page source. You can change this by creating a filter function and returning a boolean false.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return false; // false => script loader will be placed in the <head> section instead of the footer
}

gtm4wp_scroller-enabled filter

By default, the JavaScript file necessary to track user scrolls is loaded in the footer of the page source. You can change this by creating a filter function and returning a boolean false.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return false; // false => script loader will be placed in the <head> section instead of the footer
}

gtm4wp_integrate-woocommerce-track-classic-ecommerce filter (deprecated)

By default, the JavaScript file necessary to track standard ecommerce in WooCommerce is loaded in the header of the page source. You can change this by creating a filter function and returning a boolean true.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return true; // true=> script loader will be placed in the footer of the page instead of the <head>
}

gtm4wp_integrate-woocommerce-track-enhanced-ecommerce filter

By default, the JavaScript file necessary to track enhanced ecommerce in WooCommerce is loaded in the header of the page source. You can change this by creating a filter function and returning a boolean true.

Filter function has to declare 1 parameter of the type boolean:

function my_filter( bool $default_js_placement ): bool {
// do something
return true; // true=> script loader will be placed in the footer of the page instead of the <head>
}

gtm4wp_get_the_gtm_tag filter

Sometimes you need to change how the generated Google Tag Manager container code looks like. This filter will get one string parameter that will hold the GTM container code. Replace what you need to replace or append additional HTML code and return a new string.

The input parameter will include the surrounding <script> element as well.

The same filter will be called for the <noscript> part of the container code. You can check what part of the container code is currently processed by checking the input string and looking at the <noscript> text. If it is present, you are currently manipulating the <noscript> code that is usually added after the opening <body> tag. If it is not present, you can change the main container code located in the <head> section.

Filter function has to declare 1 parameter of the type string:

function my_filter( string $gtm_tag ): string {
// do something with the $gtm_tag variable
return $gtm_tag;
}

gtm4wp_add_global_vars filter

GTM4WP creates some global JavaScript variables in order to store plugin parameters for various plugin features. This <script> block is setup to be added in the <head> section as high as possible.

Use this filter to add more variables into this block. Do not return your own <script> block itself but a valid JavaScript code that will be added to the existing <script> block generated by GTM4WP.

Filter function has to declare 1 parameter of the type string:

function my_filter( string $additional_variables ): string {
// do something with the $additional_variables variable
$additional_variables .= ' const my_global_var=false;';
return $additional_variables;
}

gtm4wp_compile_datalayer filter

GTM4WP uses this filter to create the data layer located in the <head> section (before the main GTM container code).

You can hook into this filter to add you own data layer variables on page load. Your filter function should return an associative array that can have embedded arrays as well. This will be converted to a JavaScript object using json_encode().

Filter function has to declare 1 parameter of the type array:

function my_filter( array $dataLayer ): array {
// do something with the $dataLayer variable
$dataLayer["myCustomVar"] = 'Hello World!';
return $dataLayer;
}

gtm4wp_after_datalayer filter

This filter is used by GTM4WP internally to add the Google Optimize code after the initial data layer declaration, before the Google Tag Manager container starts to load. You can also use this filter to add your HTML (including JavaScript codes) between the data layer declaration and the main GTM container code in the <head> section. If you add JavaScript code, do not forget to also echo the surrounding <script> tag.

Filter function has to declare 1 parameter of the type string:

function my_filter( string $after_code ): string {
$after_code .= '
<script>
// my code between the data layer and the GTM container code
</script>';
return $after_code;
}

gtm4wp_eec_product_array filter

Applied after GTM4WP prepared a product array in any GA3 version enhanced ecommerce code. Use this filter to extend the product array with your own custom dimensions and metrics or to update existing product attributes. Standard product attributes will be converted to GA4 product attributes automatically.

The filter will be called with each product separately therefore if it is for example called in the cart and there are 3 products in the cart, the filter will be applied 3 times.

The filter function will also receive the ecommerce action in which the product array is currently processed.

function my_product_filter( array $eec_product, string $ecommerce_action ): array {
// add an attribute only on product detail pages
if ( $ecommerce_action === "productdetail" ) {
$eec_product["dimension1"] = "Hello World"; // set a product scoped custom dimension
$eec_product["name"] .= " - Order today!"; // append string to existing product attribute
}

return $eec_product;
}

Standard product attribute array format:

array(
"id" => 213,
"name" => "My sample product",
"sku" => "SAMPLE_123",
"category" => "My category",
"price" => 12.00,
"stocklevel" => 2,
"brand" => "My brand",
"variant" => "blue"
);

Possible values of $ecommerce_action:

  • purchase: order received page
  • cart: cart page
  • productdetail: product detail page
  • readdedtocart: user clicked on the “Undo” link on the cart page after removing an item
  • addtocartsingle: product added to cart
  • widgetproduct: product shown in a sidebar widget
  • productlist: product shown in a product list (category page or special product list like ‘New products’)
  • groupedproductlist: product shown on a product detail page of a grouped product

gtm4wp_eec_cart_item filter

Sometimes there can be items in the cart that does not need to be reported to GTM. WooCommerce has a filter that can tell what product show be treated as hidden product, in addition to that, you can use this filter to hide a product only from GTM reporting.

function my_cart_item_filter( bool $item_included, array $cart_item): bool {
// do something to evaluate return value
return false; // return false to hide the product from GTM reporting in the cart
}

See WooCommerce source code to learn more about the $cart_item attribute.

gtm4wp_eec_order_item filter

Sometimes there can be items in the order that does not need to be reported to GTM. You can use this filter to hide a product only from GTM reporting.

function my_purchase_item_filter( bool $item_included, array $order_item): bool {
// do something to evaluate return value
return false; // return false to hide the product from GTM reporting
}

See WooCommerce documentation to learn more about the $order_item attribute.

© 2025 Google Tag Manager for WordPress - Privacy policy

  • WordPress.org plugin page
  • Plugin support
  • Plugin reviews
Cookies are not evil
This website may use cookies to remember your log-in details, to optimize site functionality for your use, and deliver marketing based on your interests.

This does not include collecting any personal data of you. In any case we need to process any of your personal data, we will ask for your specific consent later for sure.

Accepting this message only means that we will be able to analyze site content performance using larger buckets of users and to deliver tailored messages to those user buckets. We will be not able to analyze your activity alone especially not in a way where your name or any personal data of you is attached to this analysis.

Accept Settings
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-*1 yearUsed by the cookie consent manager of this website to remember your choice of cookie usage.
gtm4wp_last_weatherstatusend of user sessionUsed by a plugin of this WordPress website to dump debug related non-personal data about the weather data identified during your site visit.
Advertising
Tracking codes that could be used to tailor our messages based on your interests. This does not mean that we will be able to track your activities alone especially not in a way that would allow us to connect your name or other personal data to your website activities.
Statistical
Tracking of website usage without using any feature that would involve personal data processing either by us or by our tracking vendor. This includes IP address anonymization and disabling features related to interest based advertising.
Save & Accept
  • Home
  • Features
    • Basic features of GTM4WP

      You can use many post or page attribute in you Google Tag Manager setup even for custom post types and taxonomies.

      Basic data

      Page / post attributes

      Site search

      User data

      Multisite data

      3rd party data

      Browser attributes

      Device attributes

      Operating system attributes

      Weather conditions

  • Setup
    • Setup advanced GTM4WP features

      Integrate your measurement with WooCommerce, setup Google Analytics 4, Google Ads remarketing or conversion tracking maximizing capabilities.

      WooCommerce related

      Google Analytics 4

      Google Ads – Dynamic Remarketing

      Google Ads – Conversion tracking

      Google Ads – Enhanced Conversions

      Other setup articles

      Exclude WordPress admins

      Embedded media players

  • Developers
    • Information for developers and WP managers

      There are several tools in GTM4WP to support your special needs and to adjust plugin behavior to your website or WooCommerce shop.

      Do you want to contribute with a bugfix or a new feature? Visit the GTM4WP’s GitHub page!

      For Developers

      Actions and filters

      WP themes + Proper ecommerce tracking

      For WP managers

      Setup GTM Environments

      Hard code GTM parameters

  • Blog
  • GDPR
Search