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 classic pages #328

Merged
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
0e945df
Remove inline JS tracking
Nov 8, 2023
5eee69e
Remove abstract methods
Nov 8, 2023
273ef92
Add inline script data
Nov 8, 2023
01facf0
Format and attach event data in abstract class
Nov 9, 2023
18f6ba1
Map classic hooks to Blocks and inline event data
Nov 10, 2023
4d33305
Update wcgaiData single product name
Nov 17, 2023
6a563e2
Remove key for wcgaiData products
Nov 17, 2023
3506c88
Fix array keys
Nov 17, 2023
738f3ec
Update event map and script data
Nov 17, 2023
d5a659e
Add a tracker utility
Nov 17, 2023
977f49a
Update utils
Nov 17, 2023
e24a184
Move actions to data formatting functions
Nov 17, 2023
bb6ecc7
Add tracker utility
Nov 17, 2023
bd5f4cf
Add blocks and classic integrations
Nov 17, 2023
e14d27f
Add config file
Nov 17, 2023
3a36b96
Add new entry point
Nov 17, 2023
a2349aa
Remove enqueue_standard_tracking_code
Nov 20, 2023
82d2ce2
Update method signature
Nov 20, 2023
5ebea9c
Remove event map
Dec 7, 2023
d135e41
Rename data formatters
Dec 7, 2023
1fb3336
Restructure Tracker
Dec 7, 2023
d114b28
Update tracking
Dec 7, 2023
d7d9add
Reduce categories data to name only
Dec 7, 2023
4d65dfc
Update event => WC hook map
Dec 7, 2023
08ce30e
Update Tracker
Dec 10, 2023
326c59e
Add utility to find a single product by its ID
Dec 10, 2023
58a6fc2
Allow for single entry for data type in script data
Dec 10, 2023
89900a1
Track add to cart events
Dec 10, 2023
cabeb45
Fix JS formatting issues
martynmjones Jan 18, 2024
87d82f6
Remove unused PHP tests
martynmjones Jan 18, 2024
e4dd675
Update composer lock file
martynmjones Jan 18, 2024
6301d5e
Update composer.json
martynmjones Jan 18, 2024
17abd40
Remove calls to deleted functions
martynmjones Jan 19, 2024
d9c754b
Update cart formatting
martynmjones Jan 23, 2024
8616b72
Add remove_from_cart tracking
martynmjones Jan 23, 2024
67bfd6b
Add begin_checkout tracking
martynmjones Jan 24, 2024
3c4f670
Add select_content tracking
martynmjones Jan 24, 2024
af0b665
Add purchase tracking
martynmjones Jan 24, 2024
3a24994
Fix param description
martynmjones Jan 24, 2024
e12c442
Fix price formatting
martynmjones Jan 25, 2024
f99c620
Add check for enabled events
martynmjones Jan 25, 2024
5ef1fb6
Use correct product identifier
martynmjones Jan 25, 2024
2646ad9
Add product identifier to cart item
martynmjones Jan 25, 2024
48ee481
Add unit tests
martynmjones Jan 25, 2024
dc7fb24
Refactor test_script_data to support previous versions of PHP
martynmjones Jan 25, 2024
66142e2
Refactor getProductId util
martynmjones Jan 25, 2024
9104cb9
Add support for product variations
martynmjones Jan 26, 2024
9ba0a7f
Update plugin name to Google Analytics for WooCommerce
martynmjones Jan 31, 2024
2ea428c
Additional name updates
martynmjones Feb 1, 2024
155b32f
Merge pull request #353 from woocommerce/update/branding-to-google-an…
martynmjones Feb 1, 2024
5395991
Simplify tracker event handler API,
tomalec Feb 8, 2024
8fa9baf
Merge pull request #357 from woocommerce/update/tracking-for-classic-…
martynmjones Feb 9, 2024
c1bb15a
Fix price formatting
martynmjones Feb 9, 2024
b1a0de1
Remove unnecessary check
martynmjones Feb 9, 2024
bed5877
Remove demo comment
martynmjones Feb 9, 2024
7fcab41
Assign formatter to constant inside eventHandler
martynmjones Feb 9, 2024
c4931e3
Simplify set_script_data
martynmjones Feb 9, 2024
46dd222
Rename tracker_var to tracker_function_name
martynmjones Feb 9, 2024
930cf21
Update unit tests
martynmjones Feb 9, 2024
db37466
Update config export
martynmjones Feb 9, 2024
d625984
Fix addedToCart declaration
martynmjones Feb 9, 2024
9f60881
Remove spread operator
martynmjones Feb 19, 2024
9a1e058
Update classic select_content tracking
martynmjones Feb 26, 2024
234fe84
Update remove_from_cart tracking and add mini-cart support
martynmjones Feb 26, 2024
7ff8829
Update select_content selector
martynmjones Feb 26, 2024
8272538
Add comment describing why some actions are removed
martynmjones Feb 26, 2024
81f719e
Fix linting errors
martynmjones Feb 26, 2024
1bd5af3
Fix ID mismatch when removing a variation from cart
martynmjones Feb 26, 2024
c979cf0
Adjust append_script_data
martynmjones Feb 26, 2024
e893d8c
Avoid overwriting third-party event handlers
martynmjones Feb 27, 2024
94cc036
Adjust select_content conditions and update comment
martynmjones Feb 27, 2024
e27406d
Fix spacing
martynmjones Feb 27, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 0 additions & 140 deletions assets/js/src/actions.js

This file was deleted.

6 changes: 6 additions & 0 deletions assets/js/src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* global wcgaiData */
export const config = wcgaiData.config ?? {};
export const events = wcgaiData.events ?? {};
export const cart = wcgaiData.cart ?? {};
export const products = wcgaiData.products ?? {};
export const product = wcgaiData.product ?? {};
14 changes: 0 additions & 14 deletions assets/js/src/ga-integration.js

This file was deleted.

90 changes: 90 additions & 0 deletions assets/js/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { tracker } from './tracker';

import {
trackBeginCheckout,
trackShippingTier,
trackListProducts,
trackAddToCart,
// trackChangeCartItemQuantity,
trackRemoveCartItem,
trackSelectContent,
trackSearch,
trackViewItem,
trackException,
} from './tracker/data-formatting';

/**
* Register all Google Analytics 4 events that can be tracked
*/
tracker.setupEvents( [
{
/**
* @see https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#begin_checkout
*/
name: 'begin_checkout',
callback: trackBeginCheckout,
martynmjones marked this conversation as resolved.
Show resolved Hide resolved
},
{
/**
* @see https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#add_shipping_info
*/
name: 'add_shipping_info',
callback: trackShippingTier,
},
{
/**
* @see https://developers.google.com/gtagjs/reference/ga4-events#view_item_list
*/
name: 'view_item_list',
callback: trackListProducts,
},
{
/**
* @see https://developers.google.com/gtagjs/reference/ga4-events#add_to_cart
*/
name: 'add_to_cart',
callback: trackAddToCart,
},
{
/**
* @see https://developers.google.com/gtagjs/reference/ga4-events#remove_from_cart
*/
name: 'remove_from_cart',
callback: trackRemoveCartItem,
},
{
/**
* @see https://developers.google.com/gtagjs/reference/ga4-events#select_content
*/
name: 'select_content',
callback: trackSelectContent,
},
{
/**
* @see https://developers.google.com/gtagjs/reference/ga4-events#search
*/
name: 'search',
callback: trackSearch,
},
{
/**
* @see https://developers.google.com/gtagjs/reference/ga4-events#view_item
*/
name: 'view_item',
callback: trackViewItem,
},
{
/**
* @see https://developers.google.com/analytics/devguides/collection/ga4/reference/events?client_type=gtag#exception
*/
name: 'exception',
callback: trackException,
},
] );

// Initialize tracking for classic WooCommerce pages
import { trackClassicIntegration } from './integrations/classic';
trackClassicIntegration();

// Initialize tracking for Block based WooCommerce pages
import './integrations/blocks';
27 changes: 27 additions & 0 deletions assets/js/src/integrations/blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { removeAction } from '@wordpress/hooks';
import { addUniqueAction } from '../utils';
import { tracker } from '../tracker';
import { ACTION_PREFIX, NAMESPACE } from '../constants';

addUniqueAction(
`${ ACTION_PREFIX }-product-list-render`,
NAMESPACE,
tracker.event( 'view_item_list' ).attach
);

/**
* Temporarily remove all actions for demo purposes.
Copy link
Member

Choose a reason for hiding this comment

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

Is this comment still relevant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Removed in bed5877

Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment about why we remove those actions and who creates them?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment added 8272538

*/

removeAction( `${ ACTION_PREFIX }-checkout-render-checkout-form`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-checkout-submit`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-checkout-set-email-address`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-checkout-set-phone-number`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-checkout-set-billing-address`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-cart-add-item`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-cart-set-item-quantity`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-cart-remove-item`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-product-view-link`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-product-search`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-product-render`, NAMESPACE );
removeAction( `${ ACTION_PREFIX }-store-notice-create`, NAMESPACE );
29 changes: 29 additions & 0 deletions assets/js/src/integrations/classic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { tracker } from '../tracker';
import { events, cart, products, product } from '../config.js';

/**
* The Google Analytics integration for classic WooCommerce pages
* triggers events using three different methods.
*
* 1. Automatically attach events listed in the global `wcgaiData.events` object.
* 2. Listen for custom jQuery events triggered by core WooCommerce.
* 3. Listen for various actions (i.e clicks) on specific elements.
*/

export const trackClassicIntegration = () => {
const eventData = {
storeCart: cart,
products,
product,
};

Object.values( events ?? {} ).forEach( ( event ) => {
switch ( event ) {
// If the queued event name matches an event that has been registered with the
// tracker then automatically attach the event using the default data structures.
case tracker.event( event ).get()?.name:
tracker.event( event ).attach( eventData );
break;
}
} );
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getProductId,
formatPrice,
getCartCoupon,
} from './utils';
} from '../utils';

/**
* Tracks view_item_list event
Expand All @@ -18,19 +18,21 @@ export const trackListProducts = ( {
products,
listName = __( 'Product List', 'woocommerce-google-analytics-integration' ),
} ) => {
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,
} ) ),
} );
if ( products.length === 0 ) {
return false;
}

return {
item_list_id: 'engagement',
item_list_name: __(
'Viewing products',
'woocommerce-google-analytics-integration'
),
items: products.map( ( product, index ) => ( {
...getProductImpressionObject( product, listName ),
index: index + 1,
} ) ),
};
};

/**
Expand Down Expand Up @@ -84,15 +86,15 @@ export const trackChangeCartItemQuantity = ( { product, quantity = 1 } ) => {
* @param {Object} params.storeCart The cart object
*/
export const trackBeginCheckout = ( { storeCart } ) => {
trackEvent( 'begin_checkout', {
return {
currency: storeCart.totals.currency_code,
value: formatPrice(
storeCart.totals.total_price,
storeCart.totals.currency_minor_unit
),
...getCartCoupon( storeCart ),
items: storeCart.items.map( getProductFieldObject ),
} );
};
};

/**
Expand Down Expand Up @@ -154,9 +156,9 @@ export const trackViewItem = ( {
listName = __( 'Product List', 'woocommerce-google-analytics-integration' ),
} ) => {
if ( product ) {
trackEvent( 'view_item', {
return {
items: [ getProductImpressionObject( product, listName ) ],
} );
};
}
};

martynmjones marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Loading
Loading