Google analytics使用分为普通web(移动web),及app(包括移动app).ios app访问记录,需要几个库:
(libGoogleAnalytics.a,CFNetwork,libsqlite3.0.dylib)
下面是谷歌官网提供的操作步骤:
1。 Open Xcode and create a new iPhone OS project.
2。 Drag GANTracker.h and libGoogleAnalytics.a from the SDK's Library directory into your new project.
3。 Include the CFNetwork framework in your project and link against libsqlite3.0.dylib.
测试结果:
以及经测试过的源码:
//
// ViewController.h
// anyltics
//
// Created by sunkey on 12-8-21.
// Copyright (c) 2012年 sunkey. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "GANTracker.h"
static NSString* const kAnalyticsAccountId = @"UA-xxxxxxxx-1";
static const NSInteger kGANDispatchPeriodSec = 10;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[GANTracker sharedTracker] startTrackerWithAccountID:kAnalyticsAccountId
dispatchPeriod:kGANDispatchPeriodSec
delegate:nil];
NSError *error;
if (![[GANTracker sharedTracker] setCustomVariableAtIndex:1
name:@"iOS1"
value:@"iv1"
withError:&error]) {
NSLog(@"error in setCustomVariableAtIndex");
}
if (![[GANTracker sharedTracker] trackEvent:@"Application iOS"
action:@"Launch iOS"
label:@"Example iOS"
value:99
withError:&error]) {
NSLog(@"error in trackEvent");
}
if (![[GANTracker sharedTracker] trackPageview:@"/app_entry_point"
withError:&error]) {
NSLog(@"error in trackPageview");
}
NSLog(@"load\n");
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
NSLog(@"unload \n");
//[[GANTracker sharedTracker] stopTracker];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
@end
参考:Google Analytics SDK for iOS