Installation and initialization

Step 1. Add the library to your project

If you use Gradle for building the app, add this dependency to the build.gradle file:

dependencies {
    // AppMetrica SDK.
    implementation 'com.yandex.android:mobmetricalib:3.13.1'
    // Play Install Referrer library.
    implementation 'com.android.installreferrer:installreferrer:1.1.2'
}
Copied to clipboard

Connecting the library Play Install Referrer is mandatory. It is used to track the installation source.

If you don't use Gradle

Download the libraries AppMetrica SDK and Play Install Referrer, and add them to the project.

Step 2. Initialize the library

Attention. You should take into account some features of the AppMetrica library during initialization. For more information, see Features of the AppMetrica library.

Initialize the library in the app and set up user activity tracking. Extend the Application class and override the onCreate() method as follows:

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);
    }
}
Copied to clipboard
What is the API key?

The API key is a unique application identifier that is issued in the AppMetrica web interface during app registration.

Make sure you have entered it correctly.

AppMetrica allows tracking pre-installed apps. To use this feature, you should initialize the library with the extended configuration.

Step 3. (Optional) Configure location detection

Location allows you to estimate the geographical distribution of users. By default, AppMetrica determines the device location by the IP address with accuracy to the country.

To determine the device location with accuracy to the city, open the AndroidManifest.xml file and make the following changes before the application element:

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

ACCESS_COARSE_LOCATION allows you to track the device's location. For more information, see Android documentation.

Step 4. (Optional) Configure sending events, profile attributes, and revenue

To collect information on user actions in the app, set up sending your own events. For more information, see Sending your own events.

To collect information about users, set up profile attributes sending. For more information, see Profiles.

Note. Unlike events, a profile attribute can take only one value. When you send a new attribute value, the old value is overwritten.

To track in-app purchases, set up revenue sending. For more information, see Revenue.

Step 5. Test the library operation

Attention. Before testing the library, ensure that the SDK is initialized in compliance with recommendations.
To test how the library works:
  1. Start the app with the AppMetrica SDK and use it for a while.
  2. Make sure your device is connected to the internet.
  3. In the AppMetrica interface, make sure that:
    • There is a new user in the Audience report.
    • The number of sessions in the Engagement → Sessions report has increased.
    • There are events and profile attributes in the Events and Profiles reports.

Troubleshooting

The number of sessions does not increase

Check your session tracking settings. For more information, see Tracking user activity.

There are no events in the report
  1. Perform a minimum of 10 app actions that trigger the event sending.

    It's necessary because AppMetrica accumulates events in the buffer and sends to the server in several parts.

  2. Wait for 10 minutes and check the report. Reports don't display events immediately.