Skip to content
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 blocks #311

Merged

Conversation

martynmjones
Copy link
Contributor

@martynmjones martynmjones commented Oct 5, 2023

Changes proposed in this Pull Request:

Closes #268

Tracking for blocks only supports Universal Analytics as it currently stands. This PR updates the tracking for GA4 and removes references to UA documentation.

Screenshots:

Screenshot 2023-10-25 at 19 35 45

Detailed test instructions:

view_item_list

  1. Create a page using the All Products block and view page in incognito with GA Debug enabled (Alternatively use Tag Assistant to monitor events)
  2. Confirm that on the initial page load a view_item_list event is triggered with the correct parameters
  3. Click on the second page of the product listings and confirm that only one new view_item_list is triggered and that it matches the above data structure.

add_to_cart

  1. On the All Products page, click Add to cart for a product
  2. Confirm that an add_to_cart event is triggered with the correct parameters

remove_from_cart and change_cart_quantity

  • These events can be ignored with this PR. The experimental hooks in blocks don't work well for how GA4 expects the remove_from_cart event to work. Issue with tracking quantity changes in cart #312 has been opened so that this can be addressed in a follow-up PR.

begin_checkout

Note: The experimental__woocommerce_blocks-checkout-render-checkout-form hook which the begin_checkout event relies on doesn't currently work. Reported in woocommerce/woocommerce-blocks#11472.

  1. To test this event, go to the block-powered checkout page, apply a coupon, and run the following in console:
wp.hooks.doAction('experimental__woocommerce_blocks-checkout-render-checkout-form', { storeCart: wp.data.select('wc/store/cart').getCartData() } );
  1. Confirm that the begin_checkout event is triggered with the correct parameters and that item quantities and coupon name are correct in the event data

add_shipping_info

  1. Add more than one shipping option to the test site
  2. Select an option during checkout and click Place Order
  3. Confirm that the add_shipping_info event is triggered with the correct parameters

select_content

  1. Go to a page with the All Products block
  2. Click on a product
  3. Confirm that the select_content event is triggered with the correct parameters

search

Changelog entry

Update - Tracking for GA4 when using Blocks

@martynmjones martynmjones self-assigned this Oct 5, 2023
@martynmjones martynmjones changed the title Updatetracking code for blocks Update tracking for blocks Oct 5, 2023
@github-actions github-actions bot added the changelog: update Big changes to something that wasn't broken. label Oct 5, 2023
@martynmjones martynmjones requested a review from tomalec October 26, 2023 17:33
@martynmjones martynmjones marked this pull request as ready for review October 26, 2023 18:23
@martynmjones martynmjones mentioned this pull request Oct 30, 2023
9 tasks
@tomalec
Copy link
Member

tomalec commented Oct 31, 2023

The Google documentation states that the price parameter should be a number, but we're sending string
image

Copy link
Member

@tomalec tomalec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the code tested locally.
My only concern is the price data type.
Other than that, LGTM.

} );
if ( products.length > 0 ) {
trackEvent( 'view_item_list', {
item_list_id: 'engagement',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📜 💅
If I understand the docs correctly, this id is to identify the list from which we're reporting.
With blocks, we can have multiple lists on different pages. But currently, we send engagement regardless of which list we track. Maybe we could somehow forward the block instance id, or at least page ID?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be a nice addition! I've opened issue #324 so as to not get off track in this PR.

Comment on lines +88 to +91
value: formatPrice(
storeCart.totals.total_price,
storeCart.totals.currency_minor_unit
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation states it should be a number, we send a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Updated in 4a06f52

storeCart.totals.total_price,
storeCart.totals.currency_minor_unit
),
coupon: storeCart.coupons[ 0 ]?.code || '',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


I wonder if the empty "" in case of lack of coupon is the most accurate representation. Shouldn't we just don't send this property in case of lack of coupon? I'm not sure how the value being set (event to empty string) would be interpreted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I'm not sure what the behavior would be and from a quick search I couldn't find any thing in the GA documentation about it 🤔 I've updated how it works in 813b7ee so we're not including an empty string.

@martynmjones martynmjones merged commit 45f2b5a into remove-universal-analytics Nov 2, 2023
1 check passed
@martynmjones martynmjones deleted the update/268-tracking-code-for-blocks branch November 2, 2023 15:26
@jorgemd24 jorgemd24 mentioned this pull request Mar 5, 2024
19 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: update Big changes to something that wasn't broken.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants