Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my iOS app's session lengths 30 min + in Google Analytics?

More importantly, how do I fix it?

It's as if backgrounding the app doesn't end the session.

like image 983
chenware Avatar asked Mar 27 '12 19:03

chenware


1 Answers

When your app goes into background mode it needs to tell the analytics to stop tracking.

Application Delegate would have something like:

-(void) applicationDidEnterBackground:(UIApplication*)application
{
[[GANTracker sharedTracker] stopTracker];
}

In google's Easy Tracker example, a view controller receives notifications when app state changes. Tracking is stopped when app goes into background (Around line 400).

if ([application applicationState] == UIApplicationStateBackground) {
    if (self.state == EasyTrackerStateForeground) {
      // Transitioned from foreground to background. Generate the app stop
      // event, and stop the tracker.
      NSLog(@"Transitioned from foreground to background.");
      NSError *error = nil;
      if (![[GANTracker sharedTracker] trackEvent:@""
                                           action:@""
                                            label:@""
                                            value:0
                                        withError:&error]) {
        NSLog(@"Error tracking foreground event: %@", error);
      }
      // TODO(fmela): make this time period a constant.
      if (![[GANTracker sharedTracker] dispatchSynchronous:2.0]) {
        NSLog(@"Synchronous dispatch on background failed!");
      }
      [[GANTracker sharedTracker] stopTracker];
    }
    self.state = EasyTrackerStateBackground;
  }
like image 114
Alex L Avatar answered Oct 06 '22 12:10

Alex L