Getting Started with Metrix SDK for Unity
This guide will walk you through the process of setting up and implementing the Metrix SDK in your Unity project. By integrating this SDK, you can easily collect user and behavioral data from your game or application and leverage Metrix’s powerful analytics and marketing capabilities.
Sample Project: For a complete and working example of Metrix SDK implementation in Unity, you can explore our sample project on GitHub .
1. Adding the Metrix Plugin to Your Unity Project
To begin, you need to import the Metrix SDK plugin into your Unity project:
- Download the Plugin: Get the latest version of the Metrix Unity plugin from this link .
- Import into Project: After downloading, either double-click the
MetrixSDK_UnityPlugin_vX.X.X.unitypackagefile or drag it into the Project window in Unity to import the plugin into your project.
2. Dependency Management
The Metrix plugin offers two main methods for managing its dependencies: using External Dependency Manager for Unity (EDM4U) (the automatic and recommended method) or manually adding dependencies to your project’s Gradle file (an alternative method).
2.1. Recommended Method: Using External Dependency Manager (EDM4U)
This tool (formerly known as “Unity Jar Resolver”) automatically downloads and adds the necessary libraries to your project.
- Install EDM4U: If you don’t have this tool in your project, download the latest version from this link and import it into your project as well.
2.2. Alternative Method: Manually Adding Dependencies to Gradle (Android Only)
If you prefer to manage dependencies manually or aren’t using EDM4U, you can directly add Metrix dependencies to your Android project’s build.gradle file. This method applies only to Android builds.
-
Configure
build.gradle: Ensure that themaven { url 'https://jitpack.io' }repository is added to your project-levelbuild.gradlefile within theallprojects { repositories { ... } }section.allprojects { repositories { google() mavenCentral() maven { url 'https://jitpack.io' } // Add this line } } -
Add Dependencies: Add the following dependencies to your module-level
build.gradlefile (typicallyapp/build.gradle) within thedependencies { ... }section:dependencies { implementation 'ir.metrix.analytics:metrix-unity:2.6.7' implementation 'ir.metrix.attribution:metrix-unity:2.6.7' implementation 'ir.metrix.notification:metrix-unity:2.6.7' }
3. Initializing the Metrix Library in Your Application
The initial setup steps vary slightly depending on your application’s target platform (Android or iOS).
3.1. Setup for Android Applications
- Resolve Dependencies:
- If you’re using EDM4U:
- In Unity, go to:
Assets->External Dependency Manager->Android Resolver->Resolveto resolve Android dependencies once.
- In Unity, go to:
- If you’ve added dependencies manually:
- You don’t need to run
Android Resolver. Unity and Gradle will manage dependencies automatically during the project build.
- You don’t need to run
- Configure
AndroidManifest.xml: Place your MetrixAPP_IDandAPI_KEYin your application’sAndroidManifest.xmlfile. These keys are essential for identifying your app within Metrix.
-
File Location: The
AndroidManifest.xmlfile is typically located inAssets/Plugins/Android. -
Enable Custom Manifest: If you don’t see this file, you might need to enable “Custom Main Manifest” in Unity’s Player settings: Go to
Edit->Project Settings...->Player->Android Setting(Tab) ->Publishing Settings-> EnableCustom Main Manifest. -
Add Keys: Add the following lines within the
<application>tag in yourAndroidManifest.xmlfile, replacingYOUR_APP_IDandYOUR_API_KEYwith your actual keys:<manifest> ... <application> ... <meta-data android:name="ir.metrix.APPLICATION_ID" android:value="YOUR_APP_ID" /> <meta-data android:name="ir.metrix.API_KEY" android:value="YOUR_API_KEY" /> </application> </manifest> -
YOUR_APP_ID: Your Application ID, obtained from the Metrix dashboard. -
YOUR_API_KEY: Your API Key, obtained from the Metrix dashboard.
-
Verify Permission for Advertising ID: Ensure that the
com.google.android.gms.permission.AD_IDpermission is present in your project’s final (merged) manifest. This permission is crucial for collecting the device’s Advertising ID and for correct Attribution functionality:<manifest> <uses-permission android:name="com.google.android.gms.permission.AD_ID"/> ... </manifest>
3.2. Setup for iOS Applications
- Initialize Metrix:
At the beginning of your application’s execution (e.g., in the
AwakeorStartmethod of a MonoBehaviour placed in your game’s initial scene), call theInitializemethod of the Metrix library. This initializes the Metrix SDK.
-
APP_ID: Your Application ID, obtained from the Metrix dashboard.using MetrixSDK; using UnityEngine; public class MetrixInitializer : MonoBehaviour { void Awake() { Metrix.Initialize("YOUR_APP_ID"); // Replace YOUR_APP_ID with your actual App ID from Metrix dashboard } }
- Manage Dependencies with CocoaPods: The Metrix library uses CocoaPods for dependency management in iOS projects. After exporting your Unity project as an Xcode project:
-
In your terminal, navigate to your Xcode project folder (
<your-iOS-project-directory>). -
Run the pod install command:
cd <your-iOS-project-directory> pod installThis command will install all necessary dependencies for your iOS project.
4. Enhancing Library Security with SDK Signature (Android Only)
You can enhance communication and data transfer security and further ensure the integrity of your application’s statistics by enabling the SDK Signature feature in the Metrix dashboard. This feature allows Metrix to verify the authenticity of data sent from your application.
After enabling SDK Signature in your Metrix dashboard, retrieve the corresponding signature ID from the Encoded column. Then, place this signature in your C# Unity code as follows (either before or immediately after calling Metrix.Initialize):
Metrix.setSignature("YOUR_SIGNATURE_FROM_METRIX_DASHBOARD");Note: This feature is currently available only for the Android platform.