YandexMetrica class

com.yandex.mertica

public final class YandexMetrica

Methods of the class are used for configuring the library.

Methods

voidactivate(@NonNull Context context, @NonNull YandexMetricaConfig config)

Initializes the library in an application with the extended startup configuration.

voidactivateReporter(@NonNull Context context, @NonNull ReporterConfig config)

Activates the reporter with the extended configuration. The reporter should be activated with the configuration using a different API key instead of the app's API key.

voidenableActivityAutoTracking(@NonNull Application application)

Enables automatic app lifecycle tracking.

intgetLibraryApiLevel()

Returns the API level of the library.

StringgetLibraryVersion()

Returns the current version of the AppMetrica library.

IReportergetReporter(@NonNull Context context, @NonNull String apiKey)

Returns the instance that implements the interface IReporter for a specified API key.

voidpauseSession(@Nullable Activity activity)

Pauses the user session.

voidregisterReferrerBroadcastReceivers(@NonNull BroadcastReceiver... anotherReferrerReceivers)

Registers a BroadcastReciever for other tracking system.

voidreportAppOpen(@NonNull String deeplink)

Sends a message about the app launching from a deeplink as a string.

voidreportAppOpen(@NonNull Activity activity)

Sends a message about the app launching from a deeplink.

voidreportError(@NonNull String message, @Nullable Throwable error)

Sends a custom error message.

voidreportEvent(@NonNull String eventName)

Sends an event message.

voidreportEvent(@NonNull String eventName, @Nullable String jsonValue)

Sends an event message as a string in JSON format.

voidreportEvent(@NonNull String eventName, @Nullable Map<String, Object> attributes)

Sends an event message as a set of attributes (Map).

voidreportNativeCrash(@NonNull String nativeCrash)

Sends a message about native app crashes.

voidreportReferralUrl(@NonNull String referralUrl)

Sets referral URL for app installs. This method can be used to track some traffic sources.

voidreportRevenue(@NonNull Revenue revenue)

Sends information about the purchase to the AppMetrica server.

voidreportUnhandledException(@NonNull Throwable exception)

Sends a message about an application crash.

voidreportUserProfile(@NonNull UserProfile profile)

Sends information about the user profile update to the AppMetrica server.

voidrequestAppMetricaDeviceID(@NonNull AppMetricaDeviceIDListener listener)

Requests the unique AppMetrica ID (deviceID).

voidrequestDeferredDeeplinkParameters(@NonNull DeferredDeeplinkParametersListener listener)

Requests parameters of the deferred deeplink.

voidresumeSession(@Nullable Activity activity)

Resumes the session, or creates a new one if the session timeout has expired.

voidsendEventsBuffer()

Sends stored events from the buffer.

voidsetLocation(@Nullable Location location)

Sets custom location of the device.

voidsetLocationTracking(boolean enabled)

Enables/disables sending location of the device .

voidsetStatisticsSending(@NonNull Context context, boolean enabled)

Enables/disables sending statistics to the AppMetrica server.

voidsetUserProfileID(@Nullable String userProfileID)

Sets the ID of the user profile.

Method descriptions

activate

public static void activate(@NonNull Context context, @NonNull YandexMetricaConfig config)

Initializes the library in an application with the extended startup configuration.

Parameters:
context

The instance of the Context class.

configThe instance of the YandexMetricaConfig class which contains the extended startup configuration for the library.

activateReporter

public static void activateReporter(@NonNull Context context, @NonNull ReporterConfig config)

Activates the reporter with the extended configuration. The reporter should be activated with the configuration using a different API key instead of the app's API key.

Note. Activation must occur before calling the getReporter() method.
Parameters:
context

The instance of the Context class.

configThe instance of the ReporterConfig class which contains the extended configuration for the reporter.

enableActivityAutoTracking

public static void enableActivityAutoTracking(@NonNull Application application)

Enables automatic app lifecycle tracking.

This is an alternative to the methods resumeSession(Activity activity) and pauseSession(Activity activity). This method must be called immediately after initializing the AppMetrica library, before opening the first app screen Supports Android version 4.0 and later.

Parameters:
applicationThe instance of the Application class.

getLibraryApiLevel

public static int getLibraryApiLevel()

Returns the API level of the library.

Returns:

The API level of the library.

getLibraryVersion

public static String getLibraryVersion()

Returns the current version of the AppMetrica library.

Returns:

Library version.

getReporter

public static IReporter getReporter(@NonNull Context context, @NonNull String apiKey)

Returns the instance that implements the interface IReporter for a specified API key.

Used for sending statistics using a different API key instead of the app's API key.

Parameters:
context

The instance of the Context class.

apiKey

API key that differs from the main application API key.

Returns:

The instance that implements the IReporter interface.

pauseSession

public static void pauseSession(@Nullable Activity activity)

Pauses the user session.

It is called in the implementation of the onPause() method of the Activity of your application. Use it after initializing the library in the app.

Note. The session duration depends on specified timeout. If the time interval between pausing and resuming the session is less than the specified timeout, the current session will be resumed; otherwise, a new one will be created.
Parameters:
activityThe instance of the Activity class.

registerReferrerBroadcastReceivers

public static void registerReferrerBroadcastReceivers(@NonNull BroadcastReceiver... anotherReferrerReceivers)

Registers a BroadcastReciever for other tracking system.

Used for tracking INSTALL_REFERRER from GooglePlay if you use other tracking systems.

Parameters:
anotherReferrerReceiversThe instance of the BroadcastReceiver class.

reportAppOpen

public static void reportAppOpen(@NonNull String deeplink)

Sends a message about the app launching from a deeplink as a string.

Parameters:
deeplinkDeeplink as a string.

reportAppOpen

public static void reportAppOpen(@NonNull Activity activity)

Sends a message about the app launching from a deeplink.

Parameters:
activityThe instance of the Activity class.

reportError

public static void reportError(@NonNull String message, @Nullable Throwable error)

Sends a custom error message.

Parameters:
messageError description.

Can't be empty.

errorError class.

reportEvent

public static void reportEvent(@NonNull String eventName)

Sends an event message.

Parameters:
eventNameShort name or description of the event

Can't be empty.

reportEvent

public static void reportEvent(@NonNull String eventName, @Nullable String jsonValue)

Sends an event message as a string in JSON format.

Parameters:
eventNameShort name or description of the event

Can't be empty.

jsonValueA valid JSON string with custom content.

reportEvent

public static void reportEvent(@NonNull String eventName, @Nullable Map<String, Object> attributes)

Sends an event message as a set of attributes (Map).

Parameters:
eventNameShort name or description of the event

Can't be empty.

attributesA set of attributes (Map).

reportNativeCrash

public static void reportNativeCrash(@NonNull String nativeCrash)

Sends a message about native app crashes.

Parameters:
nativeCrashNative crash message.

reportReferralUrl

public static void reportReferralUrl(@NonNull String referralUrl)

Sets referral URL for app installs. This method can be used to track some traffic sources.

Parameters:
referralUrl

Referral URL of the app install.

reportRevenue

public static void reportRevenue(@NonNull Revenue revenue)

Sends information about the purchase to the AppMetrica server.

Parameters:
revenue

The instance of the Revenue class which contains information about a purchase.

reportUnhandledException

public static void reportUnhandledException(@NonNull Throwable exception)

Sends a message about an application crash.

Parameters:
exceptionThe instance of the Throwable class.

reportUserProfile

public static void reportUserProfile(@NonNull UserProfile profile)

Sends information about the user profile update to the AppMetrica server.

Parameters:
profileThe instance of the UserProfile class which contains information about the user profile.

requestAppMetricaDeviceID

public static void requestAppMetricaDeviceID(@NonNull AppMetricaDeviceIDListener listener)

Requests the unique AppMetrica ID (deviceID).

The unique appmetrica identifier deviceID is available in AppMetricaDeviceIDListener.onLoaded(String deviceID) , if there are no errors. If an error occurs, the error value is stored in AppMetricaDeviceIDListener.onError(Error error, String referrer).

Note. In the Logs API and Post API deviceID referred to as appmetrica_device_id.
Parameters:
listenerThe interface for notification about getting a unique identifier.

requestDeferredDeeplinkParameters

public static void requestDeferredDeeplinkParameters(@NonNull DeferredDeeplinkParametersListener listener)

Requests parameters of the deferred deeplink.

Deferred deeplink parameters can be obtained during the DeferredDeeplinkParametersListener.onParametersLoaded(Map<String, String> parameters) method execution, if there are no errors while getting Google Play Install Referrer. For more information, see Usage examples and Deferred deeplinks support.

Parameters:
listenerThe interface for notification about getting parameters of the deferred deeplink.

resumeSession

public static void resumeSession(@Nullable Activity activity)

Resumes the session, or creates a new one if the session timeout has expired.

It is called in the implementation of the onResume() method of the Activity of your application. Use it after initializing the library in the app.

Note. The session duration depends on specified timeout. If the time interval between pausing and resuming the session is less than the specified timeout, the current session will be resumed; otherwise, a new one will be created.
Parameters:
activityThe instance of the Activity class.

sendEventsBuffer

public static void sendEventsBuffer()

Sends stored events from the buffer.

AppMetrica SDK does not send an event immediately after it occurred. The library stores event data in the buffer. The sendEventsBuffer() method sends data from the buffer and flushes it. Use the method to force sending stored events after passing important checkpoints of user scenarios.
Attention. Frequent use of the method can lead to increased outgoing internet traffic and energy consumption.

setLocation

public static void setLocation(@Nullable Location location)

Sets custom location of the device.

To resume the location detection by the library, pass to the method the null value.

Parameters:
location

Information about the location of the device.

setLocationTracking

public static void setLocationTracking(boolean enabled)

Enables/disables sending location of the device .

Parameters:
enabled

A flag indicating if sending information about the device location is enabled.

The default value is true.

Possible values:
  • true — Sending information about the device location is enabled.
  • false — Sending information about the location of the device is disabled.

setStatisticsSending

public static void setStatisticsSending(@NonNull Context context, boolean enabled)

Enables/disables sending statistics to the AppMetrica server.

For more information about using the method, see Disable and enable sending statistics.
Note. Disabling sending also turns off sending data from all reporters that initialized with the other API key.
Parameters:
context

The instance of the Context class.

enabled

A flag indicating that sending statistics is enabled.

The default value is true.

Possible values:
  • true — Sending statistics is enabled.
  • false — Sending statistics is disabled.

setUserProfileID

public static void setUserProfileID(@Nullable String userProfileID)

Sets the ID of the user profile.

Parameters:
userProfileID

User profile ID.