Commerce GA4 Datalayer
This module automatically sends standard Google Analytics 4 (GA4) e-commerce events to your website's data layer, making them available for Google Tag Manager or other tag management solutions. It supports a wide range of e-commerce events and allows for flexible mapping of product data.
Commerce GA4 Datalayer pushes standard GA4 ecommerce events to window.dataLayer, ready to consume with Google Tag Manager, gtag.js, or any tag management solution. If you run Drupal Commerce and need GA4 Enhanced Ecommerce reporting without hand-writing dataLayer pushes in every template, this module builds and queues the correct event payloads for you.
Features
- Fires
view_item,add_to_cart,remove_from_cart,view_cart,begin_checkout,add_shipping_info,add_payment_info,purchase,add_to_wishlist,login, andsign_upevents, each toggleable independently. - Configurable item parameter mapping using the Token module, map
item_brand,item_variant,item_size, or any custom parameter name to product/variation fields, no fixed slots. - Hierarchical taxonomy support: walk up to 3 parent levels of a term reference (e.g. category tree) into
item_category,item_category2,item_category3. - Core Commerce fields (SKU, title, price, quantity, affiliation, promotions, coupons) are mapped automatically.
- Session-based event queue decouples event generation (add to cart, checkout, order placement) from page render, flushing to
window.dataLayerviadrupalSettingson next page load: correct timing with GTM container init. - Automatic promotion resolution on
add_to_cartvia Commerce's order refresh, giving accuratediscount,promotion_id,promotion_namewithout an extra save. - Duplicate event prevention for AJAX-heavy flows (variation switches, repeat cart views).
- Checkout step detection via route + Referer comparison, so AJAX refreshes don't refire
begin_checkout/add_shipping_info/add_payment_info. - Role-based exclusion: skip tracking entirely for selected roles (e.g. staff, admins).
- Public builder service and hook for other modules to add custom GA4 events (e.g.
select_item, custom search events) or alter item payloads.
Use this module if you're running Drupal Commerce and need GA4 Enhanced Ecommerce data without maintaining custom dataLayer JavaScript across every commerce template.
Post-Installation
After enabling, go to Commerce > Configuration > GA4 DataLayer (/admin/commerce/config/ga4-datalayer) to:
- Toggle which events are active
- Define item parameter → Token mappings (with a built-in token browser)
- Exclude specific roles from tracking
No content type or text format changes are needed. You still need a tag management solution (GTM, gtag.js) configured separately to actually read from window.dataLayer, this module only populates it.
Additional Requirements
- Drupal 10.3+ or 11
- Drupal Commerce 2.36+ (commerce_order, commerce_cart, commerce_product, commerce_checkout)
- Token module
- A tag management solution reading
window.dataLayer(Google Tag Manager, gtag.js, etc.) not provided by this module
Optional:
- Commerce Wishlist required for
add_to_wishlistevent tracking - Commerce Shipping required for
add_shipping_infoevent tracking
Recommended modules/libraries
- Token required for field mapping configuration
- Commerce Wishlist enables wishlist event tracking
- Commerce Shipping enables shipping step event tracking
Community Documentation
See the project README for full configuration examples, token mapping tables, and code samples for extending the module with custom event subscribers.