-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export logic of GTM events as functions #273
Comments
It is very important that I would like to avoid using jQuery. On your specific issue: e.preventDefault(); is only called when someone clicks on a product in a product list. I am not sure what you saw in the add to cart code. |
I'm also vanilla JS fan and was very happy when you removed jQuery. The problem is not in your code but in themes that use AJAX add to cart. If there wasn't e.preventDefault(); it would trigger a page refresh. But when there is such thing, all other codes, including GTM4WP are not triggered. |
I plan to change how add to cart is being tracked as it turned out that when there are other plugins used that could prevent the add to cart event for some reason (user did not select all the necessary options) then the event is still tracked when pressing the button. Unfortunately the added_to_cart event on the frontend is not triggered when the shop is not using the ajax cart functionality. Therefore I plat to switch to use a WooCommerce hook on the backend and trigger the JS event on the next page load or so.... still not confident... :-\ |
Hello,
I came across an issue with Add to cart on some themes, where this is handled by AJAX request and as such, it begins with
e.preventDefault();
which breaks all other mounted functions on Add to cart button.I made a fix by monitoring the response from AJAX and on success trigger the event, but for this I had to copy 80% of original gtm4wp code responsible for add_to_cart event. If the code could be exported to functions, it would be easier for developers to handle such compatibility issues with just little code.
This is just an idea .. here's the code I ended up adding for this specific custom coded theme:
https://gist.github.com/morvy/921a5ace2a29221f41d17c107da6780d
Everything after line 18 is just copy/paste. If there was a function with 2 arguments - event_target_element and product_form it could've been much shorter :)
The text was updated successfully, but these errors were encountered: