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

To integrate HUAWEI DTM, you must complete the following preparations:

For details, please refer to Preparations for Integrating HUAWEI HMS Core.

Ensure that you have enabled the HUAWEI Analytics service. For details, please refer to Enabling HUAWEI Analytics.

Integrating the HMS Core SDK in Your Android Studio Project

If you are using Android Studio, you can integrate the HMS Core SDK using the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio project first.
To remotely integrate the HMS Core SDK via the Maven repository, perform the following steps:
1. Add the AppGallery Connect configuration file of your app to Android Studio.
a) Sign in to AppGallery Connect and click My projects.
b) Find your app project and click the app that needs to integrate the HMS Core SDK.
c) Go to Project settings > General information. In the App information area, download the agconnect-services.json file.

d) Copy the agconnect-services.json file to the app's root directory of your Android Studio project.

2. Add the Maven repository address and AppGallery Connect service dependencies in the build.gradle file of your project.

buildscript { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } dependencies { classpath 'com.huawei.agconnect:agcp:1.4.1.300' } } allprojects { repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }

3. Add the plug-in configuration and SDK version in the build.gradle file in the app directory.
a) Open the build.gradle file in the app directory.

b) Add build dependencies. Replace {version} with the actual SDK version number, for example, implementation 'com.huawei.hms:hianalytics:5.0.1.300' and 'com.huawei.hms:dtm-api:5.1.0.300'.

implementation 'com.huawei.hms:hianalytics:{version}' implementation 'com.huawei.hms:dtm-api:{version}'

4. After completing the preceding configuration, click the synchronization icon on the toolbar to synchronize the build.gradle file.

Configuring Obfuscation Scripts

1. If your project configuration meets all of the following conditions, you do not need to configure the obfuscation configuration file:

a) The integrated SDK version is 5.0.4.300 or later.

b) 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'.

c) In Gradle plugin version 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, you need to configure the obfuscation configuration file before building the APK to prevent Analytics Kit and DTM from being obfuscated.
a) Open the obfuscation configuration file of your project. The configuration file is proguard-rules.pro for Android Studio and proguard-project.txt for Eclipse.
b) Add configurations to exclude HUAWEI Analytics and DTM from obfuscation.

-dontwarn com.huawei.hms.** -keep interface com.huawei.hms.analytics.type.HAEventType{*;} -keep interface com.huawei.hms.analytics.type.HAParamType{*;} -keep class com.huawei.hms.analytics.HiAnalyticsTools{ public static void enableLog(); public static void enableLog(int); } -keep class com.huawei.hms.analytics.HiAnalyticsInstance{*;} -keep class com.huawei.hms.analytics.HiAnalytics{*;} -keep class com.huawei.hms.feature.** { public *; } -keep public class com.huawei.hms.common.** { public *; } -keep class com.huawei.hms.analytics.internal.filter.EventFilter{ public void logFilteredEvent(java.lang.String, android.os.Bundle); public java.lang.String getUserProfile(java.lang.String); } -keep public class com.huawei.hms.dtm.EventFilter { public <fields>; public <methods>; }

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

Creating a Configuration

  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 Create configuration. In the dialog box displayed, enter a configuration name, set Operation record and Tracked data, and click OK to create a configuration.

Creating a Variable

  1. Click Configure on the Variable tab page.
  2. Select a preset variable, such as Event Name, 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. Enter a version name and description, and click OK to create a version.

Importing a Configuration File

  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 file of the demo app: obtain the HiAnalyticsInstance instance, customize events, and insert tracking code to report custom events. The details are as follows:

Viewing Reported Events

  1. Install the built demo app on a 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, and launch the demo app to report events.
  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 links:

Related documentation

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

Download source code

Code copied