- Issue created by @newaytech
- πΊπΈUnited States rszrama
Drupal Commerce has a coupon reference field on orders that stores which coupons have been applied to the order. It's not an exact match to Google's data model, which appears to expect a single coupon, but we can grab the first one and add it to the purchase event template in:
https://git.drupalcode.org/project/google_tag/-/blob/2.0.x/src/Plugin/Go...
Would be nice if one of the options I've tagged into the "Deprecate Commerce GTM" list could be used to give merchants the opportunity to alter which coupon code gets used in the event of there being more than one. We could always sort them by their relative promotion sort orders so there's at least a consistent default with some UI based influence.
- First commit to issue fork.
- Merge request !80Issue #3442913: Tracking Coupon codes from Commerce Promotions β (Open) created by kaszarobert
- Status changed to Needs review
over 1 year ago 12:52pm 24 April 2024 - πΈπ°Slovakia kaszarobert
I created a MR with simply adding the coupon code to the dataLayer.
- π¬π§United Kingdom newaytech
Thanks kaszarobert - can I add your commit into my composer.json - or do we need a .patch file for that?
- πΈπ°Slovakia kaszarobert
@newaytech usually you add patch files to composer.json because the feature branch used for MR could change in the meantime if further changes will be required by reviewers. A patch file will stay the same. I uploaded a static patch file with the same modifications as in MR.
- π¬π§United Kingdom newaytech
Thanks kaszarobert - I've applied your patch via composer.json - works a treat - and we're now tracking promo codes in GA4 via GTM - fantastic!
- πΊπΈUnited States thalemn
@newaytech - I've applied the patch but do not know how to configure GTM to track the codes.
Can you share how you configured GTM/GA4?
Thanks.
- πΈπ°Slovakia kaszarobert
If you're using google_tag module's latest version, then create a new data layer variable in GTM called "Ecommerce Coupon" with value as
eventModel.coupon
and for the GA 4 purchase tag under Event parameters (where you add items, transaction_id, value, currency etc.) add a new row with "coupon" and "{{Ecommerce Coupon}}" - πΊπΈUnited States thalemn
Thanks @kaszarobert!
I used eventModel.coupon for the Ecommerce Coupon value.
For items and transaction_id, should I do the same like this...
eventModel.items
eventModel.transaction_idor should they be...
ecommerce.items
ecommerce.transaction_id - πΈπ°Slovakia kaszarobert
Open the Preview in GTM and check it. That's the most reliable way.
It really depends on how you send it. For example take a look at the add_to_cart event with the newest Google Tag module version does it this way in the dataLayer JS variable:
[ ... { "0": "event", "1": "add_to_cart", "2": { "currency": "EUR", "value": "10.00", "items": [ { "item_name": "Product1", "affiliation": "Shop", "price": "10.00", "quantity": 1, "item_id": "ProductSku" } ] } } ... ]
So there's no ecommerce key, just the items, so we use
eventModel.items
The older Commerce Google Tag module used to do it this way:
[ ... { "event": "add_to_cart", "ecommerce": { "items": [ { "item_name": "Product1", "item_id": "ProductSku", "price": "10.00", "item_variant": "ProductSku", "currency": "EUR", "quantity": 1 } ] }, "gtm.uniqueEventId": 6 } ... ]
There we used to have an ecommerce key and under it were the settings, so we had to use
ecommerce.items
. So please check it on GTM Preview if the variables from the dataLayer are set properly. And that goes for purchase and any other ecommerce events, too. But if you are using this module's 2.x.x version, then I think the first one will be the one that works. - πΊπΈUnited States thalemn
Thank you for the details and differences. I'm using the 2.x.x version (newest), so I'll review the first example you shared.
- π¬π§United Kingdom newaytech
I attach my Event params - passed to the GA4 purchase event - and also the debug of the dataLayer as seen on the receipt page (using the patch in #7) - in the screenshot attached.