我们如何在iOS应用程序转到后台时调度Google Analytics事件?
我的iOS应用程序有链接到苹果的App Store中,我试图跟踪这些事件。
问题是,我们无法让我的应用程序在进入后台之前正确地分配GA事件。 我们正在使用iOS SDK v2beta4。
这里是我们正在使用的代码的概述。 你可以看到我们已经把很多我称之为“保险单”的代码,因为我们认为是正确的方式是行不通的。 但即使是保险政策代码,也不会在我的应用程序进入后台之前始终调度事件。 它只能工作大约50%的时间,其余的时间我不得不返回到应用程序来获取事件发送。
我们相信正确的方法是在“applicationDidEnterBackground”中派发事件,并通过“beginBackgroundTaskWithExpirationHandler”请求iOS额外的时间来完成此事。 我们已经尝试了这个代码,没有我的“保险单”代码。 至less我相信我们正确地评论了每一行保险代码。
请注意,我们设置全局variablesUIBackgroundTaskIdentifier bgTask; 在代码的AppDelegate.h头文件中
UIBackgroundTaskIdentifier bgTask;
这里是我们认为是正确的方法来做到这一点的代码:
- (void)applicationDidEnterBackground:(UIApplication *)application { UIApplication *app = [UIApplication sharedApplication]; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[GAI sharedInstance] dispatch]; [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); }
上面的代码是我们认为应该工作,但没有。 注意:App Store不是一个普通的应用程序,而是一个应用程序中的网站,如果这有所作为。
作为一项保单,我们已经做了一些其他事情约50%的时间派发事件:
第一个[[GAI sharedInstance]调度]在已设置跟踪的函数中立即调用
源代码:
- (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]; ... }
我们也把它放在“applicationWillResignActive”
- (void)applicationWillResignActive:(UIApplication *)application { ... [[GAI sharedInstance] dispatch]; }
最后,当你完全closures应用程序时,会调用另一个GA调度
- (void)applicationWillTerminate:(UIApplication *)application { [[GAI sharedInstance] dispatch]; }
这一切都不是100%的时间。 只有大约50%的时间。 所以我们这样做:当你重新进入应用程序(无论从后台或应用程序已经完全closures无所谓),我们发送一个调度
- (void)applicationDidBecomeActive:(UIApplication *)application { [[GAI sharedInstance] dispatch]; }
有了这最后一点事件分派,但只有当用户返回到我的应用程序。 虽然我不是100%确定是否这是代码,当我回到应用程序时,我返回到应用程序或GA默认调度派发的事件。
使用[[GANTracker sharedTracker] dispatchSynchronous:]
dispatch
方法将执行asynchronous操作。 因此,它将立即返回而不用等待调度操作完成。 您的应用程序可能在实际调度操作完成之前暂停。
– 编辑 –
它看起来像dispatchSynchronous:
方法已经在最新版本的Google Analytics库中消失了。 由于GANTrackerDelegate
也消失了,就我所知,没有办法查明调度操作何时结束。 因此,我build议在预定义的超时后调用endBackgroundTask:
方法。 这并不完美,但比dispatch
后立即调用要好。
你的代码应该是这样的:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[GAI sharedInstance] dispatch]; double dispatchTimeout = 10.0; // 10 seconds timeout dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(dispatchTimeout * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_current_queue(), ^(void){ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); });
因为从iOS 6开始,“dispatch_get_current_queue()”目前已被弃用,所以现在可以使用下面的代码:
UIApplication *app = [UIApplication sharedApplication]; __block UIBackgroundTaskIdentifier bgTask; bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [[GAI sharedInstance] dispatch]; double dispatchTimeout = 10.0; // 10 seconds timeout dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(dispatchTimeout * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }); });
在GoogleAnalytics 3.14中有dispatchWithCompletionHandler:
我正在使用它:
//track any event here as usual and then call: GAI.sharedInstance().dispatchWithCompletionHandler({ (_) -> Void in UIApplication.sharedApplication().openURL(URL) })
它似乎正常工作。