Getting Started
You can view the sample Metrix implementation project here .
Installation and Setup
By following the steps below, the Metrix library will be activated and ready for use in your application:
- In your application’s
build.gradlefile, ensure thatcompileSdkVersionis greater than or equal to 31.
android {
compileSdkVersion 32 // >= 31
// ...
}- Add the
mavenCentralrepository to your project’sbuild.gradlefile in theallprojectssection:
allprojects {
repositories {
// ...
mavenCentral()
}
}- Add the Metrix library dependency to the
dependenciessection of your application’sbuild.gradlefile (add the appropriate library based on whether you’re using analytics or attribution features):
implementation 'ir.metrix.attribution:metrix:2.6.7'
implementation 'ir.metrix.analytics:metrix:2.6.7'
implementation 'ir.metrix.notification:metrix:2.6.7' // (Optional)- Place the Metrix application ID (
app id) and API key (api key) in your application’sAndroidManifest.xmlfile:
<manifest>
...
<application>
...
<!-- Add the following lines and replace YOUR_APP_ID -->
<meta-data
android:name="ir.metrix.APPLICATION_ID"
android:value="YOUR_APP_ID" />
<!-- Add the following lines and replace YOUR_API_KEY -->
<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 panel.
YOUR_API_KEY: Your API key obtained from the Metrix panel.
Note: Ensure this permission is included in the project’s final merged manifest.
<manifest>
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
...
</manifest>Note: If you are using Gradle version 8 or higher, add the following code to your ProGuard file:
-keep class ir.metrix.** { *; }
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }Optional Steps
Enhancing Library Security
You should increase the security of communication and data transfer, as well as ensure greater integrity of your application’s statistics, by enabling the SDK signature feature in your panel and obtaining your specific identifier.
After enabling SDK Signature in your panel, retrieve the corresponding signature identifier from the Encoded column and place it in your application as follows:
MetrixAttribution.setSignature("YOUR_SIGNATURE");Reducing Library Size
To reduce the APK size, add the ‘packaging’ tag to the build.gradle file in the app folder:
android {
packaging {
jniLibs {
excludes += "/lib/**/libconscrypt_jni.so"
}
}
}Disabling the Library During Development
You can disable the Metrix library during development and testing, or generally in specific application builds based on your needs. This way, no statistics regarding installations or potential events from these builds will be recorded in Metrix.
To do this, simply add the following metadata to your application’s AndroidManifest.xml file:
<manifest>
...
<application>
...
<!-- Add the following lines -->
<meta-data
android:name="metrix_developer_mode"
android:value="true" />
</application>
</manifest>Additionally, using manifestPlaceholders in Android, you can manage this value in your application’s gradle file for different build variants.
In the following example, the Metrix library will be disabled in the “develop” variant and enabled in the “release” variant:
<manifest>
...
<application>
...
<meta-data
android:name="metrix_developer_mode"
android:value="${metrixDisabled}" />
</application>
</manifest>android {
// ...
buildTypes {
debug {
//...
manifestPlaceholders = [metrixDisabled: "true"]
}
release {
//...
manifestPlaceholders = [metrixDisabled: "false"]
}
}
}Implementation in WebView
The implementation of Metrix in WebView is based on the Android Metrix SDK. All Metrix activities are performed at the native level, and web content communicates with the Android SDK through a Bridge.
After completing the initial steps:
- Call
webView.getSettings().setJavaScriptEnabled(true)to enablejavascriptin thewebview. - Call
MetrixBridge.registerAndGetInstance(webview)to activate the Metrix interface between the library and thewebview. - If you need to change the
webview, you can callMetrixBridge.setWebView(newWebview). - To disable the interface, call
MetrixBridge.unregister().
After completing these steps, your Activity class will resemble the following code snippet:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
MetrixAnalyticsBridge.registerAndGetInstance(webview);
MetrixAttributionBridge.registerAndGetInstance(webview);
try {
webView.loadUrl("your webview content");
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
MetrixAnalyticsBridge.unregister();
MetrixAttributionBridge.unregister();
super.onDestroy();
}
}This way, the Metrix library has been successfully activated in your application, and the Metrix Javascript interface will serve as the communication bridge between the Metrix library and your loaded pages in the webview.
To interact with the Metrix library and use its features, such as sending events, import the Metrix Javascript file in your HTML file. The file is located in the assets folder and can be viewed in the sample implementation . Import it as follows:
<script type="text/javascript" src="metrix_analytics.js"></script>
<script type="text/javascript" src="metrix_attribution.js"></script>This will allow you to call methods of MetrixAnalytics or MetrixAttribution in your HTML file.