Class YandexMetrica
public final class YandexMetrica
extends java.lang.Object
-
Method Summary
Modifier and Type Method Description static voidactivate(android.content.Context context, YandexMetricaConfig config)static voidactivateReporter(android.content.Context context, ReporterConfig config)Activates the reporter withReporterConfig.static voidenableActivityAutoTracking(android.app.Application application)Sessions should be tracked automatically without invoking this method.static intgetLibraryApiLevel()static java.lang.StringgetLibraryVersion()static YandexMetricaPluginsgetPluginExtension()Creates aYandexMetricaPluginsinstance that can send plugin events to main API key.static IReportergetReporter(android.content.Context context, java.lang.String apiKey)Creates anIReporterthat can send events to an alternative api key.static voidinitWebViewReporting(android.webkit.WebView webView)Adds Javascript interface named "AppMetrica" to WebView's javascript.static voidpauseSession(android.app.Activity activity)Helper method for tracking the life cycle of the application.static voidputErrorEnvironmentValue(java.lang.String key, java.lang.String value)Sets key - value data to be used as additional information, associated with your unhandled exception and error reports.static voidreportAppOpen(android.app.Activity activity)Sends report about open app via deeplinkstatic voidreportAppOpen(android.content.Intent intent)Sends report about open app via deeplinkstatic voidreportAppOpen(java.lang.String deeplink)Sends report about open app via deeplink.static voidreportECommerce(ECommerceEvent event)Sends e-commerce event.static voidreportError(java.lang.String identifier, java.lang.String message)Sends an error.static voidreportError(java.lang.String identifier, java.lang.String message, java.lang.Throwable error)Sends an error.static voidreportError(java.lang.String message, java.lang.Throwable error)Sends an error.static voidreportEvent(java.lang.String eventName)Sends report by event name.static voidreportEvent(java.lang.String eventName, java.lang.String jsonValue)Sends report by event name and event value.static voidreportEvent(java.lang.String eventName, java.util.Map<java.lang.String,java.lang.Object> attributes)Sends report by dictionary.static voidreportNativeCrash(java.lang.String nativeCrash)Deprecated.Will be removed in future versions.static voidreportReferralUrl(java.lang.String referralUrl)Deprecated.static voidreportRevenue(Revenue revenue)Sends information about the purchase.static voidreportUnhandledException(java.lang.Throwable exception)Sends unhandled exception byThrowableobject.static voidreportUserProfile(UserProfile profile)Sends information about the user profile.static voidrequestAppMetricaDeviceID(AppMetricaDeviceIDListener listener)Retrieves unique AppMetrica device identifier.static voidrequestDeferredDeeplink(DeferredDeeplinkListener listener)Requests deferred deeplink.static voidrequestDeferredDeeplinkParameters(DeferredDeeplinkParametersListener listener)Requests deferred deeplink parameters.static voidresumeSession(android.app.Activity activity)Helper method for tracking the life cycle of the application.static voidsendEventsBuffer()Initiates forced sending of all stored events from the buffer.static voidsetLocation(android.location.Location location)SetsLocationto be used as location for reports of AppMetrica.static voidsetLocationTracking(boolean enabled)Sets whether AppMetrica should include location information within its reports.static voidsetLocationTracking(android.content.Context context, boolean enabled)Sets whether AppMetrica should include location information within its reports.static voidsetStatisticsSending(android.content.Context context, boolean enabled)Enables/disables statistics sending to the AppMetrica server.static voidsetUserProfileID(java.lang.String userProfileID)Sets the ID of the user profile.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Method Details
-
activate
public static void activate(@NonNull android.content.Context context, @NonNull YandexMetricaConfig config)- Parameters:
context-Contextobject. Any application context.config- YandexMetrica configuration object.- See Also:
YandexMetricaConfig
-
sendEventsBuffer
public static void sendEventsBuffer()Initiates forced sending of all stored events from the buffer.AppMetrica SDK doesn't send events immediately after they occurred. It stores events data in the buffer. This method forcibly initiates sending all the data from the buffer and flushes it.
Use the method after important checkpoints of user scenarios.
WARNING: Frequent use of the method can lead to increasing outgoing internet traffic and energy consumption.
-
resumeSession
public static void resumeSession(@Nullable android.app.Activity activity)Helper method for tracking the life cycle of the application.
It should be called intoActivity.onResume()()}.- Parameters:
activity-Activityobject.- Throws:
java.lang.IllegalArgumentException- Ifactivityis null.- See Also:
Activity,Activity.onResume(),pauseSession(android.app.Activity)
-
pauseSession
public static void pauseSession(@Nullable android.app.Activity activity)Helper method for tracking the life cycle of the application.
It should be called intoActivity.onPause().- Parameters:
activity-Activityobject.- Throws:
java.lang.IllegalArgumentException- Ifactivityis null.- See Also:
Activity,Activity.onPause(),resumeSession(android.app.Activity)
-
enableActivityAutoTracking
public static void enableActivityAutoTracking(@NonNull android.app.Application application)Sessions should be tracked automatically without invoking this method. SeeYandexMetricaConfig.Builder.withSessionsAutoTrackingEnabled(boolean)Use this method only if your case is special and you don't see automatically tracked user sessions.Helper method for tracking the lifecycle of the application
Sessions should be tracked automatically- Parameters:
application-Applicationwhose activities starts and stops should be tracked automatically- Throws:
java.lang.IllegalArgumentException- ifapplicationis null. *
-
reportEvent
public static void reportEvent(@NonNull java.lang.String eventName)Sends report by event name.- Parameters:
eventName- Event name. In theeventNameparameter, pass a short name or description of the event.- Throws:
java.lang.IllegalArgumentException- IfeventNameis null.- See Also:
reportEvent(String)
-
reportError
public static void reportError(@NonNull java.lang.String message, @Nullable java.lang.Throwable error)Sends an error. Use this method to report un unexpected situation. If you use this method errors will be grouped byerrorstacktrace. If you want to influence the way errors are grouped usereportError(String, String, Throwable)orreportError(String, String)- Parameters:
message- Short description or name of the error.error-Throwableobject for the error. Can be null.- Throws:
java.lang.IllegalArgumentException- Ifmessageis null.- See Also:
reportUnhandledException(Throwable)
-
reportError
public static void reportError(@NonNull java.lang.String identifier, @Nullable java.lang.String message)Sends an error. Use this method to report un unexpected situation. This method should be used if you want to customize error grouping. If not usereportError(String, Throwable)- Parameters:
identifier- An identifier used for grouping errors. Errors that have the same identifiers will belong in one group. Do not use dynamically formed strings or exception messages as identifiers to avoid having too many small crash groups. Cannot be null.message- Short description or name of the error. Can be null.- Throws:
java.lang.IllegalArgumentException- Ifidentifieris null.
-
reportError
public static void reportError(@NonNull java.lang.String identifier, @Nullable java.lang.String message, @Nullable java.lang.Throwable error)Sends an error. Use this method to report un unexpected situation. This method should be used if you want to customize error grouping. If not usereportError(String, Throwable)errorstacktrace will NOT be used for grouping, onlyidentifier.- Parameters:
identifier- An identifier used for grouping errors. Errors that have the same identifiers will belong in one group. Do not use dynamically formed strings or exception messages as identifiers to avoid having too many small crash groups. Cannot be null.message- Short description or name of the error. Can be null.error-Throwableobject for the error. Its stacktrace will not be considered for error grouping. Can be null.- Throws:
java.lang.IllegalArgumentException- Ifidentifieris null.
-
reportUnhandledException
public static void reportUnhandledException(@NonNull java.lang.Throwable exception)Sends unhandled exception byThrowableobject.- Parameters:
exception-Throwableobject for the unhandled exception.- Throws:
java.lang.IllegalArgumentException- Ifexceptionis null.- See Also:
YandexMetricaConfig.Builder.withCrashReporting(boolean),reportError(String, Throwable)
-
reportNativeCrash
@Deprecated public static void reportNativeCrash(@NonNull java.lang.String nativeCrash)Deprecated.Will be removed in future versions. Sends native crash byStringobject.- Parameters:
nativeCrash-Stringobject for the native crash. Contains a crash dump information.- Throws:
java.lang.IllegalArgumentException- IfnativeCrashis null.- See Also:
YandexMetricaConfig.Builder.withNativeCrashReporting(boolean),reportUnhandledException(Throwable),reportError(String, Throwable)
-
reportEvent
public static void reportEvent(@NonNull java.lang.String eventName, @Nullable java.lang.String jsonValue)Sends report by event name and event value.- Parameters:
eventName- Event name. In theeventNameparameter, pass a short name or description of the event.jsonValue- Event value. In theeventValueparameter, pass aJSONObjectrepresented as aStringobject or pass aStringobject represented in theJava JSON format. Maximum level of nesting (for JSON object) - 5.EXAMPLE:
{ "firstName": "John", "lastName": "Smith", "age": 25, "nickname": "JS" "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", }, "phoneNumbers": [ { "type": "HOME", "number": "212 555-1234" }, { "type": "FAX", "number": "646 555-4567" } ] }- Throws:
java.lang.IllegalArgumentException- IfeventNameorjsonValueis null.- See Also:
reportEvent(String, java.util.Map),reportEvent(String),JSONObject.toString()
-
reportEvent
public static void reportEvent(@NonNull java.lang.String eventName, @Nullable java.util.Map<java.lang.String,java.lang.Object> attributes)Sends report by dictionary.- Parameters:
eventName- Event name. In theeventNameparameter, pass a short name or description of the event.attributes- Event value. In theattributesparameter, pass aMapwhose keys are of typeStringand whose values are of supported types:Boolean,String, and the wrappers for the primitive number typesDouble,Integer, ...- Throws:
java.lang.IllegalArgumentException- IfeventNameis null.- See Also:
reportEvent(String, String),reportEvent(String)
-
reportAppOpen
public static void reportAppOpen(@NonNull android.app.Activity activity)Sends report about open app via deeplink- Parameters:
activity- - opened activity with corresponding intent with deeplink
-
reportAppOpen
public static void reportAppOpen(@NonNull java.lang.String deeplink)Sends report about open app via deeplink. Null and empty values will be ignored.- Parameters:
deeplink- Deeplink value.
-
reportAppOpen
public static void reportAppOpen(@NonNull android.content.Intent intent)Sends report about open app via deeplink- Parameters:
intent- - intent used to open activity with deeplink
-
reportReferralUrl
public static void reportReferralUrl(@NonNull java.lang.String referralUrl)Deprecated.Sets referral URL for this installation. This might be required to track some specific traffic sources like Facebook.- Parameters:
referralUrl- referral URL value.
-
setLocation
public static void setLocation(@Nullable android.location.Location location)Sets
Locationto be used as location for reports of AppMetrica.If location is set using this method, it will be used instead of auto collected location. To switch back to auto collected location, pass
nulltosetLocation(Location).NOTE: Permissions:
Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATIONimprove the quality of auto collected location.- Parameters:
location- location that will be used instead of auto collected- See Also:
setLocationTracking(boolean),setLocationTracking(Context, boolean)
-
setLocationTracking
public static void setLocationTracking(boolean enabled)Sets whether AppMetrica should include location information within its reports.NOTE: Default value is false
- Parameters:
enabled-trueto allow AppMetrica to record location information in reports, otherwisefalse.- See Also:
setLocation(android.location.Location)
-
setLocationTracking
public static void setLocationTracking(@NonNull android.content.Context context, boolean enabled)Sets whether AppMetrica should include location information within its reports.NOTE: Default value is false
- Parameters:
context- Context objectenabled-trueto allow AppMetrica to record location information in reports, otherwisefalse.- See Also:
setLocation(android.location.Location)
-
setStatisticsSending
public static void setStatisticsSending(@NonNull android.content.Context context, boolean enabled)Enables/disables statistics sending to the AppMetrica server. By default, the sending is enabled.NOTE: Disabling this option also turns off data sending from the reporters that initialized for different apiKey.
- Parameters:
context- Context objectenabled-trueto allow AppMetrica sending statistics, otherwisefalse.
-
activateReporter
public static void activateReporter(@NonNull android.content.Context context, @NonNull ReporterConfig config)Activates the reporter withReporterConfig.- Parameters:
context- Context object.config- The ReporterConfig object.
-
getReporter
@NonNull public static IReporter getReporter(@NonNull android.content.Context context, @NonNull java.lang.String apiKey)Creates an
For every api key only oneIReporterthat can send events to an alternative api key.IReporterinstance is created. You can either query it each time you need it, or save the reference by yourself.- Parameters:
context- Context objectapiKey- api key of the reporter- Returns:
- reporter instance for given api key
-
getLibraryVersion
@NonNull public static java.lang.String getLibraryVersion()- Returns:
- VERSION of library.
- See Also:
getLibraryApiLevel()
-
getLibraryApiLevel
public static int getLibraryApiLevel()- Returns:
- API LEVEL of library.
- See Also:
getLibraryVersion()
-
requestDeferredDeeplinkParameters
public static void requestDeferredDeeplinkParameters(@NonNull DeferredDeeplinkParametersListener listener)Requests deferred deeplink parameters. Parameters will be available after receiving Google Play installation referrer from Google Play. Google Play installation referrer is usually received soon after first launch of application. After Google Play installation referrer is received, deferred deeplink parameters will be extracted and delivered toDeferredDeeplinkParametersListener.onParametersLoaded(Map)listener. If error occurs it will be delivered toDeferredDeeplinkParametersListener.onError(DeferredDeeplinkParametersListener.Error, String)- Parameters:
listener- the object that receives callbacks when Google Play referrer is received or error occurs.
-
requestDeferredDeeplink
Requests deferred deeplink. It will be available after receiving Google Play installation referrer from Google Play. Google Play installation referrer is usually received soon after first launch of application. After Google Play installation referrer is received, deferred deeplink will be extracted and delivered toDeferredDeeplinkListener.onDeeplinkLoaded(String)listener. If error occurs it will be delivered toDeferredDeeplinkListener.onError(DeferredDeeplinkListener.Error, String)- Parameters:
listener- the object that receives callbacks when Google Play referrer is received or error occurs.
-
requestAppMetricaDeviceID
Retrieves unique AppMetrica device identifier. It is used to identify a device in the statistics.NOTE: AppMetrica device ID is used in the Logs API and Post API as 'appmetrica_device_id'.
- Parameters:
listener- The AppMetricaDeviceIDListener object
-
setUserProfileID
public static void setUserProfileID(@Nullable java.lang.String userProfileID)Sets the ID of the user profile. NOTE: The string value can contain up to 200 characters.- Parameters:
userProfileID- The custom user profile ID.
-
reportUserProfile
Sends information about the user profile.- Parameters:
profile- TheUserProfileobject. Contains user profile information.
-
reportRevenue
Sends information about the purchase.- Parameters:
revenue- TheRevenueobject. It contains purchase information
-
reportECommerce
Sends e-commerce event.- Parameters:
event- TheECommerceEventobject to be sent.- See Also:
ECommerceEvent
-
putErrorEnvironmentValue
public static void putErrorEnvironmentValue(@NonNull java.lang.String key, @Nullable java.lang.String value)Sets key - value data to be used as additional information, associated with your unhandled exception and error reports.- Parameters:
key- the environment key.value- the environment value. To remove pair from environment passnullvalue.
-
initWebViewReporting
@MainThread public static void initWebViewReporting(@NonNull android.webkit.WebView webView)Adds Javascript interface named "AppMetrica" to WebView's javascript. It allows then to report events to AppMetrica from JS code.
NOTE: This method must be called from main thread.
NOTE: Reporting from JS code will not be enabled on API levels 16 and less due to security issues.- Parameters:
webView- WebView where AppMetrica should enable reporting- See Also:
AppMetricaJsInterface
-
getPluginExtension
Creates a
Only oneYandexMetricaPluginsinstance that can send plugin events to main API key.YandexMetricaPluginsinstance is created per each app process. You can either query it each time you need it, or save the reference by yourself.
NOTE: to use this extension you must activate AppMetrica first viaactivate(android.content.Context, YandexMetricaConfig).- Returns:
- plugin extension instance
-