AppMetrica class

Methods of the class are used for configuring the library.

Instance methods

activate(with:)

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

activateReporter(with:)

Initializes a reporter with the extended configuration.

clearAppEnvironment()

Deleting all key-value data associated with all future events.

pauseSession()

Pauses the user session.

reportAdRevenue(_:onFailure:)

Sends information about advertising revenue to the AppMetrica server.

reportECommerce(_:onFailure:)

Sends a message about an E-commerce event.

reportEvent(name:onFailure:)

Sends an event message.

reportEvent(name:parameters:onFailure:)

Sends an event message with additional parameters.

reportExternalAttribution(_:from:onFailure:)

Sends external attribution data to AppMetrica.

reportRevenue(_:onFailure:)

Sends information about the purchase to the AppMetrica server.

reportUserProfile(_:onFailure:)

Sends information about a user profile update.

reporter(for:)

Creates a reporter for sending events to an additional API key.

requestStartupIdentifiers(for:on:completion:)

Requests IDs for specific keys.

requestStartupIdentifiers(on:completion:)

Requests all predefined IDs.

resumeSession()

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

sendEventsBuffer()

Sends stored events from the buffer.

setAppEnvironment(:forKey:)

Sets a key-value pair associated with all future events.

setDataSendingEnabled(_:)

Enables/disables sending statistics to the AppMetrica server.

setupWebViewReporting(with:onFailure:)

Adds a JavaScript interface with the AppMetrica name in a window to the specified WebView. This lets you send client events from JavaScript code.

trackOpeningURL(_:)

Processes the URL that opened the app.

Properties

allowsBackgroundLocationUpdates

Enable/disable background tracking of location updates.

customLocation

Sets custom location of the device.

deviceIDHash

The current appmetrica_device_id_hash.

deviceID

The current appmetrica_device_id.

isAccurateLocationTrackingEnabled

Controls the accuracy of location tracking used by the internal location manager.

isActivated

Indicates whether AppMetrica was activated.

isLocationTrackingEnabled

Enables/disables sending location of the device.

libraryVersion

The current version of the AppMetrica library.

userProfileID

Sets the ID of the user profile.

uuid

The current UUID.

Method descriptions

activate(with:)

class func activate(with: AppMetricaConfiguration)

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

Parameters:

with

The instance of the AppMetricaConfiguration class which contains the extended startup configuration for the library.

activateReporter(with:)

class func activateReporter(with: ReporterConfiguration)

Initializes a reporter with extended configuration.

The configuration of the reporter should be initialized before the first call to the reporter. Otherwise, the configuration of the reporter is ignored.

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

Parameters:

with

The instance of the ReporterConfiguration class which contains the extended configuration for the reporter.

clearAppEnvironment()

class func clearAppEnvironment()

Cleaning the app environment, such as deleting all key-value data associated with all future events.

pauseSession()

class func pauseSession()

Pauses the user session.

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.

For more information about sessions, see Tracking user activity.

reportAdRevenue(_:onFailure:)

class func reportAdRevenue(_ adRevenue: AdRevenueInfo, onFailure: ((Error) -> Void)?)

Sends information about advertising revenue to the AppMetrica server.

Parameters:

adRevenue

The instance of the AdRevenueInfo class which contains information about advertising revenue.

onFailure

A callback method to be called in the event of an error.

reportECommerce(_:onFailure:)

class func reportECommerce(_ eCommerce: ECommerce, onFailure: ((Error) -> Void)?)

Sends a message about an E-commerce event.

Parameters:

eCommerce

The ECommerce class instance.

onFailure

A callback method to be called in the event of an error.

reportEvent(name:onFailure)

class func reportEvent(name: String, onFailure: ((Error) -> Void)?)

Sends an event message.

Parameters:

name

Short name or description of the event.

onFailure

The block that is executed when an error occurs. The error is passed as a block argument.

reportEvent(name:parameters:onFailure)

class func reportEvent(name: String, parameters: [AnyHashable : Any]?, onFailure: ((Error) -> Void)?)

Sends an event message with additional parameters.

Parameters:

name

Short name or description of the event.

parameters

Parameters as key-value pairs.

onFailure

A callback method to be called in the event of an error.

reportExternalAttribution(_:from:onFailure:)

class func reportExternalAttribution(_ attribution: [AnyHashable : Any], from source: AttributionSource, onFailure: ((any Error) -> Void)? = nil)

Sends external attribution data to AppMetrica.

attribution

A dictionary with attribution data. It must be convertible to JSON, or else the method fails with the onFailure error block run.

source

The attribution data source. Source list.

onFailure

A callback method to be called in the event of an error. The error is passed as an argument.

reportRevenue(_:onFailure:)

class func reportRevenue(_ revenueInfo: RevenueInfo, onFailure: ((Error) -> Void)?)

Sends information about the purchase to the AppMetrica server.

Parameters:

revenueInfo

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

onFailure

A callback method to be called in the event of an error.

reportUserProfile(_:onFailure:)

class func reportUserProfile(_ userProfile: UserProfile, onFailure: ((Error) -> Void)?)

Sends information about the user profile update.

Parameters:

userProfile

The instance of the UserProfile class which contains information about the user profile.

onFailure

A callback method to be called in the event of an error.

reporter(for:)

class func reporter(for: String) -> AppMetricaReporting

Creates a reporter for sending events to an additional API key.

To initialize a reporter with the extended configuration, use the activateReporter(with:) method. The configuration of the reporter should be initialized before the first call to the reporter. Otherwise, the configuration of the reporter is ignored.

Parameters:

for

An API key that differs from the app's API key.

Returns:

The instance that implements the AppMetricaReporting protocol for the specified API key.

requestStartupIdentifiers(for:on:completion:)

class func requestStartupIdentifiers(for: [StartupKey], on: dispatch_queue_t?, completion: ([StartupKey : Any]?, Error?) -> Void)

Getting IDs for specific keys

Parameters:

for

An array of identification keys for the query. See StartupKey.

on

Queue for sending a block. If the value is zero, the main queue is used.

completion

The block will be sent when available IDs appear or in case of an error.

If they are available at the time of calling, the block is sent immediately. See the AMAIdentifiersCompletionBlock definition for more information about the returned types.

requestStartupIdentifiers(on:completion:)

class func requestStartupIdentifiers(on: dispatch_queue_t?, completion: ([StartupKey : Any]?, Error?) -> Void)

Getting all predefined IDs

Parameters:

on

Queue for sending a block. If the value is zero, the main queue is used.

completion

The block will be sent when available IDs appear or in case of an error.

Predefined IDs are:

  • StartupKey.uuidKey
  • StartupKey.deviceIDHashKey
  • StartupKey.deviceIDKey

If they are available at the time of calling, the block is sent immediately. See the AMAIdentifiersCompletionBlock definition for more information about the returned types.

resumeSession()

class func resumeSession()

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

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.

For more information about sessions, see Tracking user activity.

sendEventsBuffer()

class func 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.

Alert

Frequent use of the method can lead to increased outgoing internet traffic and energy consumption.

setAppEnvironment(_:forKey:)

class func setAppEnvironment(_ value: String?, forKey: String)

Setting key-value data to be used as additional information associated with all future events.

If the value is zero, the previously set key value is deleted. Nothing is done if the key wasn't added.

Parameters:

value

Value.

key

Key.

setDataSendingEnabled(_:)

class func setDataSendingEnabled(_ enabled: Bool)

Enables/disables sending statistics to the AppMetrica server.

For more information about using the method, see Disabling and enabling sending statistics.

Note

Disabling sending also turns off sending data from all reporters that initialized with the other API key.

Parameters:

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.

setupWebViewReporting(with:onFailure:)

class func setupWebViewReporting(with: JSControlling, onFailure: ((Error) -> Void)?)

Adds a JavaScript interface with the AppMetrica name in a window to the specified webview. This lets you send client events from JavaScript code.

Notes:

  • The method must be called from the main queue.
  • The method is not available on tvOS.
  • Call this method before loading any content. We recommend calling this method before creating a webview and before adding your scripts to WKUserContentController. For more information, see Usage examples.

Parameters:

with

The JSControlling instance.

onFailure

A callback method to be called in the event of an error.

trackOpeningURL(_:)

class func trackOpeningURL(_ URL: URL)

Processes the URL that opened the application.

Used for tracking app openings via deeplink

Parameters:

URL

URL that opened the app.

Property descriptions

allowsBackgroundLocationUpdates

var allowsBackgroundLocationUpdates: Bool { get; set; }

Enable/disable background tracking of location updates. Disabled by default.

customLocation

var customLocation: CLLocation? { get; set; }

Sets custom location of the device.

deviceIDHash

var deviceIDHash: String? { get; }

Retrieves the appmetrica_device_id_hash.

deviceID

var deviceID: String? { get; }

Retrieves the appmetrica_device_id.

isAccurateLocationTrackingEnabled

var isAccurateLocationTrackingEnabled: Bool { get; set; }

Controls the accuracy of location tracking used by the internal location manager.

If set to true, the location manager attempts to use the most accurate location data available. This property takes effect only if the isLocationTrackingEnabled parameter is set to true, and the location wasn't set manually using the customLocation property.

isActivated

var isActivated: Bool { get; }

Indicates whether AppMetrica was activated.

Note

Use this property to check whether AppMetrica has already been activated, typically to avoid redundant activation calls or to make sure that statistics collection is running.

isLocationTrackingEnabled

var isLocationTrackingEnabled: Bool { get; set; }

Enables/disables sending location of the device .

libraryVersion

var libraryVersion: String { get; }

Returns the current version of the AppMetrica library.

userProfileID

var userProfileID: String? { get; set; }

Sets the ID of the user profile. AppMetrica doesn't display predefined attributes in the web interface if ProfieId sending isn't configured.

uuid

var uuid: String { get; }

Retrieves the current UUID.