-
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 blocks #311
Changes from 17 commits
eea89a6
ed8af32
6641bf6
a663b12
80dde61
92bafdd
bd25a7e
b6ec761
bd73c66
1dabbe1
0f614c9
9aef6f5
65fcc7a
662a963
848c446
9840d7f
e615217
4a06f52
f6041b9
813b7ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,10 @@ import { __ } from '@wordpress/i18n'; | |
import { | ||
getProductFieldObject, | ||
getProductImpressionObject, | ||
getProductId, | ||
formatPrice, | ||
} from './utils'; | ||
|
||
/** | ||
* Variable holding the current checkout step. It will be modified by trackCheckoutOption and trackCheckoutStep methods. | ||
* | ||
* @type {number} | ||
*/ | ||
let currentStep = -1; | ||
|
||
/** | ||
* Tracks view_item_list event | ||
* | ||
|
@@ -23,17 +17,19 @@ export const trackListProducts = ( { | |
products, | ||
listName = __( 'Product List', 'woocommerce-google-analytics-integration' ), | ||
} ) => { | ||
trackEvent( 'view_item_list', { | ||
event_category: 'engagement', | ||
event_label: __( | ||
'Viewing products', | ||
'woocommerce-google-analytics-integration' | ||
), | ||
items: products.map( ( product, index ) => ( { | ||
...getProductImpressionObject( product, listName ), | ||
list_position: index + 1, | ||
} ) ), | ||
} ); | ||
if ( products.length > 0 ) { | ||
trackEvent( 'view_item_list', { | ||
item_list_id: 'engagement', | ||
item_list_name: __( | ||
'Viewing products', | ||
'woocommerce-google-analytics-integration' | ||
), | ||
items: products.map( ( product, index ) => ( { | ||
...getProductImpressionObject( product, listName ), | ||
index: index + 1, | ||
} ) ), | ||
} ); | ||
} | ||
}; | ||
|
||
/** | ||
|
@@ -45,11 +41,6 @@ export const trackListProducts = ( { | |
*/ | ||
export const trackAddToCart = ( { product, quantity = 1 } ) => { | ||
trackEvent( 'add_to_cart', { | ||
event_category: 'ecommerce', | ||
event_label: __( | ||
'Add to Cart', | ||
'woocommerce-google-analytics-integration' | ||
), | ||
items: [ getProductFieldObject( product, quantity ) ], | ||
} ); | ||
}; | ||
|
@@ -63,11 +54,6 @@ export const trackAddToCart = ( { product, quantity = 1 } ) => { | |
*/ | ||
export const trackRemoveCartItem = ( { product, quantity = 1 } ) => { | ||
trackEvent( 'remove_from_cart', { | ||
event_category: 'ecommerce', | ||
event_label: __( | ||
'Remove Cart Item', | ||
'woocommerce-google-analytics-integration' | ||
), | ||
items: [ getProductFieldObject( product, quantity ) ], | ||
} ); | ||
}; | ||
|
@@ -91,70 +77,55 @@ export const trackChangeCartItemQuantity = ( { product, quantity = 1 } ) => { | |
}; | ||
|
||
/** | ||
* Track a begin_checkout and checkout_progress event | ||
* Notice calling this will set the current checkout step as the step provided in the parameter. | ||
* Track begin_checkout event | ||
* | ||
* @param {number} step The checkout step for to track | ||
* @return {(function( { storeCart: Object } ): void)} A callable receiving the cart to track the checkout event. | ||
* @param {Object} params The function params | ||
* @param {Object} params.storeCart The cart object | ||
*/ | ||
export const trackCheckoutStep = | ||
( step ) => | ||
( { storeCart } ) => { | ||
if ( currentStep === step ) { | ||
return; | ||
} | ||
|
||
trackEvent( step === 0 ? 'begin_checkout' : 'checkout_progress', { | ||
items: storeCart.cartItems.map( getProductFieldObject ), | ||
coupon: storeCart.cartCoupons[ 0 ]?.code || '', | ||
currency: storeCart.cartTotals.currency_code, | ||
value: formatPrice( | ||
storeCart.cartTotals.total_price, | ||
storeCart.cartTotals.currency_minor_unit | ||
), | ||
checkout_step: step, | ||
} ); | ||
|
||
currentStep = step; | ||
}; | ||
export const trackBeginCheckout = ( { storeCart } ) => { | ||
trackEvent( 'begin_checkout', { | ||
currency: storeCart.totals.currency_code, | ||
value: formatPrice( | ||
storeCart.totals.total_price, | ||
storeCart.totals.currency_minor_unit | ||
), | ||
Comment on lines
+89
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documentation states it should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch! Updated in 4a06f52 |
||
coupon: storeCart.coupons[ 0 ]?.code || '', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❔ There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
items: storeCart.items.map( getProductFieldObject ), | ||
} ); | ||
}; | ||
|
||
/** | ||
* Track a set_checkout_option event | ||
* Notice calling this will set the current checkout step as the step provided in the parameter. | ||
* | ||
* @param {Object} params The params from the option. | ||
* @param {number} params.step The step to track | ||
* @param {string} params.option The option to set in checkout | ||
* @param {string} params.value The value for the option | ||
* Track add_shipping_info event | ||
* | ||
* @return {(function() : void)} A callable to track the checkout event. | ||
* @param {Object} params The function params | ||
* @param {Object} params.storeCart The cart object | ||
*/ | ||
export const trackCheckoutOption = | ||
( { step, option, value } ) => | ||
() => { | ||
trackEvent( 'set_checkout_option', { | ||
checkout_step: step, | ||
checkout_option: option, | ||
value, | ||
} ); | ||
|
||
currentStep = step; | ||
}; | ||
export const trackShippingTier = ( { storeCart } ) => { | ||
trackEvent( 'add_shipping_info', { | ||
currency: storeCart.totals.currency_code, | ||
value: formatPrice( | ||
storeCart.totals.total_price, | ||
storeCart.totals.currency_minor_unit | ||
), | ||
coupon: storeCart.coupons[ 0 ]?.code || '', | ||
shipping_tier: | ||
storeCart.shippingRates[ 0 ]?.shipping_rates?.find( | ||
( rate ) => rate.selected | ||
)?.name || '', | ||
items: storeCart.items.map( getProductFieldObject ), | ||
} ); | ||
}; | ||
|
||
/** | ||
* Tracks select_content event. | ||
* | ||
* @param {Object} params The function params | ||
* @param {Object} params.product The product to track | ||
* @param {string} params.listName The name of the list in which the item was presented to the user. | ||
*/ | ||
export const trackSelectContent = ( { | ||
product, | ||
listName = __( 'Product List', 'woocommerce-google-analytics-integration' ), | ||
} ) => { | ||
export const trackSelectContent = ( { product } ) => { | ||
trackEvent( 'select_content', { | ||
content_type: 'product', | ||
items: [ getProductImpressionObject( product, listName ) ], | ||
content_id: getProductId( product ), | ||
} ); | ||
}; | ||
|
||
|
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.
📜 💅
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?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.
That would be a nice addition! I've opened issue #324 so as to not get off track in this PR.