Skip to content

Instantly share code, notes, and snippets.

@jancassio
Last active August 29, 2015 14:01
Show Gist options
  • Save jancassio/f8ffda0460c6bc44d3d3 to your computer and use it in GitHub Desktop.
Save jancassio/f8ffda0460c6bc44d3d3 to your computer and use it in GitHub Desktop.
A very simple GAI + Flurry tracker.

#iOS Google Analytics and Flurry Tracker

This is a very simple helper track GAI and Flurry screen views and events quickly. Of course, you can customize based on marketing plan to increase tracking precision.

Starting tracker

Before use the Tracker, you need to initialize then, the best place to do this is in your AppDelegate file, at application:didFinishLaunchingWithOptions:launchOptions callback.

[[Tracker shared] start];

Tracking screen views

// To track screen, use the snippet below at "viewDidAppear" callback of your View Controller.
[[Tracker shared] trackScreenView:@"MyScreenName"];

Tracking user/system events

// To track events, use the example below
[[Tracker shared] trackActionInScreenWithName:@"Screen Name" category"Category" action:@"Action" label:@"Label" value:0];
#import <Foundation/Foundation.h>
@interface Tracker : NSObject
+ (Tracker *)shared;
- (void)startTracker;
- (void)trackScreen:(NSString *)screen;
- (void)trackActionInScreenWithName:(NSString *)screenName
category:(NSString *)categoryName
action:(NSString *)actionName
label:(NSString *)label
value:(NSNumbe *)value;
@end
#import "Tracker.h"
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
#import "GAIFields.h"
#import "Flurry.h"
@interface Tracker ()
{
id<GAITracker> googleTracker;
}
@end
@implementation Tracker
+ (Tracker *)shared
{
static dispatch_once_t onceToken;
__strong static Tracker *_shared = nil;
dispatch_once(&onceToken, ^{
_shared = [Tracker new];
});
return _shared;
}
#pragma mark - Tracker engine initialization
- (void)startTracker
{
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 120.f;
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelError];
googleTracker = [[GAI sharedInstance] trackerWithName:TrackerName trackingId:@"UA-XXXXXXXX"];
[googleTracker set:kGAISampleRate value:@"50.0"];
[Flurry setCrashReportingEnabled:YES];
[Flurry startSession:@"XXXXXXXXXXXXXXXXXXXX"];
}
- (void)trackScreen:(TrackerScreen)screen
{
[googleTracker set:kGAIScreenName value:screenName];
[googleTracker send:[[GAIDictionaryBuilder createAppView] build]];
[Flurry logEvent:@"Screen View" withParameters:@{@"Screen":screenName}];
}
- (void)trackActionInScreenWithName:(TrackerScreen)screenName
category:(TrackerCategoryType)categoryName
action:(TrackerActionType)actionName
label:(NSString *)label
value:(NSNumber *)value;
{
[googleTracker set:kGAIScreenName value:screenName];
[googleTracker send:[[GAIDictionaryBuilder createEventWithCategory:categoryName action:actionName label:label value:value] build]];
[googleTracker set:kGAIScreenName value:nil];
// Here you can customize your params based on your marketing plan.
NSDictionary *params = @{
@"trigger":actionName,
@"parameters":label,
@"Screen":screenName
};
[Flurry logEvent:categoryName withParameters:params];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment