Skip to content

How to setup google analytics or similar

Davide Casali edited this page Oct 15, 2013 · 7 revisions

 

AVAILABLE SINCE BAKER 4.2 - FEATURE STILL IN DEVELOPMENT

For more insights, read:

 

Step 1: add the analytics library you need

Get the analytics library you want to use. A few options are:

Add the library to your Baker Xcode project. In case of Google Analytics SDK 3.0 you must add:

  • GAI.h
  • GAITracker.h
  • GAITrackedViewController.h
  • GAIDictionaryBuilder.h
  • GAIFields.h
  • GAILogger.h
  • libGoogleAnalyticsServices.a

Please note that there's also a libGoogleAnalytics_debug.a, but it will not work (unless you know what you're doing). Specifically, the Google Analytics SDK uses the CoreData and SystemConfiguration frameworks, so you will need to add the following to your application target's linked libraries (select the Target and add them under Summary):

  • libGoogleAnalyticsServices.a
  • CoreData.framework
  • SystemConfiguration.framework

Step 2: initialize the library

Open BakerAnalyticsEvents.m and:

  1. Import the library adding at the beginning of the file:
  • #import "GAI.h" and #import "GAIDictionaryBuilder.h" for Google Analytics
  • #import "Countly.h" for Countly
  1. Add the tracking initialization inside the function init with your tracking keys, as provided by the service.
  • tracker = [ [GAI sharedInstance] trackerWithTrackingId:@"YOUR_TRACKING_KEY" ]; for Google Analytics
  • [ [Countly sharedInstance] start:@"YOUR_APP_KEY" withHost:@"https://YOUR_API_HOST.com" ]; for Countly

Step 3: fire the event

Again in BakerAnalyticsEvents.m add the event calls you need under receiveEvent:.

For example, using Google Analytics it could be something like:

    if ([[notification name] isEqualToString:@"BakerApplicationStart"]) {
        [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                      action:@"open"  // Event action (required)
                                                       label:@"Baker App Open"          // Event label
                                                       value:nil] build]];    // Event value
    } else if ([[notification name] isEqualToString:@"BakerIssueDownload"]) {
        [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                      action:@"open"  // Event action (required)
                                                       label:@"Baker Issue Download"          // Event label
                                                       value:nil] build]];    // Event value
    } else if ([[notification name] isEqualToString:@"BakerIssueOpen"]) {
        [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                      action:@"open"  // Event action (required)
                                                       label:@"Baker Issue Open"          // Event label
                                                       value:nil] build]];    // Event value
    } else if ([[notification name] isEqualToString:@"BakerIssueBuy"]) {
        [tracker send:[[GAIDictionaryBuilder createEventWithCategory:@"ui_action"     // Event category (required)
                                                      action:@"open"  // Event action (required)
                                                       label:@"Baker Issue Buy"          // Event label
                                                       value:nil] build]];    // Event value
    } else if (...) {
        // add more
    }

Using Countly can be something like:

NSDictionary * dict = [NSDictionary dictionaryWithObjectsAndKeys: @"More data", @"Baker", nil];

if ([[notification name] isEqualToString:@"BakerApplicationStart"]) {
        [[Countly sharedInstance] recordEvent:@"Baker App Open" segmentation:dict count:1];
    } else if ([[notification name] isEqualToString:@"BakerIssueDownload"]) {
        [[Countly sharedInstance] recordEvent:@"Baker Issue Download" segmentation:dict count:1];
    } else if ([[notification name] isEqualToString:@"BakerIssueOpen"]) {
        [[Countly sharedInstance] recordEvent:@"Baker Issue Open" segmentation:dict count:1];
    } else if ([[notification name] isEqualToString:@"BakerIssueBuy"]) {
        [[Countly sharedInstance] recordEvent:@"Baker Issue Buy" segmentation:dict count:1];
    } else if (...) {
        // add more
    }