Google Analytics Reduced Session Tracking after v3 SDK update

后端 未结 2 1309
隐瞒了意图╮
隐瞒了意图╮ 2020-12-12 04:48

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

Earlier the version of analytics was v2.x and now I ha

相关标签:
2条回答
  • 2020-12-12 04:56

    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!

    0 讨论(0)
  • 2020-12-12 05:13

    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

    0 讨论(0)
提交回复
热议问题