繁体   English   中英

Google Analytics(分析)iOS v2beta3 - 手动调度在applicationWillResignActive中不起作用

[英]Google Analytics iOS v2beta3 - manual dispatch doesn't work in applicationWillResignActive

我一直在使用谷歌的Analytics SDK v2beta3并且一切正常,但是当应用程序离开活动状态时我无法手动调度。 (fyi,对于我的应用程序,我需要预留电池电量,所以我只使用'[[GAI sharedinstance] dispatch]'来在用户完成应用程序时发送我的事件数据。)

我已经尝试了几件事,但是在跟踪调度到达并在跟踪期间运行时,它似乎没有做任何事情......没有日志输出(我打开了调试模式)并且没有上传数据。 它应该最低限度地报告“GoogleAnalytics 2.0b3 - [GAIDispatcher initiateDispatch:retryNumber:](GAIDispatcher.m:479)DEBUG:没有未决的点击量。” 或者我会想到的那种东西。 但是日志中没有任何内容,也没有发送数据。

相反,当应用程序从后台恢复时,会发送匹配,然后我会在控制台上看到所有调试语句,数据会成功发送到我的Google Analytics帐户。

以下是我的代码......

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...
    // set up Google Analytics tracker
    [GAI sharedInstance].trackUncaughtExceptions = YES; // automatically track uncaught exceptions with Google Analytics - sent with stack trace.
    [GAI sharedInstance].dispatchInterval = -1;         // set Google Analytics dispatch off, will do manually when app goes into background.
    [GAI sharedInstance].debug = YES;                   // set debug to YES for extra debugging information.
    id<GAITracker> tracker = [[GAI sharedInstance] trackerWithTrackingId:GOOGLE_ANALYTICS_TRACKING_ID_FOR_TEST];    // Create tracker instance.
    [tracker setSessionTimeout:600];                     // set min time between sessions to be 10 minutes
    [GAI sharedInstance].defaultTracker = tracker;   // reset the default tracker if needed
    [[GAI sharedInstance] setOptOut:NO];

    ...
}

 - (void)applicationWillResignActive:(UIApplication *)application
{
    UIDevice * device = [UIDevice currentDevice];
    BOOL backgroundTasksSupported = NO;

    if ([device respondsToSelector:@selector(isMultitaskingSupported)]) {
        backgroundTasksSupported = device.multitaskingSupported;
    }

    if (backgroundTasksSupported) {
        self.uploadAnalyticsBackgroundTask = [application beginBackgroundTaskWithExpirationHandler:^{[self endBackgroundUploadTask];}];

        // Start the long-running task and return immediately.
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{        // put block on Global run queue...

            [[GAI sharedInstance] dispatch];                                                    // for dispatch of collected metric/analysis data...

            [self endBackgroundUploadTask];
        });
    }
    else {
        [[GAI sharedInstance] dispatch];
    }

}

- (void) endBackgroundUploadTask
{
     if(self.uploadAnalyticsBackgroundTask != UIBackgroundTaskInvalid) {          // if background task running, end it
        [[UIApplication sharedApplication] endBackgroundTask: self.uploadAnalyticsBackgroundTask];
        self.uploadAnalyticsBackgroundTask = UIBackgroundTaskInvalid;
     }
}

该应用程序到达并执行'[[GAI sharedInstance] dispatch];' 但没有做任何事情。 当应用程序进入后台时,我是一个有背景任务的新手,所以也许我做错了什么。 但作为我的调查的一部分,我甚至简化了applicationWillResignActive(这应该是/应该阻止)...但我得到了相同的东西:没有调试信息和没有传输数据。

 - (void)applicationWillResignActive:(UIApplication *)application
{
    [[GAI sharedInstance] dispatch];
}

我已经尝试了调度间隔非负(比如15秒),并且我按照请求的间隔进行常规传输,但是手动调度的调用不起作用。

在我的代码的其他部分调用手动调度确实有效。 它似乎在应用程序关闭时不起作用。

关于我可能做错了什么以及如何解决这个问题的任何想法建议?

与之前的Google AnalyticsSDK版本不同,从v2开始,手动调度机制不会同步运行,即不等待调度完成,而是立即返回。 因此,如果在变为非活动状态之前使用dispatch,则不会发生调度,因为在实际调度发生之前应用程序的运行队列将暂停。 作为一种解决方法,我正在启动一个后台任务,该任务会休眠几秒钟,并使应用程序保持足够长的时间以完成调度。

希望Google将在SDK的未来版本中重新添加dispatchSynchronously方法。

此外,请注意,在applicationWillTerminate ,您只有5秒钟才能终止应用程序,所以不要等待太长时间。

- (void)dispatchAndWait:(NSTimeInterval)waitTime {
  UIBackgroundTaskIdentifier backgroundTask = 0;
  backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
  }];

  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // GAI dispatch runs asynchronously and dispatches on the main
    // thread, so we're giving it some time to dispatch and keep the
    // app running in background by sleeping on a background thread
    [[GAI sharedInstance] dispatch];
    [NSThread sleepForTimeInterval:waitTime];

    [[UIApplication sharedApplication] endBackgroundTask:backgroundTask];
  });
}

截至2013年8月,这是正确的答案。

只要应用程序正常关闭,即它收到UIApplicationWillResignActiveNotification ,就会在下次打开应用程序时调度命中。

您没有看到这个的原因可能是从Xcode退出应用程序并不正常关闭它。 按下主页按钮,然后不久Xcode的停止,将正常关闭它,它将工作。

这里的问题是,即使分发了匹配,如果它们在Google Analytics配置文件的时区中第二天凌晨4点之后到达,它们也会被分析丢弃。 这有点棘手,并且可能证明像安德烈亚斯这样的解决方案合理

我们有这个问题,但能够将我们的成功率提高到50%。 这是我们如何做到的。

我们输入了“保险代码”,当应用程序转换到后台时,我们的事件现在大约有50%的时间被调度。 在您返回应用程序之前,其他50%的时间都不会发送事件。

让我们达到50%的保险代码是将调度电话放在按钮代码内:

- (IBAction)goToAppStore:(id)sender
{    
    ...
    // Tracking
    // Using events (pressing on buttons)

    id <GAITracker> tracker = [[GAI sharedInstance] defaultTracker];

    [tracker sendEventWithCategory:@"App Checkout"
                        withAction:@"Checkout Button Pressed"
                        withLabel:nameApp.text
                        withValue:nil];

    [[GAI sharedInstance] dispatch];
    ...
}

另外,查看主要问题中的示例代码 - Apple声明要将代码用于执行后台任务

applicationDidEnterBackground:

代替

applicationWillResignActive:

但它似乎并没有对我们产生巨大影响。 以下是Apple关于该主题的文档(参见清单3-3)

http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

此外,如果您想更详细地了解我们如何获得大约50%的成功,我会对代码示例所做的事情提出更长的帖子: 当iOS应用程序进入后台时,我们如何发送Google Analytics事件?

在这一点上虽然我仍然想要一个解决方案,如果有人找到一个达到100%。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM