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

时间:2012-12-14 22:33:08

标签: ios google-analytics dispatch

我一直在使用Google的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秒),并且我按照请求的间隔进行常规传输,但是手动调度的调用不起作用。

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

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

3 个答案:

答案 0 :(得分:2)

与之前的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];
  });
}

答案 1 :(得分:1)

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

只要应用程序正常关闭,即收到UIApplicationWillResignActiveNotification,就会在下次打开应用程序时发送匹配。

你没有看到这个的原因可能是从Xcode退出应用程序并不能正常关闭它。

按下主页按钮,然后按Xcode停止,将正常关闭它。

这里的问题是,即使发送了匹配,如果它们在Google Analytics配置文件的时区中第二天凌晨4点之后到达,它们也会被分析丢弃。这有点棘手,可能证明解决方案like Andreas's

答案 2 :(得分:0)

我们有这个问题,但能够将我们的成功率提高到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%的成功,我会对我们对代码示例所做的工作提出更长的帖子:How do we dispatch Google Analytics events when iOS app goes to the background?

此时虽然我仍然希望解决方案达到100%,如果有人找到了。