title: How do we dispatch Google Analytics events when iOS app goes to the background?
body: <p>My iOS app has links to Apple's App Store in it and I am trying to track those as events.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Note that we set the global variable UIBackgroundTaskIdentifier  bgTask; in the AppDelegate.h header file with the code </p>
  
  <pre><code>UIBackgroundTaskIdentifier  bgTask;
  </code></pre>
  
  <p>Here is the code that we think is the correct way to do this:</p>
  
  <pre><code>- (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;
      });
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>As an insurance policy we've done a few other things which dispatch the event about 50% of the time:</p>
  
  <p>The first [[GAI sharedInstance] dispatch] is called immediately in the function where tracking has been set</p>
  
  <p>Source code: </p>
  
  <pre><code>- (IBAction)goToAppStore:(id)sender
  {    
      ...
      // Tracking
      // Using events (pressing on buttons)
  
      id &lt;GAITracker&gt; tracker = [[GAI sharedInstance] defaultTracker];
  
      [tracker sendEventWithCategory:@"App Checkout"
                          withAction:@"Checkout Button Pressed"
                          withLabel:nameApp.text
                          withValue:nil];
  
      [[GAI sharedInstance] dispatch];
      ...
  }
  </code></pre>
  
  <p>We also put it in "applicationWillResignActive"</p>
  
  <pre><code>- (void)applicationWillResignActive:(UIApplication *)application
  {
      ...  
      [[GAI sharedInstance] dispatch];
  }
  </code></pre>
  
  <p>And finally when you completely close the app another GA dispatch is called</p>
  
  <pre><code>- (void)applicationWillTerminate:(UIApplication *)application
  {
      [[GAI sharedInstance] dispatch];
  }
  </code></pre>
  
  <p>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</p>
  
  <pre><code>- (void)applicationDidBecomeActive:(UIApplication *)application
  {
      [[GAI sharedInstance] dispatch];
  }
  </code></pre>
  
  <p>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.</p>