objective-cibm-mobilefirstmobilefirst-analytics

How to use Custom Analytics in native iOS with MobileFirst?


Where can I find a sample on how to use Custom Analytics using Objective-C in a native iOS app with IBM MobileFirst?

I want to be able to create custom value analytics charts in the MobileFirst analytics console.

I tried different coding versions to do that, but it does not work

NSDictionary *stringInventory = @{
                                  @"GERMANY" : @"1",
                                  @"USA" : @"2",
                                  @"GREECE" : @"3",
                                  @"UK" : @"4",
                                  };
NSDictionary *inventory = @{
                            @"GERMANY" : [NSNumber numberWithInt:1],
                            @"USA" : [NSNumber numberWithInt:2],
                            @"GREECE" : [NSNumber numberWithInt:3],
                            @"UK" : [NSNumber numberWithInt:3],
                            };
// OPTION 1
// *******
OCLogger *logger = [OCLogger getInstanceWithPackage:@"CalculatorViewController"];  // IBM MF LOGGER
[OCLogger setCapture:TRUE]; // IBM MF LOGGER 
[OCLogger setAutoSendLogs:TRUE]; // IBM MF LOGGER
[logger log:@"success - online"]; // IBM MF LOGGER
[logger log:@"Using OCLogger for WL Analytis"]; // IBM MF LOGGER  
[logger logWithLevel:OCLogger_ANALYTICS message:@"This is a Analyitics Message CalculatorViewController:onSuccess" args:@"USA" userInfo:inventory];  // IBM MF LOGGER
[logger logWithLevel:OCLogger_ANALYTICS message:@"This is a Analyitics Message CalculatorViewController:onSuccess" args:@"GERMANY" userInfo:stringInventory];  // IBM MF LOGGER

// Option 2
// ********
WLAnalytics *doAnaylitics = [WLAnalytics sharedInstance]; // IBM MF ANALYTICS
[doAnaylitics enable]; // IBM MF ANALYTICS
[doAnaylitics log:@"MY ANALYITIS LOG" withMetadata:inventory[@"GERMANY"]]; // IBM MF ANALYTICS
[doAnaylitics send]; // IBM MF ANALYTICS
//[doAnaylitics setValuesForKeysWithDictionary:stringInventory]; // IBM MF Analytics
[doAnaylitics log:@"MY ANALYITIS LOG" withMetadata:stringInventory[@"USA"]];
[doAnaylitics send];    // IBM MF ANALYTICS
[doAnaylitics disable]; // IBM MF ANALYTICS

I found this getting started but there is no sample for Objective-C.


Solution

  • The issue that you are coming across is the log data that you are trying to send. With the NSDictionary you want it to be a key value pair, right now you are only trying to send a value. You can have as many key value pairs as you want but one is the simplest.

    NSDictionary *inventory = @{
        @"GERMANY" : [NSNumber numberWithInt:1],
    };
    

    Using this NSDictionary my property for custom charts is going to be Germany and my term will be the number 1.

    To send this NSDictionary using WLAnalytics API you would make these API calls.

    [[WLAnalytics sharedInstance] log:@"Hello" withMetadata:inventory];
    [[WLAnalytics sharedInstance] send];