Actions and filters in GTM4WP 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
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.