当我的应用进入后台时,如何停止Google分析跟踪会话?

时间:2014-10-30 12:32:59

标签: ios objective-c iphone xcode google-analytics

将Google Analytics v3用于我的iOS应用。

当我的应用程序进入后台时,我尝试手动停止当前会话,并在我的应用程序进入前台时开始新会话。

但它失败了。

这是我的代码:

在Appdelegate.m

@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary       *)launchOptions

{
    [GAI sharedInstance].trackUncaughtExceptions = YES;
    [GAI sharedInstance].dispatchInterval = 20;
    [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelError];
    [[GAI sharedInstance] trackerWithTrackingId:@"UA-54600000-1"];

    NSLog(@"Analytics Started");
}

并在

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  //  Here
  //  I WANT TO CLOSE THE CURRENT SESSION  
}

并在

- (void)applicationWillEnterForeground:(UIApplication *)application
{
   //   HERE
   //   I WANT TO START A NEW SESSION
}

1 个答案:

答案 0 :(得分:0)

您可以启用应用级别的退出标记,该标记会在整个应用中停用Google Analytics。设置完成后,该标志将持续应用程序的生命周期或重置。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
 //  Here
 // THE CURRENT SESSION  

  [[GAI sharedInstance].setOptOut = YES];
  }


- (void)applicationWillEnterForeground:(UIApplication *)application
{
//   HERE
//    START A NEW SESSION

// Get a new tracker.
id newTracker = [[GAI sharedInstance]trackerWithTrackingId:@"UA-NEW-TRACKING-ID");

// Set the new tracker as the default tracker, globally.
[GAI sharedInstance].defaultTracker = newTracker;
}

需要更多知识才能使用此google analytics link

其他信息有一个很好的tutorial

相关问题