Import Google Analytics In your application and how to track the Screenname and Events.
Add app’s libraries:
APP--->Build Phases--->Link with Library--->Add Project
- libGoogleAnalyticsServices.a
- CoreData.framework
- SystemConfiguration.framework
- libz.tbd
Add the GA SDK (not the pod file):
- GAI.h
- GAIDictionaryBuilder.h
- GAIEcommerceProduct.h
- GAIEcommerceProductAction.h
- GAIEcommercePromotion.h
- GAIFields.h
- GAILogger.h
- GAITrackedViewController.h
- GAITracker
- You need to configure —>“GoogleService-Info.plist”
After add all the library and files.Add this
// AppDelegate.h
#import "GAI.h"
@property(nonatomic, strong) id<GAITracker> tracker;
// AppDelegate.m
/******* Set your tracking ID here *******/
static NSString *const kTrackingId = @“Your TrackinG Id”;
static NSString *const kAllowTracking = @"allowTracking";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[self setGoogleAnalytics];
}
-(void) setGoogleAnalytics{
// Initialize tracker.
self.tracker =[[GAI sharedInstance] trackerWithName:@"Find the sign"trackingId:kTrackingId];
NSDictionary *appDefaults = @{kAllowTracking: @(YES)};
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// User must be able to opt out of tracking
[GAI sharedInstance].optOut =
![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];
// Optional: automatically send uncaught exceptions to Google Analytics.
[GAI sharedInstance].trackUncaughtExceptions = YES;
// Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
[GAI sharedInstance].dispatchInterval = 20;
// Optional: set Logger to VERBOSE for debug information.
[[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelVerbose];
[[GAI sharedInstance] setTrackUncaughtExceptions:YES];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[GAI sharedInstance].optOut = ![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];
}
In your ViewController.h
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
#import "AppDelegate.h"
@interface UIViewController : ViewController
Change ViewController ——> GAITrackedViewController
In your ViewController.m
@interface UIViewController ()
{
AppDelegate *appDelegate;
}
For screenName
-(void)viewWillAppear:(BOOL)animated
{
appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
self.screenName = @"i_Splash Screen";
}
If any error on screenName ..use like this..
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
@try {
[self.tracker set:kGAIScreenName value:self.screenName];
[self.tracker send:[[GAIDictionaryBuilder createScreenView] build]];
}
@catch (NSException *exception) {
NSLog(@"[ERROR] in Automatic screen tracking: %@", exception.description);
}
}
Event Track with screenName
-(void)viewWillAppear:(BOOL)animated
{
self.screenName=@"Your ScreenName";
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker send:[[GAIDictionaryBuilder createEventWithCategory:Your category(NSSTRING)
action:self.screenName
label:nil
value:nil] build]];
}
Comments
Post a Comment