Tutorial: google-analytics

google-analytics

Google Analytics

How to Install

Use Google Analytics Setup guide in order to create a snippet for your HUB2.

Integrate

Create a snippet that will subscribe to required events and put it into the "On Application Initialization" section. Instead of "// your code goes here" use Google Analytics API in order to push events into the system. Put this snippet into the "On Application Initialization" section.

Example of subscription to Playtech.Events.DEPOSIT_COMPLETED event that will send event via ga:

Playtech.on(Playtech.Events.DEPOSIT_COMPLETED, function (eventData) {
    ga('send', 'event', 'Deposit', 'Deposit_Success', 'Deposit_Success_Regular');
});

Example of configuration of GTAG

(function () {
    // track every visit
    window.gtag?.('event', 'visit');
    
    // track only first visit with the same browser
    try {
        if (!localStorage.getItem('first_visit')) {
            localStorage.setItem('first_visit', '1');
            window.gtag?.('event', 'first_visit');
        }
    } catch (error) {
        // noop
    }
    
    Playtech.on('LOGGED_IN', function () {
        window.gtag?.('event', 'login', {
            category: 'login',
            label: 'login success',
            customerID: Playtech.fetchUserDetails().userId
        });
    });

    Playtech.on('LOGGED_IN_FAIL', function () {
        window.gtag?.('event', 'login_fail', {
            'category': 'login',
            'label': 'login fail'
        });
    });

    Playtech.on('REGISTRATION_COMPLETED', function ([{ userId }]) {
        setTimeout(function () {
            window.gtag?.('event', 'register', {
                category: 'registration',
                label: 'registration completed',
                customerID: userId
            });
        }, 2000);
    });

    Playtech.on('REGISTRATION_FAIL', function () {
        window.gtag?.('event', 'register_fail', {
            'category': 'registration',
            'label': 'registration fail'
        });
    });

    Playtech.on('DEPOSIT_COMPLETED', async function() {
        const response = await Playtech.API.payments.getPaymentStatistics();

        if (response.deposits.totalDepositCount === '1') {
            window.gtag?.('event', 'first_deposit', {
                category: 'deposit',
                label: 'deposit first',
                customerID: Playtech.fetchUserDetails().userId
            });

        } else {
            window.gtag?.('event', 'deposit', {
                category: 'deposit',
                label: 'deposit completed',
                customerID: Playtech.fetchUserDetails().userId
            });
        }
    });

    Playtech.on('DEPOSIT_FAIL', function () {
        window.gtag?.('event', 'deposit_fail', {
            category: 'deposit',
            label: 'deposit fail',
            customerID: Playtech.fetchUserDetails().userId
        });
    });

    // nuvei deposit
    Playtech.on(Playtech.Events.SAFECHARGE_ANALYTICS, function ([event]) {
        const { action } = event;

        let eventName = null;

        switch (action) {
            case 'attempted deposit':
            case 'failed  deposit':
                eventName = 'deposit_fail';
                break;

            case 'attempted first deposit':
            case 'failed  first deposit':
                eventName = 'first_deposit_fail';
                break;

            case 'made first deposit':
                eventName = 'first_deposit';
                break;

            case 'made deposit':
                eventName = 'deposit';
                break;

            default:
                return;
        }

        const {
            deposit_amount,
            currency,
            label
        } = event;

        window.gtag?.('event', eventName, {
            category: 'deposit',
            label: label,
            deposit_amount,
            currency,
            customerID: Playtech.fetchUserDetails().userId
        });
    });

    // nuvei withdraw
    Playtech.on(Playtech.Events.SAFECHARGE_ANALYTICS, function ([event]) {
        const { action } = event;

        let eventName = null;

        switch (action) {
            case 'withdrawal_request_created':
                eventName = 'withdraw';
                break;

            default:
                return;
        }

        const {
            amount,
            currency,
            label
        } = event;

        window.gtag?.('event', eventName, {
            category: 'withdraw',
            label: label,
            amount,
            currency,
            customerID: Playtech.fetchUserDetails().userId
        });
    });

    // sportsbook
    Playtech.on('sportsbook-analytics', ({ eventName, userName, currency, raw }) => {
        const data = typeof raw === 'string' ? { data: raw }: raw;

        window.gtag?.('event', eventName, { ...data, currency, userName });
    });
})();