• Resolved Simon Blackbourn

    (@lumpysimon)


    Hi

    We have added a button to subscription products on the product archive page that when clicked opens a modal with info about the subscription plans. This button is absolutely positioned in a div in the woocommerce-loop-product__link link and has an on.('click') event on it.

    On my local site clicking this button works as intended, in that it opens the modal and that’s all. On our staging site running GTM4WP the modal opens but then the product page is loaded. If I disable the “Track e-commerce” option in GTM4WP it works correctly again.

    I am guessing this is probably caused by the select_item event callback that is mentioned in the “Set maximum timeout for select_item event” setting?

    I have tried adding e.preventDefault() and e.stopPropagation() to the button event listener, and adding z-index to ensure the button is definitely above the link, but neither of these works.

    Can you please advise is there is any way to prevent the select_item event firing when this button is clicked?

    Many thanks
    Simon

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi @lumpysimon,

    Your guess is right, this is the select_item click handler in the WooCommerce part of the plugin. It listens for clicks on the document in the capture phase, so it runs before any handler you attach to the button itself. At that point it checks whether anything has already cancelled the click, and since your own listener has not run yet, it sees an ordinary product link click, cancels the click, sends select_item and then navigates to the product page when the callback returns or when the timeout expires. Calling preventDefault or stopPropagation in your button handler changes nothing, because all of that happens after the plugin has already made its decision.

    The setting you found is the way to solve this. Set “Set maximum timeout for select_item event” to 0 on the Integration tab, in the WooCommerce section. At 0 the plugin sends select_item without holding the click at all, so nothing navigates and your modal keeps the click. The setting only affects select_item and no other event, and it is safe to leave at 0 in production. The only thing you give up is the short grace period that a real product link click gets so that the event can register before the browser leaves the page, and in practice the event still reaches Google across the page change.

    If you would rather keep the timeout for the rest of your product links, the markup can solve it instead. The plugin only acts when the clicked element sits inside the product link, so moving your button out of the woocommerce-loop-product__link anchor and positioning it over the link rather than inside it also stops the handler from running for that click.

    Sites add all kinds of custom elements inside the product link, and the plugin cannot reliably tell such a click apart from an ordinary click on the product itself, so that is what the timeout setting is for. Please let me know whether it solves it for you.

    Thread Starter Simon Blackbourn

    (@lumpysimon)

    Thanks for the detailed reply Thomas, much appreciated.

    Unfortunately setting the timeout to zero doesn’t make any difference, so I’ll take a look at moving the button out of the link element.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi @lumpysimon,

    Thank you for testing it and for coming back with the result. You are right that it made no difference, and the reason is a bug in the plugin rather than anything in your setup, so I owe you a correction on what I wrote before.

    The timeout value is written into the page correctly, but the tracker looks it up under a name that never resolves, so it falls back to the built in 2000 ms wait on every product list click whatever the setting says. Setting it to 0 therefore changes nothing at all on the current version, and the advice I gave you was wrong. It is fixed for 2.0, although the fix is not in the 2.0 beta that is downloadable at the moment, so there is no build you can install today to get it.

    The markup route you already landed on is the one that works. The click handler only acts when the clicked element sits inside the product link, so moving your button out of the woocommerce-loop-product__link anchor and positioning it over the link rather than inside it stops the handler from running for that click, and your modal keeps it.

    Sorry for sending you down the wrong path first. Please let me know how it goes once the button is out of the link.

    Thread Starter Simon Blackbourn

    (@lumpysimon)

    Hi Thomas – sorry for the long delay, I’ve been off on holiday. Is 2.0 still set for a Sept 1st release date? If so, I’ll hold off until then.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi @lumpysimon,

    No need to apologize, I hope the holiday was a good one.

    Yes, 1 September 2026 is still the plan. I announced it with the caveat that it assumes nothing major turns up during the beta, and that caveat still stands.

    You do not have to wait for it though, and I owe you an update on what I told you last time. When I wrote that there was no build you could install to get this fix, that was true then and it is not true any more. The release candidate went out on 17 August and it does carry the fix, so the “Set maximum timeout for select_item event” setting takes effect there, and a value of 0 does what the description says. You can download it from the releases page of the plugin on GitHub:

    https://github.com/duracelltomi/gtm4wp/releases

    It is a release candidate rather than a final release, so I would put it on your staging site first and not straight onto the live one.

    If you would rather not run a pre-release at all, the markup route still works on the version you have now and it keeps working afterwards, so moving the button out of the woocommerce-loop-product__link anchor is not wasted effort either way.

    Please let me know how it goes.

    Thread Starter Simon Blackbourn

    (@lumpysimon)

    Thanks for the update. We’ve decided not to go for the pre-release version as we’re going to need to thoroughly test v2 when it’s released anyway, so we’re happy to wait until 1st Sept for that rather than having to test two releases. If all is good with v2, then we’ll deploy our code to our production site.

    Plugin Author Thomas Geiger

    (@duracelltomi)

    Hi @lumpysimon,

    A short update: 2.0.0 was released on 1 September as planned, so it is available on wordpress.org as a normal plugin update now. The “Set maximum timeout for select_item event” setting takes effect in this version, and a value of 0 does what the description says, so once you have tested v2 you should not need any workaround for your subscription button.

    Please let me know how your testing goes.

    Thread Starter Simon Blackbourn

    (@lumpysimon)

    Thanks @duracelltomi , setting that to zero in v2 is working perfectly.

Viewing 8 replies - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.