Tracking Coupon codes from Commerce Promotions

Created on 23 April 2024, over 1 year ago

Problem/Motivation

Our client has requested the use of Promo codes to assist with a marketing campaign. Great job Drupal Commerce - Promotion module installed, configured and good to go. We now need to understand the efficacy of the campaign - and see the promo codes manifest within our GA4 reports.

Proposed resolution

Implement a further field within the PurchaseEvent Commerce Event:

"coupon": 'SUMMER_SALE'

https://developers.google.com/analytics/devguides/collection/ga4/ecommer...

Use a similar logic as for the tax field - using the adjustment collector to arrive at the promotion label...

$order->collectAdjustments(['promotion']
✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom newaytech

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • 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.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΈπŸ‡°Slovakia kaszarobert

    I created a MR with simply adding the coupon code to the dataLayer.

  • Pipeline finished with Failed
    over 1 year ago
    Total: 244s
    #155374
  • πŸ‡¬πŸ‡§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_id

    or 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.

Production build 0.71.5 2024