Skip to content

Commit

Permalink
Added Google Analytics library and USHAnalytics
Browse files Browse the repository at this point in the history
  • Loading branch information
isaaclimdc committed Apr 23, 2014
1 parent c32ec0e commit 05751e0
Show file tree
Hide file tree
Showing 12 changed files with 706 additions and 1 deletion.
42 changes: 42 additions & 0 deletions App/App.xcodeproj/project.pbxproj
Expand Up @@ -1972,6 +1972,9 @@
3EFF5B021620E64B0072FAE1 /* verified@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 3EFF5AFE1620E64B0072FAE1 /* verified@2x.png */; };
3EFF5B051620E9740072FAE1 /* locate.png in Resources */ = {isa = PBXBuildFile; fileRef = 3EFF5B031620E9740072FAE1 /* locate.png */; };
3EFF5B061620E9740072FAE1 /* locate@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 3EFF5B041620E9740072FAE1 /* locate@2x.png */; };
6A49AF0F1907247D005CD313 /* libGoogleAnalyticsServices.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A49AF0E1907247D005CD313 /* libGoogleAnalyticsServices.a */; };
6A49AF1219072491005CD313 /* USHAnalytics.m in Sources */ = {isa = PBXBuildFile; fileRef = 6A49AF1119072491005CD313 /* USHAnalytics.m */; };
6A49AF14190725B3005CD313 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A49AF13190725B3005CD313 /* libz.dylib */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -2392,16 +2395,28 @@
3EFF5AFE1620E64B0072FAE1 /* verified@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "verified@2x.png"; sourceTree = "<group>"; };
3EFF5B031620E9740072FAE1 /* locate.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = locate.png; sourceTree = "<group>"; };
3EFF5B041620E9740072FAE1 /* locate@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "locate@2x.png"; sourceTree = "<group>"; };
6A49AF081907247D005CD313 /* GAI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GAI.h; sourceTree = "<group>"; };
6A49AF091907247D005CD313 /* GAIDictionaryBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GAIDictionaryBuilder.h; sourceTree = "<group>"; };
6A49AF0A1907247D005CD313 /* GAIFields.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GAIFields.h; sourceTree = "<group>"; };
6A49AF0B1907247D005CD313 /* GAILogger.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GAILogger.h; sourceTree = "<group>"; };
6A49AF0C1907247D005CD313 /* GAITrackedViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GAITrackedViewController.h; sourceTree = "<group>"; };
6A49AF0D1907247D005CD313 /* GAITracker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GAITracker.h; sourceTree = "<group>"; };
6A49AF0E1907247D005CD313 /* libGoogleAnalyticsServices.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libGoogleAnalyticsServices.a; sourceTree = "<group>"; };
6A49AF1019072491005CD313 /* USHAnalytics.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = USHAnalytics.h; sourceTree = "<group>"; };
6A49AF1119072491005CD313 /* USHAnalytics.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = USHAnalytics.m; sourceTree = "<group>"; };
6A49AF13190725B3005CD313 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = usr/lib/libz.dylib; sourceTree = SDKROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
3E6D031F159B54CF00CDB5B7 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
6A49AF14190725B3005CD313 /* libz.dylib in Frameworks */,
3E5CA9F7164710430065AF67 /* libsqlite3.0.dylib in Frameworks */,
3E9E4DA4167005DC0095F4A4 /* FacebookSDK.framework in Frameworks */,
3E46F624163DC48200C24119 /* Twitter.framework in Frameworks */,
6A49AF0F1907247D005CD313 /* libGoogleAnalyticsServices.a in Frameworks */,
3E951EBC163C913B00D681BF /* Accounts.framework in Frameworks */,
3E5CA9F51647102E0065AF67 /* AdSupport.framework in Frameworks */,
3E5CA9F3164710280065AF67 /* Social.framework in Frameworks */,
Expand Down Expand Up @@ -2585,6 +2600,8 @@
children = (
3E2BFE51163DD3CC00445A29 /* USHSettings.h */,
3E2BFE52163DD3CC00445A29 /* USHSettings.m */,
6A49AF1019072491005CD313 /* USHAnalytics.h */,
6A49AF1119072491005CD313 /* USHAnalytics.m */,
);
path = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -2675,6 +2692,7 @@
3E6D0325159B54CF00CDB5B7 /* Frameworks */ = {
isa = PBXGroup;
children = (
6A49AF13190725B3005CD313 /* libz.dylib */,
3E5CA9F6164710430065AF67 /* libsqlite3.0.dylib */,
3EA29937159CF51B00AC6AF2 /* Ushahidi.framework */,
3E9E4DA3167005DC0095F4A4 /* FacebookSDK.framework */,
Expand Down Expand Up @@ -2885,6 +2903,7 @@
3E6D0341159B553A00CDB5B7 /* Libraries */ = {
isa = PBXGroup;
children = (
6A49AF071907247D005CD313 /* GAnalytics */,
);
path = Libraries;
sourceTree = "<group>";
Expand Down Expand Up @@ -3300,6 +3319,20 @@
path = "zh-Hant-TW.lproj";
sourceTree = "<group>";
};
6A49AF071907247D005CD313 /* GAnalytics */ = {
isa = PBXGroup;
children = (
6A49AF081907247D005CD313 /* GAI.h */,
6A49AF091907247D005CD313 /* GAIDictionaryBuilder.h */,
6A49AF0A1907247D005CD313 /* GAIFields.h */,
6A49AF0B1907247D005CD313 /* GAILogger.h */,
6A49AF0C1907247D005CD313 /* GAITrackedViewController.h */,
6A49AF0D1907247D005CD313 /* GAITracker.h */,
6A49AF0E1907247D005CD313 /* libGoogleAnalyticsServices.a */,
);
path = GAnalytics;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -5157,6 +5190,7 @@
3E9BABC1164826B7006ED993 /* USHMapAddViewController.m in Sources */,
3E9BABDA1648A91C006ED993 /* USHLocationAddViewController.m in Sources */,
3E9A5F101649903F00FF10C1 /* USHSwitchTableCell.m in Sources */,
6A49AF1219072491005CD313 /* USHAnalytics.m in Sources */,
3E0F3A9E16499EF3004BA80F /* USHSliderTableCell.m in Sources */,
3E9E4D5A166EACDF0095F4A4 /* USHFilterViewController.m in Sources */,
3E01346016723DB000C40CB4 /* USHImageViewController.m in Sources */,
Expand Down Expand Up @@ -5610,6 +5644,10 @@
GCC_PREFIX_HEADER = "Other/App-Prefix.pch";
INFOPLIST_FILE = Themes/Ushahidi/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Libraries/GAnalytics",
);
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "126E50AF-E31D-43EE-8A6D-C82AE5DDA65F";
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "126E50AF-E31D-43EE-8A6D-C82AE5DDA65F";
Expand All @@ -5633,6 +5671,10 @@
GCC_PREFIX_HEADER = "Other/App-Prefix.pch";
INFOPLIST_FILE = Themes/Ushahidi/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 6.0;
LIBRARY_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Libraries/GAnalytics",
);
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "9D31B280-3B15-47B7-BBDE-FEF3FB29D20A";
"PROVISIONING_PROFILE[sdk=iphoneos*]" = "9D31B280-3B15-47B7-BBDE-FEF3FB29D20A";
Expand Down
5 changes: 4 additions & 1 deletion App/Classes/Controllers/USHMapTableViewController.m
Expand Up @@ -30,8 +30,9 @@
#import <Ushahidi/USHDevice.h>
#import <Ushahidi/UITableView+USH.h>
#import <Ushahidi/NSString+USH.h>
#import "USHSettings.h"
#import <UShahidi/USHRefreshButtonItem.h>
#import "USHSettings.h"
#import "USHAnalytics.h"

@interface USHMapTableViewController ()

Expand Down Expand Up @@ -169,6 +170,8 @@ - (void)viewDidLoad {
[self adjustToolBarHeight];
[self showRefreshControl];
[self initialSyncIfNeeded];

[USHAnalytics sendScreenView:USHAnalyticsMapTableVCName];
}

- (void) viewWillAppear:(BOOL)animated {
Expand Down
11 changes: 11 additions & 0 deletions App/Classes/Delegates/USHAppDelegateMain.m
Expand Up @@ -35,6 +35,8 @@
#import <Ushahidi/UIAlertView+USH.h>
#import <Ushahidi/UIBarButtonItem+USH.h>
#import "USHSettings.h"
#import "GAI.h"
#import "USHAnalytics.h"

@interface USHAppDelegateMain ()

Expand Down Expand Up @@ -99,10 +101,19 @@ - (void) showUserAgreement {
}
}

- (void) setupGoogleAnalytics {
[GAI sharedInstance].trackUncaughtExceptions = YES;
[GAI sharedInstance].dispatchInterval = 20;
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
[[GAI sharedInstance] trackerWithTrackingId:USHAnalyticsTrackingID];
}

#pragma mark - UIApplication


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self setupGoogleAnalytics];

//#################### STYLING ####################
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewDidLoad:) name:USHViewDidLoad object:nil];
//#################### USHAHIDI ####################
Expand Down
19 changes: 19 additions & 0 deletions App/Classes/Models/USHAnalytics.h
@@ -0,0 +1,19 @@
//
// USHAnalytics.h
// App
//
// Created by Isaac Lim on 4/22/14.
// Copyright (c) 2014 Ushahidi. All rights reserved.
//

#import <Foundation/Foundation.h>

#define USHAnalyticsTrackingID @"UA-42721355-4"

NSString * const USHAnalyticsMapTableVCName;

@interface USHAnalytics : NSObject

+ (void)sendScreenView:(NSString *)screenName;

@end
24 changes: 24 additions & 0 deletions App/Classes/Models/USHAnalytics.m
@@ -0,0 +1,24 @@
//
// USHAnalytics.m
// App
//
// Created by Isaac Lim on 4/22/14.
// Copyright (c) 2014 Ushahidi. All rights reserved.
//

#import "USHAnalytics.h"
#import "GAI.h"
#import "GAIFields.h"
#import "GAIDictionaryBuilder.h"

NSString * const USHAnalyticsMapTableVCName = @"Map Table View Controller";

@implementation USHAnalytics

+ (void) sendScreenView:(NSString *)screenName {
id tracker = [[GAI sharedInstance] defaultTracker];
[tracker set:kGAIScreenName value:screenName];
[tracker send:[[GAIDictionaryBuilder createAppView] build]];
}

@end
166 changes: 166 additions & 0 deletions App/Libraries/GAnalytics/GAI.h
@@ -0,0 +1,166 @@
/*!
@header GAI.h
@abstract Google Analytics iOS SDK Header
@version 3.0
@copyright Copyright 2013 Google Inc. All rights reserved.
*/

#import <Foundation/Foundation.h>
#import "GAILogger.h"
#import "GAITracker.h"
#import "GAITrackedViewController.h"

/*! Google Analytics product string. */
extern NSString *const kGAIProduct;

/*! Google Analytics version string. */
extern NSString *const kGAIVersion;

/*!
NSError objects returned by the Google Analytics SDK may have this error domain
to indicate that the error originated in the Google Analytics SDK.
*/
extern NSString *const kGAIErrorDomain;

/*! Google Analytics error codes. */
typedef enum {
// This error code indicates that there was no error. Never used.
kGAINoError = 0,

// This error code indicates that there was a database-related error.
kGAIDatabaseError,

// This error code indicates that there was a network-related error.
kGAINetworkError,
} GAIErrorCode;

/*!
Google Analytics iOS top-level class. Provides facilities to create trackers
and set behaviorial flags.
*/
@interface GAI : NSObject

/*!
For convenience, this class exposes a default tracker instance.
This is initialized to `nil` and will be set to the first tracker that is
instantiated in trackerWithTrackingId:. It may be overridden as desired.
The GAITrackedViewController class will, by default, use this tracker instance.
*/
@property(nonatomic, assign) id<GAITracker> defaultTracker;

/*!
The GAILogger to use.
*/
@property(nonatomic, retain) id<GAILogger> logger;

/*!
When this is true, no tracking information will be gathered; tracking calls
will effectively become no-ops. When set to true, all tracking information that
has not yet been submitted. The value of this flag will be persisted
automatically by the SDK. Developers can optionally use this flag to implement
an opt-out setting in the app to allows users to opt out of Google Analytics
tracking.
This is set to `NO` the first time the Google Analytics SDK is used on a
device, and is persisted thereafter.
*/
@property(nonatomic, assign) BOOL optOut;

/*!
If this value is positive, tracking information will be automatically
dispatched every dispatchInterval seconds. Otherwise, tracking information must
be sent manually by calling dispatch.
By default, this is set to `120`, which indicates tracking information should
be dispatched automatically every 120 seconds.
*/
@property(nonatomic, assign) NSTimeInterval dispatchInterval;

/*!
When set to true, the SDK will record the currently registered uncaught
exception handler, and then register an uncaught exception handler which tracks
the exceptions that occurred using defaultTracker. If defaultTracker is not
`nil`, this function will track the exception on the tracker and attempt to
dispatch any outstanding tracking information for 5 seconds. It will then call
the previously registered exception handler, if any. When set back to false,
the previously registered uncaught exception handler will be restored.
*/
@property(nonatomic, assign) BOOL trackUncaughtExceptions;

/*!
When this is 'YES', no tracking information will be sent. Defaults to 'NO'.
*/
@property(nonatomic, assign) BOOL dryRun;

/*! Get the shared instance of the Google Analytics for iOS class. */
+ (GAI *)sharedInstance;

/*!
Creates or retrieves a GAITracker implementation with the specified name and
tracking ID. If the tracker for the specified name does not already exist, then
it will be created and returned; otherwise, the existing tracker will be
returned. If the existing tracker for the respective name has a different
tracking ID, that tracking ID is not changed by this method. If defaultTracker
is not set, it will be set to the tracker instance returned here.
@param name The name of this tracker. Must not be `nil` or empty.
@param trackingID The tracking ID to use for this tracker. It should be of
the form `UA-xxxxx-y`.
@return A GAITracker associated with the specified name. The tracker
can be used to send tracking data to Google Analytics. The first time this
method is called with a particular name, the tracker for that name will be
returned, and subsequent calls with the same name will return the same
instance. It is not necessary to retain the tracker because the tracker will be
retained internally by the library.
If an error occurs or the name is not valid, this method will return
`nil`.
*/
- (id<GAITracker>)trackerWithName:(NSString *)name
trackingId:(NSString *)trackingId;

/*!
Creates or retrieves a GAITracker implementation with name equal to
the specified tracking ID. If the tracker for the respective name does not
already exist, it is created, has it's tracking ID set to |trackingId|,
and is returned; otherwise, the existing tracker is returned. If the existing
tracker for the respective name has a different tracking ID, that tracking ID
is not changed by this method. If defaultTracker is not set, it is set to the
tracker instance returned here.
@param trackingID The tracking ID to use for this tracker. It should be of
the form `UA-xxxxx-y`. The name of the tracker will be the same as trackingID.
@return A GAITracker associated with the specified trackingID. The tracker
can be used to send tracking data to Google Analytics. The first time this
method is called with a particular trackingID, the tracker for the respective
name will be returned, and subsequent calls with the same trackingID
will return the same instance. It is not necessary to retain the tracker
because the tracker will be retained internally by the library.
If an error occurs or the trackingId is not valid, this method will return
`nil`.
*/
- (id<GAITracker>)trackerWithTrackingId:(NSString *)trackingId;

/*!
Remove a tracker from the trackers dictionary. If it is the default tracker,
clears the default tracker as well.
@param name The name of the tracker.
*/
- (void)removeTrackerByName:(NSString *)name;

/*!
Dispatches any pending tracking information.
It would be wise to call this when application is exiting to initiate the
submission of any unsubmitted tracking information. Note that this does not
have any effect on dispatchInterval, and can be used in conjuntion with
periodic dispatch. */
- (void)dispatch;

@end

0 comments on commit 05751e0

Please sign in to comment.