Kowal Data Layer for Magento 2
- SKU
- M2-DATA-LAYER
Description / Kowal Data Layer for Magento 2
MAGENTO 2 · ECOMMERCE · GTM · ATTRIBUTION
Connect ecommerce events with the order source.
Kowal Data Layer
Prepare Magento purchase data for GA4 and ad tag configurations. Preserve first touch and last non-direct touch on the order, and verify tag implementation in GTM preview.
Ecommerce events · GTM exports · Attribution in the admin panel
HOW IT WORKS IN PRACTICE
01 · Choose data and identifiers
Set item_id, brand, category, and Store View.
02 · Configure tags and consent
Import the GTM export and connect it with your CMP.
03 · Check events and orders
Verify values, currencies, and saved attribution.
What does your store gain?
Explore the module features, use cases, and settings.
Magento 2 Data Layer for GA4, GTM, and sales attribution
Kowal Data Layer creates a consistent ecommerce data layer directly from Magento 2. It supports the full shopping path, integrations through Google Tag Manager, and saving the source of a specific order.
This solution is for stores that want to connect GA4 analytics, advertising campaigns, and Magento data without manually adding scripts to more theme and checkout elements.
GA4 ecommerce
Events from product lists and PDP through cart, wishlist, and checkout all the way to purchase.
Ready-to-use GTM exports
Packages for GA4, Google Ads, Meta, TikTok, Pinterest, Microsoft Ads, and OpenAI Ads.
Order attribution
First touch and last non-direct touch with information about the source, medium, campaign, landing page, and referrer.
Analytics must not block sales
The module was designed using a fail-safe model. Data layer or attribution errors are isolated and logged, but they should not stop the cart, checkout, order, or redirect to payment.
Supported ecommerce events
user_data, view_item_list, select_item, view_item, add_to_cart, remove_from_cart, view_cart, add_to_wishlist, begin_checkout, add_shipping_info, add_payment_info, purchase, and purchase_test.
Multistore and product feeds
Configuration per Store View lets you align identifiers, currency, names, category, and brand. item_id can match the identifiers used in Google Merchant Center, Meta Catalog, and other feeds.
Consent Mode
The module can work with Kowal Cookie Consent. Data Layer handles ecommerce data, while the CMP handles consent and launching the appropriate tags.
FAQ
Does the module replace Google Tag Manager?
No. It prepares ecommerce data and ready-made configuration exports that can be used in GTM.
Does it work with Varnish and FPC?
Yes, the solution takes into account stores that use cache and dynamic checkout elements.
Does it save the order source in Magento?
Yes. First touch and last non-direct touch attribution is copied from the cart to the order.
Does it support multistore setups?
Yes. Identifier and data configuration can work at the Store View level.
Can an analytics error block checkout?
The architecture was designed so analytics errors are isolated from the sales process.
GTM export is the start of implementation
Kowal → Data Layer → GTM Exports lets you download JSON for the selected platform after configuring identifiers. Import the package into the GTM workspace using Merge, check the tags and Preview, and only then publish the configuration.
Updating the Magento module does not update previously imported GTM tags. Download the appropriate export again when its logic changes, and check for duplicate tags and conversions.
Attribution in the admin panel and measurement limits
Data Layer → Customer Attribution shows orders with saved attribution, including guest purchases. The grid has filters, sorting, and saved views; access requires a separate role permission.
UTM sources, click ID types, organic, and referral are recognized without saving full queries or raw advertising click IDs. The data reflects recognized visits, not a complete picture of all customer touchpoints across different devices.
Purchase and diagnostic purchase_test
purchase_test in the standard REST checkout means the order was created, not that the purchase was paid. Exported conversion tags ignore it. The event requires separate activation in the configuration.
Redirects to custom payment gateways may require an adapter. You can check errors in the var/log/kowal_datalayer.log logs; the design isolates them from the sales process.
Consent, private data, and cache
The CMP manages consents and updates their status. When Kowal Cookie Consent loads GTM, disable the second GTM loader in Data Layer. The tag export itself does not replace consent configuration.
User identity is retrieved from a private endpoint to avoid mixing customer data in FPC. For an external GTM loader, take into account user_data readiness, the correct consents, and the result in Preview.
Hyvä and identifier consistency
The package includes the optional Kowal_DataLayerHyva, enabled together with Enable Hyva Integration for the appropriate Store View. The adapter supports private-content, dynamic lists, and FormData.
Standard REST checkout is covered. Hyvä Checkout/Magewire and GraphQL require separate integration. The item_id in the export must match the identifiers of the appropriate feed or ad catalog.
Installation via Composer
Package kowal/module-datalayer, module Kowal_DataLayer. After configuring access to the Kowal repository, install the package, enable the module, update Magento, and clear the cache. In production, include compilation and static content deployment according to the store process.
From configuration to results
1. Choose data and identifiers
Set item_id, brand, category, and Store View.
2. Configure tags and consent
Import the GTM export and connect it with your CMP.
3. Check events and orders
Verify values, currencies, and saved attribution.
From campaign visit to order
A customer arrives from a campaign, views a product, and completes the purchase. The module saves recognized sources in the quote and transfers them to the order.
In the admin panel, you compare first touch and last non-direct touch. In GTM Preview, you check the purchase event with value, currency, and items.
Organize ecommerce data and sales attribution
Want to adapt the module to your store? Ask about Kowal Data Layer implementation and discuss the configuration or needed extensions.
More Information
| Hyvä theme | Hyvä compatible |
|---|---|
| Theme compatibility | Hyvä, Luma / Blank, KOWAL |
Module Installation Instructions
Installation and configuration guide
Requirements
- Magento Open Source / Adobe Commerce 2.4.x.
- PHP compatible with the Magento version in use.
- Composer access to the private module repository.
- The
kowal/basemodule. - Permission to run
setup:upgrade, because the module adds attribution fields to thequoteandsales_ordertables. - A Google Tag Manager web container if the store is to use ready-made integration exports.
- Optional:
Kowal_CookieConsent, if the store uses it for GTM and Consent Mode.
Installation with Composer
You will receive Composer repository access data, meaning the customer email address and license token, by email after purchase. They are also available in the customer panel after logging in at kowal.store. Replace TWOJ_EMAIL_KLIENTA with the email address of your account and TWOJ_TOKEN with the received token. Run the commands in the Magento root directory.
composer config repositories.kowal composer https://repo.kowal.store
composer config http-basic.repo.kowal.store 'TWOJ_EMAIL_KLIENTA' 'TWOJ_TOKEN'
composer require kowal/module-datalayer
php bin/magento module:enable Kowal_DataLayer
php bin/magento setup:upgrade
php bin/magento cache:flushThe setup:upgrade command creates the columns:
quote.kowal_attribution_first;quote.kowal_attribution_last;sales_order.kowal_attribution_first;sales_order.kowal_attribution_last.
The columns contain serialized first-touch and last-touch data. They should not be created manually.
Updating an existing installation
Before updating production, create a database backup according to the project procedure. Then update the package and Magento schema:
composer update kowal/module-datalayer --with-dependencies
php bin/magento module:enable Kowal_DataLayer
php bin/magento setup:upgrade
php bin/magento cache:flushIn production mode, run DI compilation and static asset deployment again. This is required, among other things, for the new attribution endpoint, quote/order observers, and changes in the frontend script.
In the production environment, if the project requires it, run the standard Magento deployment:
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy
php bin/magento cache:flushAfter updating ACL permissions, the administrator may need to log in to the panel again to see the Data Layer > GTM Exports item.
Basic configuration
Admin panel:
Stores > Configuration > Kowal > Kowal Data Layer
Menu:
kowal.store > Modules > Data Layer > Settings
General
Enable Module- enable the module for the selected configuration scope.Debug Mode- enable only during testing and implementation verification.Render Mode- leaveServer and JavaScriptif there is no reason to limit the operating mode.Missing Value Strategy- choose whether missing values should be omitted or sent asundefined.Clear Ecommerce Before Push- recommendedYes.Fail-safe Mode- recommendedYes; sales-critical processes remain protected even after disabling this option.Log Level-Errors onlyis recommended in production.Log Payloads- enable only temporarily.
Product Mapping
Product Identifier Attribute- choose the attribute used asitem_id. Default:sku.Brand Attribute- choose the manufacturer/brand attribute. Default:manufacturer.Category Strategy- choose how to builditem_category.Category Attribute- set only when the category strategy uses a product attribute.Configurable Product Strategy- decide whether to use child/simple or parent/configurable data.Include Out Of Stock Products On Lists- applies to product list events.
Events
Enable the events required by the implementation:
user_dataview_item_listselect_itemview_itemadd_to_cartremove_from_cartview_cartadd_to_wishlistbegin_checkoutadd_shipping_infoadd_payment_infopurchasepurchase_test, if the store uses an intermediate page before payment.
Privacy
Send User ID- sends the Magento customer ID for logged-in customers.Send Hashed Email- sends only the SHA-256 hash of the email. The raw email is not sent todataLayer.
Attribution
Capture First and Last Touch Attribution saves the source of the customer visit in the quote and order. Recommended setting: Yes.
The setting works per store view. After it is disabled, the module no longer records new touchpoints. Data previously saved in existing orders remains in the database.
The module recognizes:
utm_source,utm_medium,utm_campaign,utm_id,utm_term,utm_content, andutm_source_platformparameters;- Google Ads, Microsoft Ads, Meta, TikTok, Pinterest, X, and LinkedIn click ID types;
- visits from organic search results;
- external referrals;
- direct visits.
first_touch and last_touch, meaning the last non-direct source, are saved. Regular navigation between store pages does not overwrite the source. Referrals from payment URLs, callbacks, returns, and success pages are ignored so that the payment operator is not considered the sales source.
Capture works in two ways:
- on the Magento side for standard, uncached visits;
- through a lightweight browser request for pages served from FPC or Varnish.
Data is maintained in the checkout session, saved with the quote, and copied to the order before the order is placed. The mechanism covers the classic frontend, AJAX/REST checkout, and orders created from a quote that already contains attribution.
Raw gclid, fbclid, and other click identifier values are not saved. The module stores only the parameter type, for example gclid, and the resulting source classification. Query strings are removed from the saved landing page and referrer.
Attribution can be checked:
- in the order view in the
Customer Attributionsection; - in
dataLayerin theattributionproperty of thepurchaseevent; - directly in the
kowal_attribution_firstandkowal_attribution_lastcolumns of thequoteandsales_ordertables.
Source capture should be included in the store privacy documentation according to the implementation policy. The module does not create its own long-term user identifier and does not store click ID values, but it does store campaign metadata and the order source.
Integrations
Integrate With Kowal Cookie Consent- enable if the store usesKowal_CookieConsent.Render GTM From DataLayer Module- leaveNoif GTM is already rendered by Cookie Consent or another module.Google Tag Manager ID- set only when rendering GTM from this module is enabled.purchase_test Redirect Rules- add only the payment methods that should use the intermediatepurchase_testpage.
purchase_test rule modes:
Static URL- use only for payments with a fixed operator URL.URL Pattern- use when a URL pattern with placeholders{order_id},{order_increment_id},{store_id}, or{quote_id}is sufficient.Adapter Required- use for gateways that generate a dynamic transaction URL. Before launching in production, a dedicated adapter must be added for the specific payment module.
GTM Export Identifiers
In this section, enter the service identifiers that should be embedded in the generated JSON files:
| Field in Magento | Value |
|---|---|
GA4 Measurement ID | identifier in the G-XXXXXXXXXX format |
Google Ads Conversion ID | identifier in the AW-XXXXXXXXX format |
Google Ads Purchase Conversion Label | purchase conversion action label |
Meta Pixel ID | numeric Pixel ID |
TikTok Pixel ID | alphanumeric Pixel ID |
Pinterest Tag ID | numeric Tag ID |
Microsoft Ads UET Tag ID | numeric UET Tag ID |
Fields may be empty if the store does not use a given platform. Saving the configuration checks the format of each completed value. The export button checks only the set required by the selected platform, so for example a GA4 export does not require Meta or TikTok configuration.
After completing the fields, click Save Config, then the appropriate Download ... JSON button in the Generate GTM Exports field. The downloaded file already contains the final identifiers. You do not need to edit constant variables after importing to GTM.
Multistore configuration
Configure the module at the store view level if the stores differ by:
- language;
- currency;
- advertising feed;
- product identifier attribute;
- brand attribute;
- category structure;
- payment methods;
- attribution capture rules;
- service identifiers used in imported GTM packages.
Implementation verification
- Open Google Tag Manager Preview.
- Check
dataLayerin the browser console or with the DataLayer Checker extension. - Test the product page and the
view_itemevent. - Open a category or search results and check
view_item_list. - Click a product on the list and check
select_item. - Add a product to the cart and check
add_to_cart. - Change the quantity or remove a product from the cart and check
remove_from_cart. - Add a product to the wishlist and check
add_to_wishlist. - Go to the cart and checkout, and check
view_cartandbegin_checkout. - Save the shipping and payment method, and check
add_shipping_infoandadd_payment_info. - Place a test order and check
purchase. - Refresh the success page and make sure
purchaseis not duplicated. - Check
var/log/kowal_datalayer.log. - In a new private window, open the store with the URL
?utm_source=test&utm_medium=cpc&utm_campaign=attribution_test. - Browse several pages, add a product to the cart, and place an order.
- In the order view, check the
Customer Attributionsection andattribution.first_touchandattribution.last_touchin thepurchaseevent. - Confirm that
first_touch.sourcehas the valuetest,mediumhas the valuecpc, andcampaignhas the valueattribution_test. - In the same window and session, return to the store from another tagged source and confirm that
first_touchremained unchanged andlast_touchwas updated. - If the store uses an external gateway, confirm after returning from payment that the operator domain did not replace the order source.
Google Tag Manager configuration export
In the Magento panel, go to Stores > Configuration > Kowal > Kowal Data Layer > GTM Export Identifiers. Separate packages are available for GA4, Google Ads, Meta, TikTok, Pinterest, and Microsoft Ads.
- Select the appropriate configuration scope:
Default Config, website, or store view. - Complete the identifiers for the platforms in use and save the configuration.
- Click the JSON download button for the selected platform in the
Generate GTM Exportsfield. - In GTM, create a new workspace and select
Admin > Import Container. - Select the downloaded file and use
Mergemode. - Review the list of added variables, rules, and tags, and confirm that constant variables contain the correct identifiers.
- Check consent requirements and launch GTM Preview.
- Publish the container only after confirming the events in the platform diagnostic tool.
Element names are prefixed with Kowal - platform name, so the packages can be combined in one container. The purchase_test event is not exported as a conversion; sales are reported only through purchase.
If a required field is empty or has an invalid format, the module will not generate the export and will display a message indicating the value that needs correction. This prevents accidental import of a container with a placeholder. The alternative Kowal > Data Layer > GTM Exports page remains available, but it uses values from the Default Config scope; for multistore implementations, the buttons in the configuration are recommended.
The exports apply to the web container and browser-side sending. They do not replace server-side integrations such as Meta Conversions API or TikTok Events API.
Consents and GTM publishing
- Advertising tags require
ad_storage,ad_user_data, andad_personalization. - GA4 uses the built-in consent mechanisms of the Google tag.
- If GTM is rendered by
Kowal_CookieConsent, leaveRender GTM From DataLayer Moduleset toNo. - Do not publish the container without checking Consent Mode, GTM Preview, and the event diagnostics of the relevant platform.
- With a restrictive CSP, add the script domains and endpoints of the selected platforms according to their documentation.
purchase_test integration
If the payment method redirects the customer outside the store and knows the target operator URL, the payment integration can use:
Kowal\DataLayer\Model\PurchaseTestRedirect::prepare($paymentRedirectUrl)The method returns the module intermediate page URL. The intermediate page sends purchase_test and then redirects the customer to the payment operator. If JavaScript or DataLayer does not work, the redirect should still be performed.
For an integration where the URL depends on the order or transaction token, use:
Kowal\DataLayer\Model\PurchaseTestRedirect::prepareForOrder($order, $fallbackRedirectUrl)If the method is set to Adapter Required mode, the adapter should implement:
Kowal\DataLayer\Api\PurchaseTestRedirectAdapterInterfaceThe adapter must be registered in DI as an element of the adapters array for Kowal\DataLayer\Model\PurchaseTest\RedirectAdapterPool. The adapter is the right place for logic dependent on a specific payment operator.
Diagnostics
Customer Attribution section missing on the order
- Check whether
Capture First and Last Touch Attributionis enabled for the correct store view. - Confirm that
php bin/magento setup:upgradewas run and that the four attribution columns exist. - Clear the configuration and layout cache.
- Run a test in a new private window with a tagged UTM URL.
- Check
var/log/kowal_datalayer.logforattribution capture failedorattribution assignment failedmessages.
Attribution works without FPC but does not work with Varnish
- Check the POST request to
kowal_datalayer/attribution/capturein developer tools. - Confirm that the request has status
200and is made to the same store domain. - Check whether CSP, WAF, or reverse proxy rules are blocking the endpoint.
- After deployment, make sure the current
view/frontend/web/js/data-layer.jsfile has been published.
Purchase has an incorrect payment operator source
Check whether the order was created from a quote that contained attribution before the redirect. Routes containing payment, callback, return, or success should not overwrite last-touch with a referral. If a custom payment module uses a different sequence, verify when the order is created and the quote is saved.
GTM import reports conflicts
Use Merge mode and review the change details. Export elements have the Kowal - ... prefix; if modified elements with the same name already exist in the container, deliberately choose whether to overwrite or rename them. After renaming, check tag references to variables and triggers.