如何在Google Analytics for v4 for Android中手动结束会话?

时间:2014-12-01 08:53:45

标签: android google-analytics google-analytics-v4

我已经在网上搜索了几天,但我找不到它。它是available in v3但是在v4中手动管理会话的唯一代码仅适用于starting a new one,并且可以依赖会话超时来结束。

1 个答案:

答案 0 :(得分:3)

公共Android v4 api不支持结束会话,但您可以使用Measurement Protocol中的session control end parameter完成相同操作。您可以通过向跟踪器添加sc = end来实现此目的:

analytics = GoogleAnalytics.getInstance(getApplicationContext());

// TODO: Remove this line before shipping
analytics.getLogger().setLogLevel(Logger.LogLevel.VERBOSE);

tracker = analytics.newTracker(TRACKER_ID);
Map<String, String> hit = new HitBuilders.ScreenViewBuilder().build();
hit.put("&sc", "end");
tracker.send(hit);

通过查看logcat的“发送命中”行并确保它具有sc = end参数来验证sc参数是否已发送出去。看起来应该是这样的:

V/GAV4﹕ Thread[GAThread,5,main]: Sending hit to store  PATH: https:  PARAMS: v=1,  ul=en-us,  t=screenview,  ht=1417708068636,  sr=800x1216,  a=1647515955,  an=Analytics Playground,  sc=end,  tid=UA-xxxxxxxx-1,  aid=com.example.analyticsplayground,  cid=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,  av=1.0,  _u=.nK-AL,