Google Analytics在v3 SDK更新后减少了会话跟踪 [英] Google Analytics Reduced Session Tracking after v3 SDK update

查看:148
本文介绍了Google Analytics在v3 SDK更新后减少了会话跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用 Google Analytics(分析)来跟踪事件和会话

之前的分析版本是v2.x,现在我已将其版本更新为v3,并且我可以看到会话跟踪存在一个主要区别。



我研究并找到了 v2.x至v3 移转文件


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???

解决方案

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:

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

这篇关于Google Analytics在v3 SDK更新后减少了会话跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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