Documentation

AppMetrica Xamarin

AppMetrica Xamarin is a plugin for using AppMetrica features if you are developing apps using Xamarin for Android and iOS.

The general flow of using the plugin is shown below:

The plugin 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.

Connecting AppMetrica to a project

To integrate AppMetrica in your project, you need to:

  1. Download and enable the plugin libraries. There are several ways to do this:

    • Connect the projects YandexMetrica.Xamarin.PCL, YandexMetrica.Xamarin.iOSBinding and YandexMetrica.Xamarin.AndroidBinding.

    • Build and enable 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
  2. In your app code, perform initialization of AppMetrica using the Activate() method of the YandexMetricaImplementation class. This method also performs registration of the IYandexMetrica interface implementation.

    public static void Activate(Context context, string apiKey, Application app
    [no-highlight[

    Default value

    null

    Description

    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

    ]no-highlight]
    = null) {...}
    ParameterDefault valueDescription
    apiKey A unique application identifier that is issued in the AppMetrica web interface during app registration.
    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

    public static void Activate(string apiKey) {...}
    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);
        }
    }
}

The YandexMetricaAndroid and YandexMetricaPCL libraries are connected. Activation must be performed on app start. Then you can use the general YandexMetricaPCL interface, or work directly with YandexMetricaAndroid.

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using YandexMetricaPCL;

namespace Metrica.Xamarin.CrossPlatform.Droid
{
    [Activity(Label = "Metrica.Xamarin.CrossPlatform", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : Activity
    {
        int count = 1;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Init Android AppMetrica directly
            YandexMetricaAndroid.YandexMetricaImplementation.Activate(this, "API key", this.Application);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.myButton);

            button.Click += delegate {
                button.Text = string.Format("{0} clicks!", count);

                var dict = new Dictionary<string, string> { { "click", count.ToString() } };
                YandexMetrica.Implementation.ReportEvent("Click from Android", dict);

                SharedLogic.LogClick(count);

                ++count;
            };
        }
    }
}

The YandexMetricaIOS and YandexMetricaPCL libraries are connected. Activation must be performed on app start. Then you can use the general YandexMetricaPCL interface, or work directly with YandexMetricaIOS.

using YandexMetricaPCL;
using System.Collections.Generic;

namespace Metrica.Xamarin.CrossPlatform.iOS
{
    public partial class ViewController : UIViewController
    {
        int count = 1;
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Init iOS AppMetrica directly
            YandexMetricaIOS.YandexMetricaImplementation.Activate("APIkey");

            // Perform any additional setup after loading the view, typically from a nib.
            Button.AccessibilityIdentifier = "myButton";
            Button.TouchUpInside += delegate {
                var title = string.Format("{0} clicks!", count);
                Button.SetTitle(title, UIControlState.Normal);

                var dict = new Dictionary<string, string> { { "click", count.ToString() } };
                YandexMetrica.Implementation.ReportEvent("Click from iOS", dict);

                SharedLogic.LogClick(count);

                ++count;
            };
        }
    }
}

API methods

The plugin is accessed using the methods of the IYandexMetrica interface.

Event tracking
void ReportEvent(string message);
void ReportEvent(string message, IDictionary<string, string> parameters);
void ReportError(string message, Exception exception);
AppMetrica settings
void SetLocation(float latitude, float longitude);
void SetTrackLocationEnabled (bool enabled);
void SetCustomAppVersion(string appVersion);
void SetEnvironmentValue(string key, string value);
void SetSessionTimeout (uint sessionTimeoutSeconds);
void SetReportCrashesEnabled (bool enabled);
void SetLoggingEnabled();

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.