Send the purchase event when an order is placed, not paid

Created on 3 April 2024, 6 months ago
Updated 23 April 2024, 5 months ago

Problem/Motivation

This module is currently using the OrderEvents::ORDER_PAID event to collect a purchase event to send to GTM. However, there are many scenarios where an order might be placed but not paid. Per the Google Analytics documentation,

You should place the purchase event on the page of your website where someone makes a purchase. For example, you could add the event on the confirmation page that appears when someone makes a purchase.

Proposed resolution

This module should be updated to use the 'commerce_order.place.post_transition' event to record the purchase with GA. This event is always fired upon checkout completion even if payment is still pending.

The problem, of course, is that this will be a backward incompatible change. Perhaps the event needs to be configurable, with the default being to use the order place transition and the option to use the paid event remaining as a pre-selected option for existing users. If there is no relevant settings form already where such a setting could be placed, it could be pulled from the global settings array, with existing users instructed to set it in their settings.php if they wish to use the previous behavior.

(I'm marking this and another issue with a "Deprecate Commerce GTM" tag. These are issues that fill gaps for us to formally recommend closing that module and migrating sites to this one.)

πŸ“Œ Task
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States rszrama

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

Comments & Activities

  • Issue created by @rszrama
  • πŸ‡ΊπŸ‡ΈUnited States rszrama
  • Status changed to Needs review 6 months ago
  • πŸ‡ΈπŸ‡°Slovakia kaszarobert

    I attach a patch with a possible solution to use just the commerce_order.place.post_transition event. This would also fix another issue I encountered πŸ› Drupal Commerce purchase event gets sent multiple times if someone refreshes the checkout success page Active as it would not send a purchase event based on the checkout step multiple times anymore. But as you mentioned, this alters the module's behavior when to send purchase events to the way how Commerce Google Tag Manager β†’ worked.

    Then this way, I encountered a strange error when PurchaseEvent::getData is called:

    Error: Typed property Drupal\google_tag\Plugin\GoogleTag\Event\Commerce\PurchaseEvent::$transformer must not be accessed before initialization in Drupal\google_tag\Plugin\GoogleTag\Event\Commerce\PurchaseEvent->getData() (line 79 of modules/contrib/google_tag/src/Plugin/GoogleTag/Event/Commerce/PurchaseEvent.php).
    google_tag_page_attachments(Array) (Line: 311)

    I could not fix this with proper DI, so that's why I made those changes there.

  • πŸ‡©πŸ‡ͺGermany marcusx

    Just stumbled over this as well. In my case I was confused why the purchased event is tiggered in this form_alter you removed. Was looking into a similar fix.

    I wonder if we could use

    CheckoutEvents::COMPLETION
    

    as the subscribed event instead of `'commerce_order.place.post_transition'`?

  • First commit to issue fork.
Production build 0.71.5 2024