Documentation

AppMetrica Unity

AppMetrica Unity is a plugin for the Unity3d game platform that includes support for the AppMetrica SDK version 2.0 and later for Android and iOS.

Attention! In version 2.0, the plugin files have been moved to the Assets/AppMetrica shared directory. To update the plugin, delete the Assets/AppMetrica directory from the project and the AppMetrica files from the Assets/Plugins/ directory. Then import the new plugin version (pay attention to the plugin settings).

Integrating the AppMetrica Unity plugin

Step 1. Download the plugin.

Step 2. Add the plugin to the project — open the project in Unity Editor and import the AppMetrica.unitypackage plugin (AssetsImport PackageCustom Package).

Step 3. Open the Assets/AppMetrica folder and drag the prefab AppMetrica to the project's main stage.

If the plugin is integrated this way, the AppMetrica script on the added prefab automatically initializes the AppMetrica SDK and configures tracking of sessions and errors.

The added AppMetrica prefab is a singleton. It isn't deleted when switching to a new Unity stage, and it deletes other objects that the AppMetrica script is installed on.

Configuring the plugin

In the Inspector panel, you can specify settings for the prefab added to the stage:

  • API key — The unique application identifier that is issued in the AppMetrica web interface during app registration.
  • Exceptions Reporting — Enable or disable sending errors.
  • Session Timeout Sec — The timeout for ending the session if the app is inactive.
  • Track Location — Enable or disable transmitting location data.
  • Logging Enabled — Enable or disable logging the library's activity.
  • Handle First Activation As Update — Enable or disable the ability to confirm that the first launch of an app with the AppMetrica Plugin is an app update, and not an install.
Note. The AppMetrica/ExampleUI directory has examples of using AppMetrica in apps.

To launch the iOS version in Unity 4, you need to copy the file Other/Unity.iOS.Extensions.Xcode.dll to the directory Assets/AppMetrica/Editor.

If you don't want to collect device location data, go to the Unity 3d interface (SettingsOther SettingsScripting Define Symbols) and add the APP_METRICA_TRACK_LOCATION_DISABLED directive.

The Assets/AppMetrica/Plugins/Android/ directory has an example of the AndroidManifest.xml file. It contains the components that are necessary for AppMetrica to work. By default, the manifest file is located in the app directory: Editor/Data/PlaybackEngines/AndroidDevelopmentPlayer/AndroidManifest.xml.

Note. If you use your own AndroidManifest.xml, follow the instructions to make changes to it.

If you don't want to collect data about the device location:

  1. Don't add the android.permission.ACCESS_COARSE_LOCATION permission in the AndroidManifest.xml file.
  2. In the Unity 3d interface (SettingsOther SettingsScripting Define Symbols), add the APP_METRICA_TRACK_LOCATION_DISABLED directive.

API methods

In the code, use AppMetrica.Instance for accessing AppMetrica.

Activating the plugin
void ActivateWithAPIKey (string apiKey);
void ActivateWithConfiguration (YandexAppMetricaConfig config);
event ConfigUpdateHandler OnActivation;
YandexAppMetricaConfig? ActivationConfig { get; }
Event tracking
void ReportEvent(string message);
void ReportEvent(string message, Hashtable parameters);
void ReportError(string condition, string stackTrace);
Monitoring sessions
void OnResumeApplication();
void OnPauseApplication();
AppMetrica settings
void SetTrackLocationEnabled(bool enabled);
void SetLocation(Coordinates coordinates);
void SetSessionTimeout(uint sessionTimeoutSeconds);
void SetReportCrashesEnabled(bool enabled);
void SetCustomAppVersion(string appVersion);
void SetLoggingEnabled();
void SetEnvironmentValue(string key, string value);

bool CollectInstalledApps { get; set; }
Information about the version of AppMetrica used
int LibraryApiLevel { get; }
string LibraryVersion { get; }

For more information about the AppMetrica methods and integration in an app, see the documentation sections for Android and iOS.