Introduction

With HUAWEI DTM, you can 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 HUAWEI DTM. Through the demo project, you will:

What You Will Learn

Hardware Requirements

Software Requirements

HUAWEI HMS Core integration requires the following preparations

For details, see the HUAWEI HMS Core Integration Preparation.

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

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

Add the AppGallery Connect configuration file of the app to your Android Studio.

1.Sign in to AppGallery Connect and select My apps.
2.Find your app from the list, and click the link under Android App in the Mobile phone column.

3.Go to Develop > Overview > App information.
4.Click agconnect-services.json to download the configuration file.

5.In Android Studio, switch to the Project view and move the agconnect-services.json file obtained from AppGallery Connect to the app directory in your Android project.

Add plug-in configuration and SDK version in the build.gradle file in the app directory.

1.Open the build.gradle file in the app directory

2.Configure build dependencies. Replace {version} with the actual SDK version number, for example
implementation 'com.huawei.hms:hianalytics:4.0.1.300' and
Implementation 'com.huawei.hms:dtm-api:4.0.2.300'

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

3.Add the AppGallery Connect plug-in dependency to end of the file.

apply plugin: 'com.huawei.agconnect'

Configure obfuscation scripts.

Before building the APK, configure obfuscation scripts to prevent HUAWEI Analytics Kit and DTM from being obfuscated. If obfuscation arises, HUAWEI Analytics and DTM may not function properly.

Open the obfuscation script file of your Android project.

Add configurations to exclude HUAWEI Analytics Kit 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 getUserProperty(java.lang.String); } -keep public class com.huawei.hms.dtm.EventFilter { public <fields>; public <methods>; }

If you have used AndResGuard, add it to the whitelist in the obfuscation script 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*"

Click Sync Now or Sync Project with Gradle Files to build a project. If an error occurs, check the network connection and the configurations in the build.gradle file.

Managing Tags Dynamically

HUAWEI Analytics is used as an example to describe how to create a configuration, variable, condition, and tag, and how to create and release a version. For details, please refer to Server Development in the Developer Guide.

Creating a Configuration

To use the DTM service in your app, you need to go to the Develop > Growing > Dynamic Tag Manager page and click Create configuration to create a configuration for the app.

In the dialog box that is displayed, enter a configuration name, set App type to Android and Operation records to six months, and click OK.

View the created configuration on the Configuration page.

Creating a Variable

Click a configuration name on the Configuration page, and click the Variable tab.

Click Configure on the right of Preset variables. In the Configure preset variable dialog box, select Event Name and click OK.

Click Create on the right of Custom variables. On the Create custom variable page, enter a variable name, set the variable type to Event parameter, and set the event parameter key to price. Click Save at the top of the page.

Repeat the preceding steps to create an event parameter whose key is currency.

Then, create an event parameter whose key is quantity.

View the created variable on the Variable page.

Creating a Condition

Click the Condition tab.

Click Create on the right. On the page that is displayed, enter a condition name, set Type to Custom and Trigger condition to Some events, and set the three parts of the trigger condition to Event Name, Equals, and Purchase, respectively. Click Save at the top of the page.

View the created condition on the Condition page.

Creating a Tag

Click the Tag tab.

Click Create. On the page that is displayed, enter a tag name, set Extension to Huawei Analytics, Operation to Add event, and Event name to HiPurchase, and click Add. Add three attributes whose keys are HiPrice, HiCurrency, and HiQuantity, respectively. Reference variables ep_price, ep_currency, and ep_quantity as the values of the three attributes, respectively.

After setting parameters, click Add under condition properties. In the dialog box that is displayed, select PurchaseEvents as the tag trigger condition.

Click Save at the top of the Create tag page to create the tag. You can view the created tag on the Tag page.

Creating a Group

Click Create on the Group page,On the Create group page, enter a group name and click OK.

View the created group on the Group page.

Creating a Version

Click the Version tab.

Click Create on the right. In the dialog box that is displayed, enter a version name and description, and click OK.

You can view the created version on the Version page.

Releasing a Version

On the Version page, click the name of the created version to go to the version details page.

Click Release. In the dialog box that is displayed, verify the version information and click OK. After the version is released successfully, the version details page will display the person who releases the version and the version release time.

Downloading a Version

On the version details page, click Download to download the configuration version. The default name of the downloaded file is DTM-***.json.

Implementing the Demo App

Configuration file import

Create the src\main\assets\containers\ directory and move the generated configuration file DTM-***.json to the directory.

Activity page

Implement the following processes in MainActivity of the demo app: obtain the HiAnalyticsInstance instance, customize events, and insert tracing points to report the custom events. The details are as follows:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); // Obtain the HiAnalyticsInstance instance. HiAnalyticsInstance instance = HiAnalytics.getInstance(this); HiAnalyticsTools.enableLog(); // Insert tracing points to report custom events. // 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); } }

Displaying Reported Events

Install the built demo app on a mobile phone, run the adb shell setprop debug.huawei.hms.analytics.app <packageName> command to enable the debug mode, and start the demo app to report events.
Register the demo app with HUAWEI Developer, go to Console > AppGallery > Analytics, click the demo app, go to Advanced analysis, and select a mobile phone to debug the demo app. You can view reported events on HUAWEI Analytics.
The demo app reports the custom event Purchase. The trigger condition configured in the dynamic tag configuration is as follows:

EventName="Purchase"

In addition, the operation type is set to Add event in the HUAWEI Analytics tag. Therefore, the custom event triggers the operation to add the HiPurchase event in the configuration. During tag execution, the values of price, quantity, and currency will be assigned to corresponding parameters in the HiPurchase event. Therefore, two events are reported and displayed on HUAWEI Analytics.

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

For more information, please click the following links:

You can download the source code in github.com/Huawei/Consumer/tree/master/Codelabs/DynamicTagManager

You can also click the button below to download the source code.

Download source code

Code copied