Skip to main content

Integrate using the Unity SDK


This guide demonstrates how to implement data tracking features into a Unity app using the App Tracking Unity SDK. After implementation, you can track your app's performance metrics, such as retention, playtime sessions length, as well as daily active users (DAU) and monthly active users (MAU).

Before you start

Before you start integrating your app with App Tracking, make sure you have access and an API key to the service.

Install the SDK

  1. Click the link to download the Unity package for the SDK: Mavis Analytics Unity package.

  2. Import the package either by drag-and-dropping the file to your assets folder or by going to Assets > Import Package > Custom Package.

  3. Generate a config file by clicking MavisAnalyticsConfig > Mavis Settings.

  4. Paste your API key and click Sync Settings.

Initialize the SDK

Drag and drop the MavisAnalytics prefab to your First Scene. This handles most of the initialisation.

Track events

You can track events by using the MavisAnalytics class anywhere in your script.

  1. Add using MavisAnalyticsSDK; at the top of the script.

  2. To set a UserID, call the SetUserId method with a user ID as an argument:

    MavisAnalytics.SetUserId(string: “<user_id>”);

    Every time an event is sent, this automatically adds the user ID to the event's properties. The user ID is the one from the Sky Mavis Account service if you have it integrated. Otherwise, it's the user ID in your app's internal authentication system.

  3. To track a custom event, call the TrackEvent method with two event arguments:

    MavisAnalytics.TrackEvent(string: "<eventName>", string: "<refEventName>"); 

    refEventName is the name of the reference event used when you want to group events together. For example:

    MavisAnalytics.TrackEvent(string: "level_completed", string: "game_loop");
  4. To track custom events with free-form data or metadata for additional information, such as the level number whether the game is won or lost, call TrackEvent with two event arguments and an additional metadata object:

    MavisAnalytics.TrackEvent(string: “level_completed”, string: "progression", object metadata);

    This example shows how to set up an object metadata:

    Dictionary<string, object> metadata = new Dictionary<string, object>()
    { "level", 1 },
    { "won", false },
    { "items" , new List<string>(){"weapon1","weapon2" } }
    MavisAnalytics.TrackEvent("<eventName>", "<refEventName", metadata);

You can now track your game's performance in the App Tracking dashboard.

Was this helpful?
Happy React is loading...