AppDelegate.h File
#import <UIKit/UIKit.h>
#import "GAI.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
}
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic, strong) id<GAITracker> tracker;
@end
AppDelegate.m File
#import "AppDelegate.h"
//Set your tracking ID here
static NSString *const kTrackingId = @"UA-XXXXXXXX-X”;
static NSString *const kAllowTracking = @"allowTracking";
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSDictionary *appDefaults = @{kAllowTracking: @(YES)};
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
// User must be able to opt out of tracking
[GAI sharedInstance].optOut =
![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];
// Initialise Google Analytics with a 120-second dispatch interval. There is a
// tradeoff between battery usage and timely dispatch.
[GAI sharedInstance].dispatchInterval = 120;
[GAI sharedInstance].trackUncaughtExceptions = YES;
self.tracker = [[GAI sharedInstance] trackerWithName:@"AppName"
trackingId:kTrackingId];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[GAI sharedInstance].optOut = ![[NSUserDefaults standardUserDefaults] boolForKey:kAllowTracking];
}
ViewController.h File
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface ViewController : GAITrackedViewController
{
AppDelegate *appDelegate;
}
@end
ViewController.m File
#import "ViewController.h"
#import "GAI.h"
#import "GAIDictionaryBuilder.h"
@interface ViewController ()
@end
@implementation ViewController
//Track the Pages
- (void)viewDidLoad
{
appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
self.screenName = @“Home Screen";
}
/**** or ****/
-(void)viewWillAppear:(BOOL)animated
{
appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
self.screenName = @“Home Screen";
}
//Track the Button Click Event
- (IBAction)loginButtonClicked:(id)sender
{
NSMutableDictionary *event =
[[GAIDictionaryBuilder createEventWithCategory:@"Login Submit"
action:@"Success"
label:nil
value:nil] build];
[[GAI sharedInstance].defaultTracker send:event];
[[GAI sharedInstance] dispatch];
}
No comments:
Post a Comment