Skip to content

Commit

Permalink
Track add to cart events
Browse files Browse the repository at this point in the history
  • Loading branch information
Martyn Jones committed Dec 10, 2023
1 parent 58a6fc2 commit 89900a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
14 changes: 12 additions & 2 deletions assets/js/src/integrations/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ addUniqueAction(
tracker.event( 'view_item_list' ).handler
);

addUniqueAction(
`${ ACTION_PREFIX }-product-render`,
NAMESPACE,
tracker.event( 'view_item' ).handler
);

addUniqueAction(
`${ ACTION_PREFIX }-cart-add-item`,
NAMESPACE,
tracker.event( 'add_to_cart' ).handler
);

/**
* Temporarily remove all actions for demo purposes.
*/
Expand All @@ -18,10 +30,8 @@ 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 );
21 changes: 19 additions & 2 deletions assets/js/src/integrations/classic.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { tracker } from '../tracker';
import { getProductFromID } from '../utils';
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.
* 1. Automatically handle events listed in the global `wcgaiData.events` object.
* 2. Listen for custom events from WooCommerce core.
* 3. Listen for various actions (i.e clicks) on specific elements.
*/

Expand All @@ -20,4 +21,20 @@ export const trackClassicIntegration = () => {
Object.values( events ?? {} ).forEach( ( eventName ) => {
tracker.event( eventName ).handler( eventData );
} );

/**
* Track the custom add to cart event dispatched by WooCommerce Core
*
* @param {Event} e - The event object
* @param {Object} fragments - An object containing fragments of the updated cart.
* @param {string} cartHash - A string representing the hash of the cart after the update.
* @param {HTMLElement[]} button - An array of HTML elements representing the add to cart button.
*/
document.body.onadded_to_cart = ( e, fragments, cartHash, button ) => {
tracker.event( 'add_to_cart' ).handler( {
product: getProductFromID(
parseInt( button[ 0 ].dataset.product_id )
),
} );
};
};

0 comments on commit 89900a1

Please sign in to comment.