Mobi2Go Analytics API

Flexibility is key in everything we do; in business, in technology & in the decisions we make.

That's why we have a Mobi2Go Analytics API in place which allows Mobi2Go to bind to analytics events (e.g. in custom scripts.)

This then allows you - the business owners & decision makers, to use it to add your own scripts to hook into our analytics & listen for various events, triggering JavaScript to make calls to your analytics service like Google Tag Manager.

 

Examples

Below are some examples of how this API can be used:

 
// Set certain variables on the dataLayer
startSession();

// When a store is selected
Mobi2Go.Analytics.bind({
    IDENTIFY_LOCATION: function(location) {
        // Google tag manager
        _event('location', 'set location', 'set location', location.name);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('store_selected', {name: location.name});
    }
});

// When a customer has logged in
Mobi2Go.Analytics.bind({
    IDENTIFY_CUSTOMER: function(customer) {
        // Google tag manager
        window.dataLayer.userId = customer.id;
        _event('customer', 'set customer', 'set customer', customer.id);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('customer_login', {name: customer.email});
    }
});

// When the last order is loaded
Mobi2Go.Analytics.bind({
    LAST_ORDER_LOADED: function(lastOrderState) {
        // Google tag manager
        _event('order', 'load last order', 'load last order', lastOrderState.id);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('last_order', {method: lastOrderState[0].method});
    }
});

// When a product menu category is viewed
Mobi2Go.Analytics.bind({
    VIEWED_PRODUCT_CATEGORY: function(category) {
        // Google tag manager
        _impressProducts(category.getProducts().toArray(), category.name);
       _event('menu', 'view category', 'view category', category.name);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('opened_category', {method: category[0].name});
    }
});

// When the product menu is viewed
Mobi2Go.Analytics.bind({
    VIEWED_PRODUCT_MENU: function(menu) {
        // Google tag manager
        _impressProducts(menu.getCategoryProducts().toArray(), menu.name);
        _event('menu', 'view menu', 'view menu', menu.name);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('opened_product_menu', {name: menu.name});
    }
});

// When a product is added to the cart
Mobi2Go.Analytics.bind({
    ADDED_PRODUCT: function(product) {
        // Google tag manager
        var ecommerce = {
            currencyCode: Mobi2Go.headoffice.currency_abrv,
            add: {
                products: [
                    _formatProduct(product)
                ]
            }
        };

        _event('AddToCart', null, null, null, ecommerce);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('add_product', {name: product.name, price: product.price.toFloat()});
    }
});

// When a product is in the cart is edited
Mobi2Go.Analytics.bind({
    EDITED_PRODUCT: function(product) {
        // Google tag manager
        var ecommerce = {
            detail: {
                products: [
                    _formatProduct(product)
                ]
            }
        };

        _event('detailProduct', null, null, null, ecommerce);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('edit_product', {name: product.name});
    }
});

// When a product is in the cart is removed
Mobi2Go.Analytics.bind({
    REMOVED_PRODUCT: function(product) {
        // Google tag manager
        if (Order.isResetting()) {
            return;
        }

        var ecommerce = {
            remove: {
                products: [
                    _formatProduct(product)
                ]
            }
        };

        _event('removeFromCart', null, null, null, ecommerce);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('remove_product', {name: product.name});
    }
});

// When a user clicks the 'Next step' button
Mobi2Go.Analytics.bind({
    EVENTS.STARTED_CHECKOUT: function() {
        // Google tag manager
        var ecommerce = {
            checkout: {
                actionField: {
                    step: 1
                },
                products: [
                    _formatProducts()
                ]
            }
        };

        _event('checkoutPaymentStep1', null, null, null, ecommerce);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('start_checkout');
    }
});

// When a user clicks the 'Pay Now' button
Mobi2Go.Analytics.bind({
    STARTED_PAYMENT: function(order) {
        // Google tag manager
        var ecommerce = {
            checkout: {
                actionField: {
                    step: 2,
                    option: order.getPaymentMethod()
                },
                products: [
                    _formatProducts()
                ]
            }
        };

        _event('checkoutPaymentStep2', null, null, null, ecommerce);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('start_payment', {total: order.getTotal().toFloat()});
    }
});

// When the order is confirmed (paid for)
Mobi2Go.Analytics.bind(
    CONFIRMED_ORDER: function(order) {
        // Google tag manager
        if (order.getPaymentMethod() === 'offline') {
            checkoutPaymentStep(order);
        }

        var ecommerce = {
            purchase: {
                actionField: {
                    id: order.getId(),
                    revenue: order.getTotal().getTaxExclusivePrice().toFloat(),
                    tax: order.getTotal().getTaxPrice().toFloat(),
                    shipping: order.getDeliveryFee().toFloat(),
                    coupon: order.getVoucherCode()
                },
                products: [
                    _formatProducts()
                ]
            }
        }

        _event('purchase', null, null, null, ecommerce);
        _event('order', 'confirmed', 'confirmed');

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('purchase', {total: order.getTotal().toFloat()});
    }
);

// When the credit card details are saved after order confirmation
Mobi2Go.Analytics.bind({
    CREDIT_CARD_SAVED: function(order) {
        // Google tag manager
        _event('order', 'save credit card', 'save credit card')

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('card_saved');
    }
});

// Helper methods
function startSession() {
    window.dataLayer.appName = Mobi2Go.app.getPlatform();
    window.dataLayer.appId = Mobi2Go.headoffice.domain_name + '-' + Mobi2Go.app.getPlatform();
    window.dataLayer.currencyCode = Mobi2Go.headoffice.currency_abrv;
}

function _event(event, action, label, value, ecommerce) {
    // Only raise events if we're not already doing so via m2g_ga
    if (!window.m2g_ga_loaded_by_mobi2go) {
        window.dataLayer.push({
            event: event,
            action: action,
            label: label,
            value: value,
            ecommerce: ecommerce
        });
    }
}

function _impressProducts (menu_products, list_name) {
    var impressions = [];

    _.each(menu_products, function(menu_product, position) {
        var impression = _impressProduct(menu_product, list_name, position +  1);
        impressions.push(impression);
    });

    var ecommerce = {
        currencyCode: Mobi2Go.headoffice.currency_abrv,
        impressions: impressions
    };

    _event('impressProducts', null, null, null, ecommerce);
}

function _impressProduct (menu_product, list_name, position) {
    var impression = {
        id: menu_product.id,
        name: menu_product.name,
        list: list_name,
        category: menu_product.category ? menu_product.category.name : null,
        position: position
    }

    return impression;
}

function _formatProducts() {
    var products = [];

    _.each(Order.getProducts(), function(product) {
        var product_to_track = _formatProduct(product);
        products.push(product_to_track)
    });

    return products;
}

function _formatProduct(product) {
    var product = {
        id: product.menu_product.id,
        name: product.menu_product.name,
        price: product.price.toFloat(),
        quantity: product.quantity,
        variant: product.size ? product.size.menu_modifier.name : null,
        category: product.menu_product.category
            ? product.menu_product.category.name
            : null,
        position: product.menu_product.category_list_position
            ? product.menu_product.category_list_position
            : null
    };

    return product;
}

Note

Event bindings can be combined, and if using ES6, you can use the Mobi2Go.Analytics.EVENTS enumerated properties, for example the below:

let Analytics = Mobi2Go.Analytics;
let Events = Mobi2Go.Analytics.Events;

// When a store is selected
Analytics.bind({
    [EVENTS.IDENTIFY_LOCATION]: function(location) {
        // Google tag manager
        _event('location', 'set location', 'set location', location.name);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('store_selected', {name: location.name});
    },
    [EVENTS.LAST_ORDER_LOADED]: function(lastOrderState) {
        // Google tag manager
        _event('order', 'load last order', 'load last order', lastOrderState.id);

        // Generic analytic service (such as Google Analytics)
        acme_tracker.track('last_order', {method: lastOrderState[0].method});
    }
});
0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.