Google Analytics librairie for Iphone and iOS

Google analytics librairie was not build for iOS4 so it does not build for simulator target even if it works fine on real devices. So here is a tip to make you project build/link on simulator target with GAN :

1 – execute the following command :

lipo libGoogleAnalytics.a -remove i386 -output libGoogleAnalytics2.a

2 – Remove libGoogleAnalytics.a from you project and add libGoogleAnalytics2.a

3 – Use the folowing code to track your events :

#import <Foundation/Foundation.h>
 
 
@interface MYGanTracker : NSObject
{
}
+ (void)initializeGoogleAnalyticsWithAccountID:(NSString *)accountID;
+ (void)stopTracker;
+ (void)trackPageView:(NSString *)path;
+ (void)trackEvent:(NSString *)event action:(NSString *)action label:(NSString *)label;
 
@end
 
 
#import "MYGanTracker.h"
 
#if !(TARGET_IPHONE_SIMULATOR)
#import "GANTracker.h"
#endif
 
// Dispatch period in seconds
static const NSInteger kGANDispatchPeriodSec = 30;
 
@implementation MYGanTracker
 
+ (void)initializeGoogleAnalyticsWithAccountID:(NSString *)accountID {
#if !(TARGET_IPHONE_SIMULATOR)
    [[GANTracker sharedTracker] startTrackerWithAccountID:accountID dispatchPeriod:kGANDispatchPeriodSec delegate:nil];
#endif
}
 
+ (void)stopTracker {
#if !(TARGET_IPHONE_SIMULATOR)	
	[[GANTracker sharedTracker] dispatch];
	[[GANTracker sharedTracker] stopTracker];
#endif
}
 
+ (void)trackPageView:(NSString *)path {
#if !(TARGET_IPHONE_SIMULATOR)
	NSError *error;
	if (![[GANTracker sharedTracker] trackPageview:path withError:&error]) {
		NSLog(@"[ERROR GAN] Can not track. Error Code : %d", error.code);
	}
#endif
}
 
+ (void)trackEvent:(NSString *)event action:(NSString *)action label:(NSString *)label {
#if !(TARGET_IPHONE_SIMULATOR)
	NSError *error;
	if (![[GANTracker sharedTracker] trackEvent:event action:action label:label value:0 withError:&error]) {
		NSLog(@"[ERROR GAN] Can not track. Error Code : %d", error.code);
	}
#endif
}
 
@end

4 – Build for simulator, it should work…

Leave a Reply