Smartech

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
    }

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
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);

Updated 2 months ago


Next

You are done configuring analytics events for triggers and goals, go back to this page and follow further steps to complete the Product Experience integration!

Product Experience

Configuring Analytics via Google Analytics


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.