Installation and initialization

Attention.

Support and development of the plugin has been stopped. We do not guarantee the SDK will work correctly.

  1. Step 1. Integrating the AppMetrica Xamarin plugin
  2. Step 2. Initialize the AppMetrica library
  3. Step 3. (Android) Connect the Play Install Referrer library

AppMetrica Xamarin is a plugin for the Xamarin platform. It includes the AppMetrica SDK support for Android and iOS.

The general flow of using the plugin is shown below:

The plug-in is provided as projects containing the following libraries:

YandexMetricaPCL

This library implements an interface for working with cross-platform parts of the app. The library contains:

  • The general AppMetrica interface — IYandexMetrica.
  • The static proxy class YandexMetrica for working via this interface with an implementation for the current platform.
  • The stub YandexMetricaDummy (which implements the IYandexMetrica interface), used if no implementation has performed registration in the YandexMetrica proxy class.
YandexMetricaAndroid

The library contains an AAR file of the AppMetrica library, generated code for working with the methods of the library and the YandexMetricaImplementation class, which implements the IYandexMetrica interface.

YandexMetricaiOS

The library contains A files for the AppMetrica library, generated code for working with the methods of the library and the YandexMetricaImplementation class, which implements the IYandexMetrica interface.

Step 1. Integrating the AppMetrica Xamarin plugin

Download and enable the plugin libraries in one of the ways:

  • Connect the projects YandexMetrica.Xamarin.PCL, YandexMetrica.Xamarin.iOSBinding and YandexMetrica.Xamarin.AndroidBinding.
  • Assemble and connect the libraries from the projects. After building, the libraries will be available in the directories:
    • YandexMetrica.Xamarin.PCL/bin/(Debug|Release)/YandexMetrica.Xamarin.PCL.dll
    • YandexMetrica.Xamarin.iOSBinding/bin/(Debug|Release)/YandexMetrica.Xamarin.iOS.dll
    • YandexMetrica.Xamarin.AndroidBinding/bin/(Debug|Release)/YandexMetrica.Xamarin.Android.dll

Step 2. Initialize the AppMetrica library

Initialize the AppMetrica library using the Activate() method of the YandexMetricaImplementation class. This method also performs registration of the IYandexMetrica interface implementation.

public static void Activate(Context context, YandexMetricaConfig config, Application app = null) {...}
Parameter Default value Description
config

Extended startup configuration. To create it, pass apiKey: YandexMetricaConfig(string apiKey)

app null

Allows enabling or disabling automatic app lifecycle tracking. By default, tracking is disabled.

If this parameter takes the null value, for correct displays of sessions, use the OnPause() and OnResume() methods of the YandexMetricaImplementation class in the corresponding sections of the project

Parameter Default value Description
config

Extended startup configuration. To create it, pass apiKey: YandexMetricaConfig(string apiKey)

app null

Allows enabling or disabling automatic app lifecycle tracking. By default, tracking is disabled.

If this parameter takes the null value, for correct displays of sessions, use the OnPause() and OnResume() methods of the YandexMetricaImplementation class in the corresponding sections of the project

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.

Connection examples

Examples of working with demonstration projects are shown below.

The YandexMetricaPCL library is connected. The YandexMetrica.Implementation is used for working with AppMetrica.

using System;
using YandexMetricaPCL;
using System.Collections.Generic;

namespace Metrica.Xamarin.CrossPlatform
{
    public class SharedLogic
    {
        public static void LogClick(int clickNumber)
        {
            var dict = new Dictionary<string, string>{ { "click", clickNumber.ToString() } };
            YandexMetrica.Implementation.ReportEvent("Click from shared logic", dict);
        }
    }
}
Copied to clipboard

Step 3. (Android) Connect the Play Install Referrer library

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

  1. Download the latest version of the Play Install Referrer library.
  2. Add it to a project, such as a directory lib.
  3. Enable Build Action for the library in AndroidAarLibrary.