1

I am using Google Analytics in my application to track events and sessions.

Earlier the version of analytics was v2.x and now I have updated its version to v3 and I can see there is a major difference in the session tracking.

I researched and found this v2.x to v3 migration document link:

The migration documents states that the session was automatically created in v2.x but we have to create it manually in v3. I tried using the code to manually create session on app launch but still there is much difference in Session Tracking.

static NSString const *kGaPropertyId = @"UA-XXXX-Y";
    id tracker = [[GAI sharedInstance] trackerWithTrackingId:kGaPropertyId];

    [tracker send:[[[GAIDictionaryBuilder createEventWithCategory:@"UX"
                                                           action:@"appstart"
                                                            label:nil
                                                            value:nil] set:@"start" forKey:kGAISessionControl] build]];

Anyone have any idea if there was the feature to change session timeout from admin settings before v3 came into view?

Or anything I can do to resolve this or the reason behind this???

2 Answers 2

3

I started fixing this problem after I found out that my average session time was 8 minutes, where I've got a app which plays movies and you expect a much higher average session time.

I ended up with the following implementation:

[[NSNotificationCenter defaultCenter] addObserver:[GATracking class] selector:@selector(startTrackingSession) name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:[GATracking class] selector:@selector(endTrackingSession) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:[GATracking class] selector:@selector(endTrackingSession) name:UIApplicationWillTerminateNotification object:nil];

These observers handle the start and end of a session. A session will start when the application gets active and stops if the application goes to background or gets killed.

Here's the code for starting and ending the tracking session. The ending takes place inside a background task, to make sure the ending call gets dispatched to Google Analytics before going into inactive state. Otherwise it would be schedules for the next startup.

+ (void)startTrackingSession
{

    [GAI sharedInstance].dispatchInterval = 20;

    // Initialize tracker.
    id tracker = [[GAI sharedInstance] defaultTracker];

    [tracker send:[[[GAIDictionaryBuilder createEventWithCategory:@"application_events"
                                                           action:@"application_session_start"
                                                            label:nil
                                                            value:nil] set:@"start" forKey:kGAISessionControl] build]];

    // Set this after the session start has been sent. Only needs to be set once but you must    be sure that two starts are not sent in a row or you will end up with 0:00:00 sessions.
    [tracker set:kGAISessionControl
           value:nil];

    [[GAI sharedInstance] dispatch];
}

+ (void)endTrackingSession
{
    id tracker = [[GAI sharedInstance] defaultTracker];

    // Call when the session ends.
    [tracker send:[[[GAIDictionaryBuilder createEventWithCategory:@"application_events"
                                                           action:@"application_session_end"
                                                            label:nil
                                                            value:nil] set:@"end" forKey:kGAISessionControl] build]];

    [tracker set:kGAISessionControl
           value:nil];

    [self dispatchUsingBackgroundTask];
}

+ (void)dispatchUsingBackgroundTask
{
    // As the end tracking session gets called when entering background, run it in a background task to make sure it gets dispatched
    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;
        });
    });
}

Important part if you still see tracking session with a max of 30 minutes, change this setting in the admin section of your property: Changing the session timeout property to a max of 4 hours to make sure your session length can get bigger then 30 minutes

If you don't see this settings, you're probably not using Universal Analytics. To find out if you do, checkout this link: https://support.google.com/analytics/answer/3450662?hl=en

-2

Had a very same problem. Ended up with creating the new app property on Google Analytics. With new property, everything works like before. Good Luck!

6
  • What changes to be made in this new property? And will it create a new tracking-id?
    – AtWork
    Commented Aug 13, 2014 at 13:05
  • that is the new tracking id for the app :) Commented Aug 13, 2014 at 13:14
  • But why is it making difference in sessions as I just updated the SDK.
    – AtWork
    Commented Aug 13, 2014 at 13:17
  • I don't really know, I have tried to make it work many hours, then I just created new tracking id, and it works now. Commented Aug 14, 2014 at 5:18
  • But that is not the ideal solution and will not work for me. Any other idea I can research on?
    – AtWork
    Commented Aug 14, 2014 at 5:35

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.