Smartech

Configuring Analytics via Google Analytics

Step 1: Add a custom dimension called "hsl_data" in your google analytics account

You can add the custom dimension in the admin screen of your Google Analytics dashboard. Here is how your Google Analytics dashboard will look like after adding the custom dimension. Note down the index for the custom dimension added, it will be used in step 2.

Step 2: Create new Class

Create a new class "AppGA" and add the following method to it. Note the custom dimension index from step 1 and use the index in the sample code as shown below.

#import <SmartechNudges/Hansel-umbrella.h>

+ (void)send:(GAIDictionaryBuilder *) builder forCategory: (NSString*) category action:(NSString*) action label: (NSString*) label toTracker: (id<GAITracker>) tracker{
    NSString* eventName = [AppGA getEventNameForCategory:category action:action label:label];
    //Get the index of the custom dimenstion you added in step 1 and replace it here.
     NSUInteger index = 1;
  //Please pass the string "ga" for vendor if you are using Google Analytics to track the event.
  //get the data for all Interaction Maps created on hansel dashboard.
  NSDictionary* hanselData = [HanselTracker logEvent:eventName andVendor:@"ga" withProperties:[builder build]];
    if(hanselData[@"hsl_data"]){
           [builder set:hanselData[@"hsl_data"] forKey:[GAIFields customDimensionForIndex:index]];
       }
    [tracker send:[builder build]];
}

+ (NSString*) getEventNameForCategory:(NSString*) category action:(NSString*) action label: (NSString*) label{
    NSString* eventName = [NSString stringWithFormat:@"%@_%@_%@", category, action, label];
    return eventName;
}
class func send(_ builder: GAIDictionaryBuilder!, category: String!, action: String!, label: String!, tracker: GAITracker!){
    let eventName = getEventName(category: category, action: action, label: label)
        //Get the index of the custom dimenstion you added in step 1 and replace it here.
        let index = 1;
        //Please pass the string "ga" for vendor if you are using Google Analytics to track the event.
        //get the data for all Interaction Maps created on hansel dashboard.
        let hanselData = HanselTracker.logEvent(eventName, vendor: "ga", withProperties: builder.build() as? [AnyHashable : Any])
        if let hanselAnalyticsData = hanselData["hsl_data"] as? String {
                builder.set(hanselAnalyticsData, forKey: GAIFields.customDimension(for: UInt(index)))
        }
        tracker.send(builder.build() as! [AnyHashable : Any]!)
}

class func getEventName( category: String, action: String, label: String) -> String{
    let eventName = "\(category)_\(action)_\(label)"
    return eventName
}

Step 3: Update code

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

#import <SmartechNudges/Hansel-umbrella.h>

//If the original code was
GAIDictionaryBuilder* builder = [GAIDictionaryBuilder createEventWithCategory:category action:action label:label value:value];
[builder set:@"<property_key>" forKey:@"<property_value>"];
[builder set:@"<property_key>" forKey:@"<property_value>"];
[gaTracker send:[builder build]];

//it would get updated to
GAIDictionaryBuilder* builder = [GAIDictionaryBuilder createEventWithCategory:category action:action label:label value:value];
[builder set:@"<property_key>" forKey:@"<property_value>"];
[builder set:@"<property_key>" forKey:@"<property_value>"];
[AppGA send:builder forCategory:category action:action label:label toTracker:gaTracker];
//If the original code was
let builder = GAIDictionaryBuilder.createEvent(withCategory: category, action: action, label: label, value: value)
gaTracker?.send(builder?.build() as? [AnyHashable : Any])

//it would get updated to
let builder = GAIDictionaryBuilder.createEvent(withCategory: category, action: action, label: label, value: value)
AppGA.send(builder, category: category, action: action, label: label, tracker: gaTracker)

Updated 6 months ago


Next

Continue setting up Product experience

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.