Getting Analysis Results with the API

We can extract the analysis results via the iOS SDK. To do so, we need to specify the following information:

  • Dimension/Grouping Key (which field?). If omitted, the default view will be used.
  • Filters (if any). When multiple filters are defined, they will be concatenated using "AND".
  • Date range.

Please specify the target rule with its rule ID. The rule ID is shown on the developer portal. For setting dimensions and filters, please use the value(s) set as "Column" when you define your Conversion Rule.

Here is the sample code for getting the analysis result.

Objective-C:

  • // Define a filter.
    KAFilter *filter = [[KAFilter alloc] init];
    [filter addFilter:@"AppVersion"
            withValue:@"9"];
    
    // Define a data range.
    NSDate *start = [NSDate dateWithTimeIntervalSinceNow:-1*60*60*24*7]; // One week ago
    NSDate *end = [NSDate date]; // Now
    KADateRange *range = [KADateRange rangeWithStart:start
                                              andEnd:end];
    
    // Create a query.
    KAResultQuery *query = [[KAResultQuery alloc] init];
    // Set the "UserLevel" dimension to the query.
    [query setGroupingKey:@"UserLevel"];
    // Set the filter to the query.
    [query setFilter:filter];
    // Set the date range to the query.
    [query setDateRange:range];
    
    NSString *aggregationID = @"22";
    
    // Get snapshots.
    NSError *error;
    KAGroupedResult *results = [KiiAnalytics getResultSynchronousWithID:aggregationID
                                                               andQuery:query
                                                               andError:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Define a filter.
    KAFilter *filter = [[KAFilter alloc] init];
    [filter addFilter:@"AppVersion"
            withValue:@"9"];
    
    // Define a data range.
    NSDate *start = [NSDate dateWithTimeIntervalSinceNow:-1*60*60*24*7]; // One week ago
    NSDate *end = [NSDate date]; // Now
    KADateRange *range = [KADateRange rangeWithStart:start
                                              andEnd:end];
    
    // Create a query.
    KAResultQuery *query = [[KAResultQuery alloc] init];
    // Set the "UserLevel" dimension to the query.
    [query setGroupingKey:@"UserLevel"];
    // Set the filter to the query.
    [query setFilter:filter];
    // Set the date range to the query.
    [query setDateRange:range];
    
    NSString *aggregationID = @"22";
    
    // Get snapshots.
    [KiiAnalytics getResultWithID:aggregationID
                         andQuery:query
                         andBlock:^(KAGroupedResult *results, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

Swift:

  • // Define a filter.
    let filter = KAFilter()
    filter.addFilter("AppVersion", withValue: "9")
    
    // Define a data range.
    let start = NSDate(timeIntervalSinceNow: -1*60*60*24*7)
    let end = NSDate()
    let range = KADateRange(start: start, andEnd: end)
    
    // Create a query.
    let query = KAResultQuery()
    // Set the "UserLevel" dimension to the query.
    query.groupingKey = "UserLevel"
    // Set the filter to the query.
    query.filter = filter
    // Set the date range to the query.
    query.dateRange = range;
    
    let aggregationID = "22"
    
    // Get snapshots.
    do{
      let result: KAGroupedResult! = try KiiAnalytics.getResultSynchronousWithID(aggregationID, andQuery: query)
      // Do something.
    }catch let error as NSError{
      // Handle the error.
      return
    }
  • // Define a filter.
    let filter = KAFilter()
    filter.addFilter("AppVersion", withValue: "9")
    
    // Define a data range.
    let start = NSDate(timeIntervalSinceNow: -1*60*60*24*7)
    let end = NSDate()
    let range = KADateRange(start: start, andEnd: end)
    
    // Create a query.
    let query = KAResultQuery()
    // Set the "UserLevel" dimension to the query.
    query.groupingKey = "UserLevel"
    // Set the filter to the query.
    query.filter = filter
    // Set the date range to the query.
    query.dateRange = range;
    
    let aggregationID = "22"
    
    // Get snapshots.
    KiiAnalytics.getResultWithID(aggregationID, andQuery: query, andBlock:
      { (result: KAGroupedResult?, error: NSError?) -> Void in
        if (error != nil) {
          // Handle the error.
          return
        }
      }
    )

The analysis result will be returned as an object of KAGroupedResult, with a snapshots property containing NSArray of KAGroupedSnapShot. Each KAGroupedSnapShot contains the following properties:

  • Name: The name of the dimension/grouping key.
  • Data: The JSON Array that contains the data retrieved by the query.
  • PointStart: The date (milliseconds since UNIX epoch in UTC) in which the data starts.
  • PointInterval: Interval (in milliseconds) between data points.

Please check our appledoc for more details on how to interpret GroupSnapShot.