Overview

With Dynamic Tag Manager (DTM), you can deploy and update your tag configuration easily and securely on a web-based UI, and realize data-driven operations based on powerful analysis capabilities of HUAWEI Analytics.

What You Will Create

In this codelab, you will use the created demo project to call open APIs of DTM. Through the demo project, you will:

What You Will Learn

In this codelab, you will learn how to:

Hardware Requirements

Software Requirements

Required Knowledge

To integrate DTM, you must complete the following preparations:

  1. Create an app in AppGallery Connect.
  2. Create an Android Studio project.
  3. Add the app package name and save the configuration file.
  4. Add the AppGallery Connect plugin and the Maven repository in the project-level build.gradle file.
  5. Configure the signing certificate in Android Studio.
For details, please refer to Integrating the SDK.

Adding the AppGallery Connect Configuration File of Your App

If you are using Android Studio, you can integrate the HMS Core SDK via the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio project first.
To integrate the SDK with the remote Maven repository, perform the following steps:

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your app project and click the app that needs to integrate the HMS Core SDK.
  3. Go to Project settings > General information. In the App information area, download the agconnect-services.json file.
  4. Copy the agconnect-services.json file to the app directory of your Android Studio project.

Configuring the Maven Repository Address for the HMS Core SDK

The procedure for configuring the Maven repository address in Android Studio is different for Gradle plugin earlier than 7.0, Gradle plugin 7.0, and Gradle plugin 7.1 or later. Click a relevant link below to find the configuration procedure for the specific Gradle plugin version.

Gradle plugin earlier than 7.0

Gradle plugin 7.0

Gradle plugin 7.1 or later

Adding Build Dependencies

  1. Open the build.gradle file in the app directory of your project.
  2. Add the following information under apply plugin: ‘com.android.application' in the file header:
    apply plugin: 'com.huawei.agconnect'
  3. Add build dependencies in the dependencies block.
    implementation 'com.huawei.hms:hia***{version}***{version}' implementation 'com.huawei.hms***{version}***{version}'

Synchronizing the Project

After completing the preceding configuration, click the synchronization icon on the toolbar to synchronize the project with Gradle files.

Configuring Obfuscation Scripts

  1. If your project configuration meets all of the following conditions, do not configure the obfuscation configuration file:
    • The integrated SDK version is 5.0.4.300 or later.
    • In the project-level build.gradle file, the Android Studio gradle plugin version is 3.4.0 or later, for example, classpath ‘com.android.tools.build:gradle:3.4.0'.
    • In Gradle plugin versions earlier than 5.0, the R8 code obfuscation is not disabled, or android.enableR8=true is configured in the gradle.properties file in your project. Ignore this configuration if you use Gradle plugin 5.0 or later.
  2. If any of the previous conditions is not met, configure the obfuscation configuration file before building the APK to prevent Analytics Kit and DTM from being obfuscated.
    a) Open the proguard-rules.pro file in the app directory of your Android Studio project or proguard-project.txt in the app directory of your Eclipse project.
    b) Add configurations to exclude Analytics Kit and DTM from obfuscation.
    -ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.huawei.hianalytics.**{*;} -keep class com.huawei.updatesdk.**{*;} -keep class com.huawei.hms.**{*;} -keep public class androidx.recyclerview.widget.RecyclerView{ public <methods>; } -keep public class androidx.recyclerview.widget.RecyclerView$LayoutManager{ public <methods>; } -keep public class android.support.v7.widget.RecyclerView{ public <methods>; } -keep public class android.support.v7.widget.RecyclerView$LayoutManager{ public <methods>; }

    If you are using AndResGuard, add its trustlist to the app-level build.gradle file of your project.

    "R.string.hms*", "R.string.connect_server_fail_prompt_toast", "R.string.getting_message_fail_prompt_toast", "R.string.no_available_network_prompt_toast", "R.string.third_app_*", "R.string.upsdk_*", "R.layout.hms*", "R.layout.upsdk_*", "R.drawable.upsdk*", "R.color.upsdk*", "R.dimen.upsdk*", "R.style.upsdk*", "R.string.agc*"

Implementing the Demo App

Enabling Required Services

  1. Sign in to AppGallery Connect and click My projects.
  2. Find your app project, and click the desired app name.
  3. Go to Grow > Dynamic Tag Manager. On the page displayed, click Enable HUAWEI Analytics to enable the HUAWEI Analytics service.
  4. If you have enabled the HUAWEI Analytics service, click Enable Dynamic Tag Manager. In the dialog box displayed, enter a configuration name, set Operation record, and click OK to create a configuration.

Creating a Variable

  1. Go to Workspace > Variable, and click Configure.
  2. On the Configure preset variable page displayed, select the preset variable to be created and click OK.

Creating a Condition

  1. Go to Workspace > Condition, and click Create.
  2. Set relevant parameters and click Save to create a condition.

Creating a Tag

  1. Go to Workspace > Tag, and click Create.
  2. Set relevant parameters and click Save to create a tag.

Creating a Version

  1. Click the Workspace tab and then click Create version.
  2. On the Create version page displayed, enter a version name and description, select Create version or Create and release version, and click OK.

Releasing a Version

  1. On the Version tab page, click a version name to go to the Version details page.
  2. Click Release, and then click OK on the Release page displayed.

Reporting Custom Events

Implement the following processes in MainActivity.java of the demo app: obtain the HiAnalyticsInstance instance, customize events, and add visual events for reporting.
Java

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Ob**HiAnalyticsInstance**sInstance instance. HiAnalyticsInstance instance = HiAnalytics.getInstance(this); HiAnalyticsTools.enableLog(); // Add visual events for reporting. // Set the custom event**Purchase** Purchase. String eventName = "Purchase"; // Customize event parameters. Bundle bundle = new Bundle(); bundle.putDouble("price", 999); bundle.putLong("quantity", 100L); bundle.putString("currency", "CNY"); // Report events. if (instance != null) { instance.onEvent(eventName, bundle); } }

Kotlin

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val instance = HiAnalytics.getInstance(this) HiAnalyticsTools.enableLog() // Add visual events for reporting. // Set the custom event**Purchase** Purchase. val eventName = "Purchase" // Customize event parameters. val bundle = Bundle() bundle.apply { putDouble("price", 999.0) putLong("quantity", 100L) putString("currency", "CNY") } // Report events. instance.let { it.onEvent(eventName, bundle) } }

Viewing Reported Events

  1. Install the generated APK on the mobile phone.
  2. In the CLI on your PC or Terminal in Android Studio, run the adb shell setprop debug.huawei.hms.analytics.app <packageName> command to enable the debug mode, launch the demo app, and go to the screen for which you have customized tracking events to complete event reporting.
  3. Sign in to AppGallery Connect and click My projects. Find your project, and click the app for which you want to view analytics data. Go to HUAWEI Analytics > App debugging.
    After you complete operations in Reporting Custom Events, Creating a Condition, and Creating a Tag, the custom event will trigger event modification to add the from_dtm parameter configured in Parameters for addition or modification during tag creation to the event. You can view the reported events in the app debugging page on HUAWEI Analytics.

Well done. You have successfully completed this codelab and learned how to:

For more information, please click the following link:

Related Documents
You can click the button below to download the source code:

Sample Code for Android Projects

Code copied