Krish Krish - 6 months ago 76
Objective-C Question

Google Analytics - How to send multiple dimensions for an event in ios

I am new to Google Analytics. In my project i have to send multiple dimensions for an event. Actually i need to raise an event with two dimensions, event raises twice because i am raising the event individually for each dimension. I googled for multiple dimensions and i found this, here i'm lil confused to convert it in to iOS (i mean in Objective-C).
Here is my code which i call for each dimension.

[tracker send:[[[GAIDictionaryBuilder createEventWithCategory:[eventDict valueForKey:@"category"]
action:[eventDict valueForKey:@"action"]
label:[eventDict valueForKey:@"label"]
value:[eventDict valueForKey:@"value"]]
set:nameStr forKey:[GAIFields customDimensionForIndex:indexval]] build]];


Please suggest me a best solution for mutiple dimensions for an event.

Answer

Try this solution this will help you, this solution worked for me.

Note - You need to set it to tracker,google analytics framework internally it sends the data that is send to the tracker. You can check by printing the logs.

For Screen Views you can do this.

 + (void)trackScreenStatusScreenName:(NSString *)screenName 
    {
        id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
        [tracker set:kGAIScreenName value:screenName];

        NSString * userStages = @"Some string";//dimension for user stages
        NSString * userID = @"Some user ID";
        NSString * contentStages = @"Some stage";

       [tracker set:[GAIFields customDimensionForIndex:1]
                      value:userID];
       [tracker set:[GAIFields customDimensionForIndex:2]
                          value:userStages];

       [tracker set:[GAIFields customDimensionForIndex:3] value:contentStages];

       [tracker send:[[GAIDictionaryBuilder createScreenView] build]];

    }

For event you can do this

+ (void)createEventWithCategory:(NSString *)eventCategory action:(NSString *)action label:(NSString *)label value:(NSNumber *)value
{
    id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

    NSString * userStages = @"Some string";//dimension for user stages
            NSString * userID = @"Some user ID";
            NSString * contentStages = @"Some stage";

           [tracker set:[GAIFields customDimensionForIndex:1]
                          value:userID];
           [tracker set:[GAIFields customDimensionForIndex:2]
                              value:userStages];
           [tracker set:[GAIFields customDimensionForIndex:3] value:contentStages];

    [tracker send:[[GAIDictionaryBuilder createEventWithCategory:eventCategory
                                                          action:action
                                                           label:label
                                                           value:value] build]];
}