我们如何在iOS应用程序转到后台时调度Google Analytics事件? [英] How do we dispatch Google Analytics events when iOS app goes to the background?

查看:141
本文介绍了我们如何在iOS应用程序转到后台时调度Google Analytics事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用程序中有Apple App Store的链接,我试图将它们作为事件进行跟踪。



问题是我们无法使用我的应用程序在GA事件进入后台之前正确分派GA事件。我们正在使用iOS SDK v2beta4。



以下是我们正在使用的代码的概述。您可以看到我们已经投入了很多我称之为保险策略的代码,因为我们认为是正确的方式并不奏效。但即使是保险政策代码并不总是在我的应用程序进入后台之前调度事件。它只适用于大约50%的时间,其余时间我必须返回到应用程序才能派发事件。



我们相信正确的方法是在applicationDidEnterBackground中派发事件,并通过beginBackgroundTaskWithExpirationHandler请求iOS额外的时间来完成此事。我们已经尝试了这个代码,但没有使用我的保险单代码。至少我相信我们正确地注释了每一行保险代码。



请注意,我们设置了全局变量UIBackgroundTaskIdentifier bgTask;在代码中的AppDelegate.h头文件中

  UIBackgroundTaskIdentifier bgTask; 

以下代码是我们认为正确的方法:

   - (void)applicationDidEnterBackground:(UIApplication *)application 
{
UIApplication * app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^ {
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
$ b 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 
{
...
//跟踪
//使用事件(按下按钮)

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];
}

最后,当您完全关闭应用时,另一个GA调度称为

(b)

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
}

这些都不是100%的时间。只有大约50%的时间。所以我们这样做了:当你重新进入应用程序(无论从后台或应用程序完全关闭都没关系),我们发送一个调度

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

最后一点,事件被分派,但只有当用户返回到我的应用程序。虽然我不是100%确定是否这个代码在我回到应用程序时调度事件,或者当我回到应用程序时调度事件。

解决方案

因为从iOS 6开始不推荐使用dispatch_get_current_queue(),所以现在可以使用以下代码:

  UIApplication * app = [UIApplication sharedApplication]; 
__block UIBackgroundTaskIdentifier bgTask;

bgTask = [app beginBackgroundTaskWithExpirationHandler:^ {
[app endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
$ b $ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {
[[GAI sharedInstance] dispatch];

double dispatchTimeout = 10.0; // 10 seconds timeout $ $ b $ dispatch_time(dispatch_get_main_queue(),^(void){
[app endBackgroundTask:bgTask]; $ b dispatch_time_t popTime = $ b bgTask = UIBackgroundTaskInvalid;
});
});


My iOS app has links to Apple's App Store in it and I am trying to track those as events.

The problem is that we can't get my app to properly dispatch the GA events before it goes into the background. We are using iOS SDK v2beta4.

Here is an overview of the code we are using. You can see we've put in a lot of what I call "insurance policy" code because what we think is the correct way is not working. But even the insurance policy code does not always dispatch the events before my app goes into the background. It only works about 50% of the time and the rest of the time I have to return to the app to get the events to dispatch.

We believe the correct way is to dispatch the event in "applicationDidEnterBackground" and to ask the iOS for extra time to do this via "beginBackgroundTaskWithExpirationHandler". We've tried this code on its own without my "insurance policy" code. At least I believe we commented out every line of insurance code correctly.

Note that we set the global variable UIBackgroundTaskIdentifier bgTask; in the AppDelegate.h header file with the code

UIBackgroundTaskIdentifier  bgTask;

Here is the code that we think is the correct way to do this:

- (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;
    });
}

The above code is what we think should work but does not. Note: The App Store is not a usual app, but a website inside an app if that makes a difference.

As an insurance policy we've done a few other things which dispatch the event about 50% of the time:

The first [[GAI sharedInstance] dispatch] is called immediately in the function where tracking has been set

Source code:

- (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];
    ...
}

We also put it in "applicationWillResignActive"

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

And finally when you completely close the app another GA dispatch is called

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

None of this works 100% of the time. Only about 50% of the time. So we did this: When you re-enter the app (it doesn't matter if from the background or the app has been completely closed) we send a dispatch

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

With this last bit the events are dispatched but only if a user returns to my app. Although I am not 100% sure if it is this code that is dispatching the events when I return to the app or a GA default dispatch when I go back to the app.

解决方案

as 'dispatch_get_current_queue()' is currently deprecated as of iOS 6, you can now use the following code:

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

这篇关于我们如何在iOS应用程序转到后台时调度Google Analytics事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆