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文件中添加定位权限:
...
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 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()
中初始化库,您将在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);