Configuring Analytics via Google Analytics

Step 1: Create new Class

Create a new class "AppGA" and add the following methods to it.

public static void send(Tracker tracker, HitBuilders.EventBuilder eventBuilder,
                            String category, String action, String label, HashMap<String, Object> properties) {
        if (eventBuilder == null) {
            eventBuilder = new HitBuilders.EventBuilder().setCategory(category).setAction(action).setLabel(label);
        }
        String eventName = getEventNameForGa(category, action, label);

        //Please pass the string "ga" for vendor if you are using Google Analytics to track the event.
        HashMap<String, String> hanselData = HanselTracker.logEvent(eventName, "ga", properties);
        if (properties == null) {
            properties = new HashMap<>();
        }
        properties.putAll(hanselData);
        for (String key : properties.keySet()) {
            eventBuilder.set("&" + key, properties.get(key).toString());
        }
        tracker.send(eventBuilder.build());
    }

    private static String getEventNameForGa(String category, String action, String label) {
        String eventName = "";
        if (category!=null) {
            eventName += category;
        }

        if (action!=null) {
            if (eventName.length() > 0) {
                eventName += "_" + action;
            } else {
                eventName += action;
            }
        }
      
        if (label!=null) {
            if (eventName.length() > 0) {
                eventName += "_" + label;
            } else {
                eventName += label;
            }
        }

        return eventName;
    }
fun send(tracker: Tracker, eventBuilder: HitBuilders.EventBuilder?,
             category: String, action: String, label: String, properties: HashMap<String, Any>?) {
        var eventBuilder = eventBuilder
        var properties = properties
        if (eventBuilder == null) {
            eventBuilder = HitBuilders.EventBuilder().setCategory(category).setAction(action).setLabel(label)
        }
        val eventName = getEventNameForGa(category, action, label)

        //Please pass the string "ga" for vendor if you are using Google Analytics to track the event.
        val hanselData = HanselTracker.logEvent(eventName, "ga", properties)
        if (properties == null) {
            properties = HashMap()
        }
        properties.putAll(hanselData)
        for (key in properties.keys) {
            eventBuilder!!.set("&$key", properties[key]!!.toString())
        }
        tracker.send(eventBuilder!!.build())
    }

    private fun getEventNameForGa(category: String, action: String, label: String): String {
        var eventName = ""
        if (HSLUtils.isSet(category)) {
            eventName += category
        }

        if (HSLUtils.isSet(action)) {
            if (eventName.length > 0) {
                eventName += "_$action"
            } else {
                eventName += action
            }
        }

        return eventName
    }
import {NativeModules} from 'react-native';

var AppGA = (function () {
  function logEvent(tracker, category, action, properties) {
    var mergedProperties = {};
    var eventName = getEventNameForGa(category, action);
    NativeModules.HanselTrackerRn.logEvent(eventName,"ga",properties,(hanselData) => {
        if(!properties) {properties = {};}
        mergedProperties = Object.assign(properties, hanselData);   
        tracker.trackEvent(category, action, properties);
    });
  }
})();

function getEventNameForGa(category, action) {
    var eventName = ""
    if (category && category.length > 0) {
        eventName += category
    }

    if (action && action.length > 0) {
        if (eventName.length > 0) {
            eventName += "_"+action;
        } else {
            eventName += action
        }
    }

    return eventName;
}

Step 2: Tracking Hansel changes

For all those events on which you want to track the impact of Hansel changes, make the updates as suggested in the snippet below:

//If the original code was
HitBuilders.EventBuilder eventBuilder = new HitBuilders.EventBuilder()
           .setCategory(category)
           .setAction(action)
           .setLabel(label)
           .set("<property_key>", "<property_value>")
           .set("<property_key>", "<property_value>");
gaTracker.send(eventBuilder.build());

//it would get updated to
HitBuilders.EventBuilder eventBuilder = new HitBuilders.EventBuilder()
           .setCategory(category)
           .setAction(action)
           .setLabel(label)
           .set("<property_key>", "<property_value>")
           .set("<property_key>", "<property_value>");
AppGA.send(gaTracker, eventBuilder, category, action, label);
//If the original code was
val eventBuilder: HitBuilders.EventBuilder = HitBuilders.EventBuilder()
           .setCategory(category)
           .setAction(action)
           .setLabel(label)
           .set("<property_key>", "<property_value>")
           .set("<property_key>", "<property_value>")
gaTracker.send(eventBuilder.build())

val eventBuilder: HitBuilders.EventBuilder = HitBuilders.EventBuilder()
           .setCategory(category)
           .setAction(action)
           .setLabel(label)
           .set("<property_key>", "<property_value>")
           .set("<property_key>", "<property_value>")
AppGA.send(gaTracker, eventBuilder, category, action, label)
//If the original code was
gaTracker.trackEvent(category, action, properties)

//it would get updated to
AppGA.logEvent(gaTracker, category, action, properties);