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

时间:2022-08-21 15:19:12

Using Google analytics v3 for my iOS app.

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

I tried to manually stop the current session when my app enters background and start a new session when my App enters foreground.

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

But it fails.

但它失败了。

Here is my code :

这是我的代码:

In 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");
}

and in

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

and in

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

1 个解决方案

#1


0  

You can enable an app-level opt out flag that will disable Google Analytics across the entire app. Once set, the flag will persist for the life of the app or until it is reset.

您可以启用应用级别的退出标记,以便在整个应用中停用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;
}

need ref on more knowledge use this google analytics link

需要参考更多知识使用此谷歌分析链接

additional information have a nice tutorial

其他信息有一个很好的教程

#1


0  

You can enable an app-level opt out flag that will disable Google Analytics across the entire app. Once set, the flag will persist for the life of the app or until it is reset.

您可以启用应用级别的退出标记,以便在整个应用中停用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;
}

need ref on more knowledge use this google analytics link

需要参考更多知识使用此谷歌分析链接

additional information have a nice tutorial

其他信息有一个很好的教程