Overview

With HUAWEI 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 HMS Core 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

  1. Open the build.gradle file in the root directory of your Android Studio project.
  2. Add the AppGallery Connect plugin and the Maven repository address.
    • Go to buildscript > repositories and configure the Maven repository address for the HMS Core SDK.
    • Go to allprojects > repositories and configure the Maven repository address for the HMS Core SDK.
    • If the agconnect-services.json file has been added to the app, go to buildscript > dependencies and add the AppGallery Connect plugin configuration.
      buildscript { repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } dependencies { ... // Add the AppGallery Connect plugin configuration. classpath 'com.huawei.agconnect:agcp:1.4.2.300' } } allprojects { repositories { google() jcenter() // Configure the Maven repository address for the HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } }

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:hianalytics:{version}'
    implementation ‘com.huawei.hms:dtm-api:{version}'

Synchronizing the Project

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

Configuring Obfuscation Scripts

  1. If your project configuration meets all of the following conditions, do not configure the obfuscation configuration file:
    1. The integrated SDK version is 5.0.4.300 or later.
    2. 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'.
    3. 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.
    1. 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.
    2. Add configurations to exclude Analytics Kit and DTM from obfuscation.
    3. -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 ; } -keep public class androidx.recyclerview.widget.RecyclerView$LayoutManager{ public ; } -keep public class android.support.v7.widget.RecyclerView{ public ; } -keep public class android.support.v7.widget.RecyclerView$LayoutManager{ public ; }
      If you are using AndResGuard, add its trustlist to the obfuscation configuration file.
      "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. Click Configure on the Variable tab page.
  2. On the Configure preset variable page displayed, select the preset variable to be created and click OK.

Creating a Condition

  1. Click Create on the Condition tab page.
  2. Set relevant parameters and click Save to create a condition.

Creating a Tag

  1. Click Create on the Tag tab page.
  2. Set relevant parameters and click Save to create a tag.

Creating a Version

  1. Click Create on the Version tab page.
  2. In the Create version dialog box displayed, set Name and Description, 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 on the Version details page, and then click OK.

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); // Obtain the HiAnalyticsInstance instance. HiAnalyticsInstance instance = HiAnalytics.getInstance(this); HiAnalyticsTools.enableLog(); // Add visual events for reporting. // Set the custom event name to 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 name to 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 demo app 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.
    The demo app reports the custom event Purchase. Since Trigger condition is set to all_event and Operation to Modify event in the tag, the custom event will trigger event modification and the from_dtm parameter configured under Parameters for addition or modification will be added in the modified event. Therefore, you can view the modified event on the HUAWEI Analytics service page.

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

已复制代码