-
Notifications
You must be signed in to change notification settings - Fork 70
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
Update tracking for classic pages #328
Update tracking for classic pages #328
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea I get from the brief description of the approach in the P2.
However, I'm not sure I fully understand specifics and implementation.
I tried to simplify the implementation logic a bit, maintaining more-or-less the same behavior.
By proposal is at https://github.com/woocommerce/woocommerce-google-analytics-integration/compare/update/tracking-for-classic-pages...update/tracking-for-classic-pages-tracker-class?expand=1
Please, let me know if that makes sense to you and still goes in the direction you envision.
I think there is still room for simplification in behavior and logic in both branches :)
I'd appreciate a F2F call, to discuss the details.
When I try to render the cart page, it throws an error:
Did we drop PHP 7.x support? |
Apologies @tomalec, I've pushed an updated so you should no longer get that error 9f60881. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for all the changes and clarifications.
I left some comments. Many are cosmetic ones. The most concerning to me are:
select_content
logic Update tracking for classic pages #328 (comment)remove_from_cart
handlers Update tracking for classic pages #328 (comment)
Also, while testing, I do not get remove_from_cart
event triggered when I remove a variable item from the cart.
Screencast from 19.02.2024 19:21:07.webm
The trackerEventHandler
is called, but fails with
index.js:15 Uncaught TypeError: Cannot read properties of undefined (reading 'variation')
at getProductFieldObject (index.js:15:15)
at remove_from_cart (data-formatting.js:62:33)
at trackerEventHandler (index.js:72:5)
at HTMLFormElement.<anonymous> (classic.js:68:47)
The changes we discussed earlier have been made and the ID mismatch has been resolved so this is ready for another round @tomalec. Thanks! |
// Return early if the user has clicked on anything other | ||
// than a product link or an Add to cart button. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅 Sorry, but as a non-native English speaker I read it as "the user has clicked on anything other than (a product link or an Add to cart button)" and I think you mean "the user has clicked on (anything other than a product link) or an Add to cart button", maybe something like this would be safer:
// Return early if the user has clicked on anything other | |
// than a product link or an Add to cart button. | |
// Return early if the user has clicked on | |
// an "Add to cart" button or anything other than a product link |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for highlighting that! Updated in 94cc036
const targetLink = event.target.closest( | ||
'.woocommerce-loop-product__link' | ||
); | ||
|
||
const isButton = event.target.classList.contains( 'button' ); | ||
|
||
const isAddToCartButton = | ||
event.target.classList.contains( 'add_to_cart_button' ) && | ||
! event.target.classList.contains( | ||
'product_type_variable' | ||
); | ||
|
||
if ( ! targetLink && ( ! isButton || isAddToCartButton ) ) { | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💅
I got a gut feeling that this selector may be hard to keep in sync to match with WC core/theme changes.
Should we fire the add_to_cart
event for non-ajax buttons as well?
From reading WC Core code, I think we can disable ajax_add_to_cart
globally or for a product, then end up with a
<a class="button add_to_cart_button">Add to cart</a>
.
If we want to fire for those, then the !isButton
condition should be removed. Otherwise, we could use .ajax_add_to_cart
to precisely select only those add-to-cart-like buttons that actually add to cart, guarding against .product_type_variable
to any other.
Are you sure we will not have some other .button
in the product card that does not actually select the product, but does some other thing? Like a link to a promotion, group, brand site, etc?
Maybe we can identify "Add to cart"-likeness by having data-product_id
attribute :)
So we would do
const targetLink = event.target.closest( | |
'.woocommerce-loop-product__link' | |
); | |
const isButton = event.target.classList.contains( 'button' ); | |
const isAddToCartButton = | |
event.target.classList.contains( 'add_to_cart_button' ) && | |
! event.target.classList.contains( | |
'product_type_variable' | |
); | |
if ( ! targetLink && ( ! isButton || isAddToCartButton ) ) { | |
return; | |
} | |
const isProductLink = | |
// Regular product link. | |
event.target.closest( '.woocommerce-loop-product__link' ) || | |
// Add-to-cart-like button. | |
event.target.hasAttribute( 'data-product_id' ); | |
const isAddToCartButton = | |
event.target.classList.contains( 'add_to_cart_button' ) && | |
! event.target.classList.contains( | |
'product_type_variable' | |
); | |
if ( ! isProductLink || isAddToCartButton ) { | |
return; | |
} |
Also, all this confusion looks like an encouragement to champion a PR to WC Core to add the product_id datar attribute to the product card ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When clicking on non-ajax add to cart button the event will be sent when the page loads and the confirmation message is displayed so I don't think we need additional targeting there.
I've added a check like you suggested so that the event will only send if the button includes the data-product_id
attribute to reduce the chance of issues with other extensions in 94cc036.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the changes and explanations.
Left a few tweak ideas
LGTM.
Co-authored-by: Tomek Wytrębowicz <[email protected]>
Changes proposed in this Pull Request:
This is a major restructuring centered around a new Tracker utility which manages all Google Analytics events triggered by either WooCommerce Blocks or classic pages. All tracked events must have an accompanying data formatter in order for the Tracker to send the event.
Several files and tests have been removed which are no longer of use
Screenshots:
Test setup
Debug a test website via Tag Assistant that has both Block powered and classic pages for the Cart, Checkout, and Product Listings (Using the
All Products
Block). Ensure allEvent Tracking
options are enabled inWooCommerce > Settings > Integrations > Google Analytics
.Detailed test instructions:
Work through all test instructions using both types of pages while not logged into the site as an admin.
view_item_list
event is sent containing all the products on the pageselect_content
event is sent for that productview_item
event is sent for the product and aview_item_list
event is sent if any related products or other lists are displayedadd_to_cart
event is sentremove_from_cart
event it sentbegin_checkout
event is sentpurchase
event is sentAdditional
WooCommerce > Settings > Integrations > Google Analytics
Product Identification
settingsGoogle Analytics for WooCommerce
(See #353).Changelog entry