JSFiddle - React, Tailwind, and code Playground

by Steven Senkus

JavaScript

var $window = window;
$window.ga = function() {
    console.log('$window.GA', JSON.stringify(arguments));
};

function sendEvent(category, action, label, value) {
    if (!category || !action) {
        return;
    }

    var fieldsObj = {
        hitType: 'event'
    };

    fieldsObj.eventCategory = category;
    fieldsObj.eventAction = action;

    if (label) {
        fieldsObj.eventLabel = label;
    }

    if (value) {
        fieldsObj.eventValue = value;
    }

    $window.ga('send', fieldsObj);
}

function sendEventExtend(category, action, label, value) {
    var fieldsObj = {
        hitType: 'event'
    }
    if (!category || !action) {
        return;
    } else {
        _.extend(fieldsObj, {
            eventCategory: category,
            eventAction: action,
            eventLabel: label,
            eventValue: value
        });
    }
    $window.ga('send', fieldsObj);
}
sendEvent('cat', 'act','lab', 'val')
sendEventExtend('cat', 'act','lab', 'val')
sendEvent('cat', 'act','lab', null)
sendEventExtend('cat', 'act','lab', null)