Skip to content

GA4 DataLayer / GTM

The GA4 DataLayer / GTM module in Polski for WooCommerce pushes GA4 ecommerce events (view_item_list, view_item, add_to_cart, remove_from_cart, begin_checkout, purchase) to window.dataLayer and can load your Google Tag Manager container or the gtag.js tag. It is off by default and needs a GTM container ID, a GA4 measurement ID, or both.

Go to Polski > Modules and enable GA4 DataLayer / GTM (module ID: datalayer).

The module settings are stored in the polski_datalayer option. Go to Polski > Modules > GA4 DataLayer / GTM.

Setting Format Default Description
gtm_container_id GTM-XXXXXXX empty Google Tag Manager container identifier
ga4_measurement_id G-XXXXXXXXXX empty GA4 measurement identifier (for gtag.js mode)
use_sku_as_id boolean off Use the SKU instead of the product ID as item_id

Google Tag Manager: enter gtm_container_id. The module injects the GTM container snippet (a script in <head> and a noscript in <body>). Ecommerce events go to window.dataLayer, where GTM triggers can pick them up.

gtag.js: enter ga4_measurement_id. The module loads https://www.googletagmanager.com/gtag/js with the async attribute and runs gtag("config", ...) for your ID.

You can fill in both. The ecommerce events are always pushed to window.dataLayer as objects in the format shown below; the module does not call gtag('event', ...).

The module sends the following events in the GA4 ecommerce format:

Event When Where it comes from
view_item_list A product is rendered in a WooCommerce product loop (shop, category, search) woocommerce_after_shop_loop_item, one push per product
view_item A single product page woocommerce_after_single_product
add_to_cart AJAX add to cart from a product loop, or a normal add to cart that reloads the page jQuery added_to_cart event, or a queue in the WooCommerce session printed on the next page
remove_from_cart A product is removed in the classic cart, or its quantity drops in the block cart Click on the remove link, or the wc/store/cart data store
begin_checkout The checkout page loads with a non-empty cart wp_footer on the checkout page (not on the order-received or pay page)
purchase The thank-you page woocommerce_thankyou, once per order

Every item in ecommerce.items is built the same way:

Field Value
item_id Product ID, or the SKU when use_sku_as_id is on and the product has one
item_name Product name
price Product price, rounded to 2 decimals
item_category, item_category2, item_category3 The first three product categories; a variation uses its parent's categories
item_brand The manufacturer name from the GPSR fields, when set
item_variant For a variation: its attribute values joined with /
quantity In cart, checkout and purchase events
index, item_list_name In view_item_list only: loop position, and Search Results or Product List
dataLayer.push({
event: 'view_item_list',
ecommerce: {
items: [
{
item_id: '101',
item_name: 'Koszulka polo',
price: 89.99,
item_category: 'Koszulki',
index: 1,
item_list_name: 'Product List'
}
]
}
});
dataLayer.push({
event: 'view_item',
ecommerce: {
currency: 'PLN',
value: 89.99,
items: [
{
item_id: '101',
item_name: 'Koszulka polo',
price: 89.99,
item_category: 'Koszulki'
}
]
}
});

The AJAX path covers products that were listed on the same page; value is price times quantity.

dataLayer.push({
event: 'add_to_cart',
ecommerce: {
currency: 'PLN',
value: 89.99,
items: [
{
item_id: '101',
item_name: 'Koszulka polo',
price: 89.99,
item_category: 'Koszulki',
quantity: 1
}
]
}
});
dataLayer.push({
event: 'remove_from_cart',
ecommerce: {
currency: 'PLN',
value: 89.99,
items: [
{
item_id: '101',
item_name: 'Koszulka polo',
price: 89.99,
quantity: 1
}
]
}
});

value is the sum of product price times quantity, before coupons and shipping.

dataLayer.push({
event: 'begin_checkout',
ecommerce: {
currency: 'PLN',
value: 179.98,
items: [/* products from the cart */]
}
});

Sent on the thank-you page. After the push the module saves _polski_datalayer_tracked on the order, so reloading the page does not send it again. Item price here is the line total after discounts divided by quantity.

dataLayer.push({
event: 'purchase',
ecommerce: {
transaction_id: '12345',
affiliation: 'My Shop',
value: 179.98,
tax: 33.55,
shipping: 9.99,
currency: 'PLN',
coupon: 'SUMMER10',
items: [/* products from the order */]
}
});

transaction_id is the order number as WooCommerce displays it, affiliation is the site name and coupon lists the coupon codes separated by commas.

After entering gtm_container_id, the module injects Google's standard GTM container code:

In the <head> section (hook wp_head, priority 2, right after the window.dataLayer initialisation at priority 1). The loader inserts gtm.js with async.

After the opening <body> (hook wp_body_open):

<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
  • Async: GTM and gtag.js load with async, so they do not block page rendering
  • Inline dataLayer: ecommerce data is printed as inline JSON, without additional HTTP requests
  • Event delegation: remove_from_cart on the classic cart uses one click listener on the document
  • No layout shift: the GTM noscript iframe has zero dimensions

The module has no filters or actions of its own. To change the payload, add a GTM variable or transformation, or push your own events with the Custom Triggers module.

Use Tag Assistant to preview events in real time. Open Tag Assistant, connect to the store domain and go through the entire purchase process.

Check the dataLayer contents in the console:

console.table(window.dataLayer);

In Google Analytics 4 go to Admin > DebugView to see real-time events from a device with debug mode enabled.

The purchase event does not fire: check whether the thank-you page is cached, and whether the order already carries _polski_datalayer_tracked from an earlier visit.

No add_to_cart events: the AJAX path needs jQuery and WooCommerce's added_to_cart event, and only covers products rendered in a loop on the same page. Check the browser console for JavaScript errors.

The GTM snippet does not load: check whether the theme calls wp_head() and wp_body_open().

Does the module send events to GA4 without Google Tag Manager?

Section titled "Does the module send events to GA4 without Google Tag Manager?"

It loads gtag.js and configures your measurement ID, which records page views. The ecommerce events are pushed to window.dataLayer in GTM format, so GA4 ecommerce reports need GTM tags that forward them.

Can the purchase event be sent twice?

Section titled "Can the purchase event be sent twice?"

Not from this module for the same order: it marks the order with _polski_datalayer_tracked after the first push and skips it afterwards.

What is sent as the product brand?

Section titled "What is sent as the product brand?"

The manufacturer name from the product's GPSR fields, as item_brand. Products without it have no item_brand.

Reporting issues: github.com/wppoland/polski/issues

This page is for informational purposes only and does not constitute legal advice. Polski for WooCommerce is open source software (GPLv2) provided without warranty.