集成:

1:在build.gradle文件中添加以下依赖 项:

dependencies {

// AppMetrica SDK.
implementation 'com.yandex.android:mobmetricalib:3.21.0'

}

2:Application - onCreate() 添加如下代码:

public class MyApp extends Application {

@Override
public void onCreate() {

super.onCreate();

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key).build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);
// Automatic tracking of user activity.
YandexMetrica.enableActivityAutoTracking(this);

}

}

基本统计功能到这里就集成完毕了。接下来是

可选配置:

1.位置检测配置:

默认情况下,AppMetrica 通过 IP 地址确定设备的位置,并具有特定于国家/地区的精度。

想要获取准确到城市的位置,请在AndroidManifest.xml文件中添加定位权限:

<manifest>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application>...</application>
</manifest>

2.会话超时的时长配置,默认时间是10秒:

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

3.跟踪应用程序崩溃,默认开启,可选择性关闭:

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key)
// Disabling the data sending about the app crashes.
.withCrashReporting(false)
.build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);

4.启用/禁用日志记录:

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key)
// Setting up the configuration. For example, to enable logging.
.withLogs()
.build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);

5.启用/禁用位置记录,默认记录:

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key)
// Disabling the data sending about the device location.
.withLocationTracking(false)
.build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);

手动设置位置:

1.在初始化的时候设置位置

// Determining the location.
Location currentLocation = ...;

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key)
// Set your own location information of the device.
.withLocation(currentLocation)
.build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);

2.在初始化之后,具体业务中设置位置:

// Determining the location.
Location currentLocation = ...;
// Setting your own location information of the device.
YandexMetrica.setLocation(currentLocation);

如果还需要恢复成自动获取位置:

YandexMetrica.setLocation(null);

自定义事件 

json格式嵌套,深度最多五层

String eventParameters = "{\"name\":\"Alice\", \"age\":\"18\"}";

YandexMetrica.reportEvent("New person", eventParameters);

从 WebView 的 JavaScript 代码发送事件

WebView webView = (WebView) findViewById(R.id.myWebView);
// do your initialization here
webView.getSettings().setJavaScriptEnabled(true);
YandexMetrica.initWebViewReporting(webView);
webView.loadUrl(myURL);

 js发送事件:

function buttonClicked() {
AppMetrica.reportEvent('Button clicked!', '{}');
})

初始化建议


  • 如果您的应用程序有多个进程,请为每个进程使用相同的配置初始化库。否则,配置可能取决于哪个进程首先启动。
  • 在Application.onCreate()方法中初始化 AppMetrica 库。这将在每个进程中初始化库。
    如果在集成第三方库后出现初始化错误,请确保仅在主进程中初始化第三方库。
  • ContentProvider
    实例是在Application
    实例之前创建的。如果您在​​​Application.onCreate()​​​中初始化库,您将在​​ContentProvider.onCreate()​​无法从该方法发送数据。
  • 避免在 ​​Application.onCreate()​​ 方法中进行冗长的操作。因为该方法的执行时间直接影响第一个Activity、Service或Receiver的启动速度。


跟踪本机应用程序崩溃

如果将库 SO 文件添加到项目中,则会自动报告本机应用程序崩溃。默认情况下没有 SO 文件。要添加它们,请在块中的build.gradle文件中添加以下依赖项​​dependencies​

dependencies {
...
implementation 'com.yandex.android:mobmetricalib-ndk-crashes:1.1.0'
}

如果要禁用自动跟踪方法

// Creating an extended library configuration.
YandexMetricaConfig config = YandexMetricaConfig.newConfigBuilder(API_key)
// Disabling the data sending about the native app crashes.
.withNativeCrashReporting(false)
.build();
// Initializing the AppMetrica SDK.
YandexMetrica.activate(getApplicationContext(), config);