Skip to Content
Menu

ecommerceTracking()

Track events in Google Analytics specific to Ecommerce.

JavaScript February 7, 2021

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

WooCommerce must be activated for these events to be enabled.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/analytics.js on line 1100.

    No Hooks

    This function does not have any filters or actions available. Request one?

    Note: This function contains 2 to-do comments.

    JavaScript
    nebula.ecommerceTracking = async function(){
        if ( nebula.site?.ecommerce ){
            //Add to Cart clicks
            nebula.dom.document.on('click', 'a.add_to_cart, .single_add_to_cart_button', function(e){ //@todo "Nebula" 0: is there a trigger from WooCommerce this can listen for?
                let thisEvent = {
                    event: e,
                    category: 'Ecommerce',
                    action: 'Add to Cart', //GA4 Name: "add_to_cart"
                    product: jQuery(this).attr('data-product_id')
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.product);
                window.dataLayer.push(Object.assign(thisEvent, {'event': 'nebula-add-to-cart'}));
                nebula.fbq('track', 'AddToCart');
                nebula.clarity('set', thisEvent.category, thisEvent.action);
                nebula.crm('event', 'Ecommerce Add to Cart');
            });
    
            //Update cart clicks
            nebula.dom.document.on('click', '.button[name="update_cart"]', function(e){
                let thisEvent = {
                    event: e,
                    category: 'Ecommerce',
                    action: 'Update Cart Button',
                    label: 'Update Cart button click'
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.label);
                window.dataLayer.push(Object.assign(thisEvent, {'event': 'nebula-update-cart'}));
                nebula.crm('event', 'Ecommerce Update Cart');
            });
    
            //Product Remove buttons
            nebula.dom.document.on('click', '.product-remove a.remove', function(e){
                let thisEvent = {
                    event: e,
                    category: 'Ecommerce',
                    action: 'Remove This Item', //GA4 Name: "remove_from_cart"
                    product: jQuery(this).attr('data-product_id')
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.product);
                window.dataLayer.push(Object.assign(thisEvent, {'event': 'nebula-remove-item'}));
                nebula.crm('event', 'Ecommerce Remove From Cart');
            });
    
            //Proceed to Checkout
            nebula.dom.document.on('click', '.wc-proceed-to-checkout .checkout-button', function(e){
                let thisEvent = {
                    event: e,
                    category: 'Ecommerce',
                    action: 'Proceed to Checkout Button', //GA4 Name: "begin_checkout"
                    label: 'Proceed to Checkout button click'
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.label);
                window.dataLayer.push(Object.assign(thisEvent, {'event': 'nebula-proceed-to-checkout'}));
                nebula.fbq('track', 'InitiateCheckout');
                nebula.clarity('set', thisEvent.category, thisEvent.action);
                nebula.crm('event', 'Ecommerce Proceed to Checkout');
            });
    
            //Checkout form timing
            nebula.dom.document.on('click focus', '#billing_first_name', function(e){
                nebula.timer('(Nebula) Ecommerce Checkout', 'start');
    
                let thisEvent = {
                    event: e,
                    category: 'Ecommerce',
                    action: 'Started Checkout Form', //GA4 Name: "checkout_progress"?
                    label: 'Began filling out the checkout form (Billing First Name)'
                };
    
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.label);
                window.dataLayer.push(Object.assign(thisEvent, {'event': 'nebula-started-checkout-form'}));
                nebula.crm('event', 'Ecommerce Started Checkout Form');
            });
    
            //Place order button
            nebula.dom.document.on('click', '#place_order', function(e){
                let thisEvent = {
                    event: e,
                    category: 'Ecommerce',
                    action: 'Place Order Button', //GA4 Name: "purchase"
                    label: 'Place Order button click'
                };
    
                ga('send', 'timing', 'Ecommerce', 'Checkout Form', Math.round(nebula.timer('(Nebula) Ecommerce Checkout', 'end')), 'Billing details start to Place Order button click');
                nebula.dom.document.trigger('nebula_event', thisEvent);
                ga('send', 'event', thisEvent.category, thisEvent.action, thisEvent.label);
                window.dataLayer.push(Object.assign(thisEvent, {'event': 'nebula-place-order-button'}));
                nebula.fbq('track', 'Purchase');
                nebula.clarity('set', thisEvent.category, thisEvent.action);
                nebula.crm('event', 'Ecommerce Placed Order');
                nebula.crm('identify', {hs_lifecyclestage_customer_date: 1}); //@todo "Nebula" 0: What kind of date format does Hubspot expect here?
            });
        }
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name.

    JavaScript
    nebula.ecommerceTracking = function(){
        //Write your own code here, leave it blank, or return false.
    }