Set Up A Custom JS Event In Alpha Insights Event Tracking
Alpha Insights tracks WooCommerce Events and interactions both on the frontend and on the server side.
The reports are configured to display some metrics against custom events, with added support planned for the future.
So if you would like to track custom events, much like you would using Google Analytics or Facebook Pixel, you can do so with the following instructions
Javascript Custom Events – Register Your File With The Dependency
The first thing you will want to do is to declare a script that comes after the Alpha Insights WooCommerce Event Tracking Script:
wp_register_script( 'your-custom-js-file', '/path/to/your/jsfile.js', array( 'jquery', 'wpd-alpha-insights-event-tracking' ), false, true );
And then you need to trigger the event in your custom JS, below is an example of the JS used to track product clicks which is built into Alpha Insights.
Example Code To Send JS Tracking Event
jQuery(document).ready(function($) {
$('.products .product a').click(function() {
var product_id = 0;
var pid = $(this).closest('.product').find('.wpd-ai-event-tracking-product-id').data('product-id');
if ( pid ) {
product_id = pid;
}
var payload = {
event_type: 'product_click', // Required
event_quantity: 1,
event_value: 0,
object_id: product_id,
object_type: 'product',
product_id: product_id,
variation_id: 0,
};
var product_click = WpdAiEventTracking( payload );
});
});
The only required parameter is the event_type, although we recommend you set values for each parameter as it will enrich the database.
Your JS file must be loaded after the woocommerce event tracking file otherwise you will not have access to the event tracking function WpdAiEventTracking.