跟踪用户活动

在 AppMetrica 中,会话是用户与应用交互的时间段。

后台会话

后台会话从 AppMetrica SDK 初始化开始。

如果在后台会话中,AppMetrica 收到 Android 的 onResume() 系统事件或 iOS 的 UIApplicationDidBecomeActiveNotification 系统事件,则用户会话开始。

任何在这些系统事件之前发送的事件都属于后台会话。

后台会话可能指:

  • 从点击应用图标到显示应用界面的时间间隔。
  • 开始提供接收推送通知的服务。

用户会话

用户会话开始时,AppMetrica 接收以下系统事件:

用户会话在会话超时时间结束时终止。 会话超时时间从用户最小化应用时开始计时。

任何在上述系统事件之后发送的事件都属于用户会话。

默认情况下,AppMetrica 会将用户在应用切换到后台模式(用户最小化应用或打开系统设置)后经过 一段显著时间 再返回应用的行为视为新会话。

设置会话超时时间

会话超时时间是指用户最小化应用后,再次打开应用前必须经过的时间 period。 您可以在 SDK 中设置会话超时时间。

  • 如果用户在超时时间结束前再次打开应用,当前会话将继续。
  • 如果用户在超时后再次打开应用,将开始新的会话。

默认情况下,会话超时时间为10秒。 参数 sessionTimeout 的最小可接受值为 10 秒。

要更改超时时间,在创建扩展库配置时,将秒数的值传递给 withSessionTimeout(int sessionTimeout) 方法。

// Creating an extended library configuration.
val config = AppMetricaConfig.newConfigBuilder(API_KEY)
    // Setting the length of the session timeout.
    .withSessionTimeout(15)
    .build()
// Initializing the AppMetrica SDK.
AppMetrica.activate(applicationContext, config)
// Creating an extended library configuration.
AppMetricaConfig config = AppMetricaConfig.newConfigBuilder(API_KEY)
        // Setting the length of the session timeout.
        .withSessionTimeout(15)
        .build();
// Initializing the AppMetrica SDK.
AppMetrica.activate(getApplicationContext(), config);

监控应用生命周期

如果应用程序将最低 Android 版本设置为 4.0 或更高,AppMetrica 库可以自动跟踪其生命周期。 库初始化后,调用 AppMetrica.enableActivityAutoTracking(Application application) 方法:

class YourApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        // Initializing the AppMetrica SDK.
        AppMetrica.activate(...)
        // Automatic tracking user activity.
        AppMetrica.enableActivityAutoTracking(this)
    }
}
public class YourApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        // Initializing the AppMetrica SDK.
        AppMetrica.activate(...);
        // Automatic tracking user activity.
        AppMetrica.enableActivityAutoTracking(this);
    }
}

备注

自动活动跟踪仅对主 API 密钥生效。 在向额外的 API 密钥发送统计数据时,使用 IReporter 接口的方法。

如果应用中最低 Android 版本低于 4.0,请使用以下方法:AppMetrica.resumeSession(activity) 和 AppMetrica.pauseSession(activity),分别在对应的活动方法中使用,即 onResume() 和 onPause()。 使用这些方法时,请确保始终通过调用AppMetrica.pauseSession(activity)方法终止活动会话。 如果没有调用该方法,库会认为会话仍处于活动状态,并定期与AppMetrica服务部分进行数据交换。 这可能会导致会话时长和能耗的统计不准确。

Example:

class YourActivity : Activity() {
    override fun onResume() {
        super.onResume()
        AppMetrica.resumeSession(this)
    }

    override fun onPause() {
        AppMetrica.pauseSession(this)
        super.onPause()
    }
}
public class YourActivity extends Activity {
    @Override
    protected void onResume() {
        super.onResume();
        AppMetrica.resumeSession(this);
    }

    @Override
    protected void onPause() {
        AppMetrica.pauseSession(this);
        super.onPause();
    }
}

使用额外的API密钥

通过向额外的 API 密钥发送数据,您可以控制统计数据的访问权限。 使用报告器传输事件。 要获取更多信息,请查看使用示例。

为了正确追踪,请手动配置报告器,为每个报告器发送会话开始和暂停的事件。 在实现活动的 onResume() 和 onPause() 时,请在 IReporter 接口中使用 resumeSession() 和 pauseSession() 方法:

class YourActivity : Activity() {
    override fun onResume() {
        super.onResume()
        AppMetrica.getReporter(applicationContext, ANOTHER_API_KEY).resumeSession()
    }

    override fun onPause() {
        AppMetrica.getReporter(applicationContext, ANOTHER_API_KEY).pauseSession()
        super.onPause()
    }
}
public class YourActivity extends Activity {
    @Override
    protected void onResume() {
        super.onResume();
        AppMetrica.getReporter(getApplicationContext(), ANOTHER_API_KEY).resumeSession();
    }

    @Override
    protected void onPause() {
        AppMetrica.getReporter(getApplicationContext(), ANOTHER_API_KEY).pauseSession();
        super.onPause();
    }
}

这有助于库准确监测:

  • 活跃用户数量。
  • Session length.
  • 应用使用频率。

如果您没有找到所需答案,可以通过反馈表单提交您的问题。 请尽可能详细地描述问题。 如果可能,请附上截图。

联系支持 为文档提出改进建议

在SDK中设置的会话超时时间。

上一篇
下一篇