Problem/Motivation
If you have checkout panes that use ajax on the first page of checkout, the begin_checkout event will fire every time a page is updated.
Steps to reproduce
Set up a site with Commerce. Add a pane to the first page of checkout that has ajax. The example that we found this on is with Commerce Shipping module. On the shipping pane, there's a button to calculate shipping rates after adding the shipping address. That button reloads the form with ajax. (and currently fires begin_checkout when it refreshes)
Proposed resolution
Only fire begin_checkout when the first page of checkout is being loaded without ajax.
Remaining tasks
Patch coming.
User interface changes
None.
API changes
None.
Data model changes
None.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | google_tag-begin_checkout-ajax-3410319-2.patch | 617 bytes | zengenuity |
Issue fork google_tag-3410319
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
zengenuity commentedPatch attached.
Comment #3
berdirJust found this as well. Patch partially helps, but a page refresh still counts twice, not sure if that's expected or not. There's also #3403313: Drupal Commerce purchase event gets sent multiple times if someone refreshes the checkout success page for a similar problem on purchase/checkout complete.
Wondering if google_tag should maybe have a built-in feature for unique events where it stores some kind of identifier in session/tempstore.
Comment #4
zengenuity commentedThe GTM expert who flagged this issue for our site suggested it would be expected for begin_checkout to fire every time the first page of checkout is displayed, such as if someone navigates away from it and then comes back. If that's true, I think it would be tough to prevent it on page refresh, since that's going to be very similar. I guess we could look at the referrer, perhaps.
For the purchase event, the situation would be different. That one should only ever fire once per order. It should probably happen on the order placed or order paid event rather than looking at the checkout step.
Comment #5
japerryI agree about the idea of a tempstore before firing, if thats even possible.. But there is likely some work needed to get this issue totally fixed per Berdir's comment.
Comment #6
kaszarobertFor commerce event tracking we've been using https://www.drupal.org/project/commerce_google_tag_manager (its latest release is hidden because it is Drupal 8, 9 only, plus the events are done for Universal analytics, not GA4) and there we never had an issue with events firing multiple times. I think there they also use tempstore. Maybe we could apply their solution here as well.
Comment #7
berdir@japerry: I'm not entirely sure that the purchase and this event are exactly the same and need the same fix per #4. Purchase definitely should not fire more than once ever for the same order, but I can see use cases where this might be desired for this event, like when you abort and re-enter checkout.
But if you have a payment and then return on a failed payment to the first step then that's also a problem and likely a bigger one than not firing the event again.
commerce_google_tag_manager did not have a persistent key value store, it just used a request subscriber and only acted on GET requests, which is not unlike the current patch here.
Comment #8
berdirFWIW, that said, we are using this patch now in production and did not yet get any complaints about it, so it's definitely an improvement over HEAD and one option would be to commit this and reopen #3403313: Drupal Commerce purchase event gets sent multiple times if someone refreshes the checkout success page to implement a persistent deduplication using tempstore or something.
Comment #9
loze commentedI maintain meta_pixel, which does essentially the same thing this module does for Meta's tracking code, with an event plugin system modeled after google_tag's. It guards its checkout funnel events like this at the top of its commerce_checkout_flow form alter:
The reasoning: a duplicate only matters if it actually reaches the browser, and that happens two ways, a page render or an ajax response. The compound condition blocks exactly the harmful case (ajax rebuilds, whose responses deliver collected events as commands), while plain POST submits are left alone because their events are discarded with the redirect anyway. The getErrors() check covers the one POST case that does render, a validation failure rebuilding the form.
This also stays consistent with berdir's earlier point that begin_checkout can legitimately re-fire when someone abandons and re-enters checkout, since GET renders are untouched. It is a request-type guard rather than deduplication, which seems like the right tool for the funnel events (the once-per-order dedup question for purchase is being handled in #3437889: Send the purchase event when an order is placed, not paid).
Implementation for reference:
https://git.drupalcode.org/project/meta_pixel/-/blob/1.0.x/modules/meta_...
Comment #11
japerryConfirmed the bug on 2.0.x. Patch #2 covers Ajax rebuilds, but a POST that fails validation re-renders the first step and sends begin_checkout again too.
MR !158 uses the request-method check from #9 instead: begin_checkout is only added on GET/HEAD, since every duplicate comes from a POST. Coming back to checkout or refreshing still fires it, which is intended (#4, #7). Once-per-order purchase is handled in #3437889: Send the purchase event when an order is placed, not paid.
It includes CommerceCheckoutEventsTest::testBeginCheckoutAjaxRebuild, which fails without the fix. A test with a real Commerce Shipping "Recalculate shipping" would be welcome.