Creating a Remote Configuration Experiment Using A/B Testing
Tags: android, abtesting, huawei
Published: 2022-03-04 09:56:40

A/B Testing allows you to run notifications experiments or Remote Configuration experiments, create A/B tests that engage with different audiences to compare your variants, and measure the aspects of your business that matter most to you, so that you can make informed decisions at all time.
	
Let’s take a look at how this process works in practice.

**1. Integrating the Remote Configuration SDK and Setting the Default Parameter Value**

a) Configure the Maven repository address in the project-level **build.gradle** file.

```
buildscript {
    repositories {
        maven { url 'https://developer.huawei.com/repo/'}
    }
    dependencies {
        classpath 'com.huawei.agconnect:agcp:1.5.2.300'
    }
}
allprojects {
    repositories {
        maven { url 'https://developer.huawei.com/repo/'}
    }
}
```


b) Add the AppGallery Connect plugin and the JSON configuration file.
Add the AppGallery Connect plugin to the app-level build.gradle file.

```
apply plugin: 'com.huawei.agconnect'
```

Sign in to AppGallery Connect, click **My projects**, click your project card, and go to **Project settings > General information**. Download the JSON configuration file and add it to the **app** directory of your project.

![Image 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ywi5vrgsx0dxpvdu4oif.png)
 
C) Add the SDK dependency to the app-level **build.gradle** file.

```
dependencies {
implementation 'com.huawei.agconnect:agconnect-remoteconfig:1.5.2.300'
implementation 'com.huawei.hms:hianalytics:5.3.1.300'
}
```

d) Set the default parameter value.
You can create a button for obtaining the updated parameter value from Remote Configuration.

Configure the default parameter value in the button, and apply the **fetch** method to obtain the updated parameter value from Remote Configuration. Set the following code:

```
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 HiAnalyticsTools.enableLog();
 HiAnalyticsInstance instance = HiAnalytics.getInstance(this);
 getAAID();
 findViewById(R.id.button).setOnClickListener(view -> {
  getRemoteCongfig();
 });

}
public void getRemoteCongfig() {
 // Obtain the Remote Configuration instance.
 config = AGConnectConfig.getInstance();
 Map<String, Object> defaultValue = new HashMap<>();
 // Add a key-value pair.
 defaultValue.put("welcome_string", "this is a default welcome_slogan");
 config.applyDefault(defaultValue);

 config.fetch(10).addOnSuccessListener(configValues -> {
  config.apply(configValues);
  String newSlogan = config.getValueAsString("welcome_string");
  Log.i(TAG, "RemoteConfig Success: " + newSlogan);
 }).addOnFailureListener(e1 ->
   Log.e(TAG, "getRemoteConfig failed: " + e1.getMessage())
 );
}
```

**2. Integrating Analytics Kit**

To generate reports for A/B tests, HUAWEI Analytics will need to be enabled, since it automatically captures A/B Testing events.

a) Add the SDK dependency to the app-level **build.gradle** file.

```
dependencies {
implementation 'com.huawei.hms:hianalytics:5.3.1.300'
}
```

b) Initialize HUAWEI Analytics in **onCreate**.

```
HiAnalyticsTools.enableLog();
HiAnalyticsInstance instance = HiAnalytics.getInstance(this);
```

**3. Setting the Parameter Value in Remote Configuration**

Sign in to [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/), click **My projects**, click the project card, and select your app. Go to **Grow > Remote Configuration**, and click **New parameter**.

![Image 8](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1l86h473e81rwrwq13qg.png)

Note: Make sure that the parameter name is the same as that of the default parameter.

Save the parameter settings and click **Release**.

**4. Creating a Remote Configuration Experiment**

In [AppGallery Connect](https://developer.huawei.com/consumer/en/service/josp/agc/index.html#/), click **My projects**, click the project card, and select your app. Go to **Grow > A/B Testing**. On the **A/B Testing** page, select **Remote configuration** from the **New experiment** drop-down list.

Configure a higher user percentage for **Target users**, and skip **Activation event**.

Set **Conditions** to **Language**, and select the languages as needed.

![Image 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arv0erlya1h6cymev9bv.png)
 
Configure **Parameter** for **Treatment & control groups** as follows.

![Image 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wygxbs13pwfvsin0xy7v.png)

Set **Indicators to track**, for example, to **Click-through rate**. Click **Save** to save all configurations.

**5. Starting Your Experiment and Fetching the Parameter Value from Remote Configuration**

a) Click **Start** under the **Operation** column of the record to run the experiment in AppGallery Connect.

![Image 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iskqvp0ugenrsbe8cveo.png)
 
b) Run your app, and click the button to fetch the parameter value from Remote Configuration.
The log is as follows:

![Image 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpkbjwctm0a0aadjov87.png)

Pay attention to the following:

- You can click Test under the Operation column of the experiment record. The anonymous application identifier (AAID) of the test device can be obtained as follows:

```
public void getAAID() {
 Task<AAIDResult> idResult = HmsInstanceId.getInstance(this).getAAID();
 idResult.addOnSuccessListener(new OnSuccessListener<AAIDResult>() {
  @Override
  public void onSuccess(AAIDResult aaidResult) {
   // Obtained the AAID successfully.
   String aaid = aaidResult.getId();
   Log.d(TAG, "getAAID successfully, aaid is " + aaid );
  }
 }).addOnFailureListener(new OnFailureListener() {
  @Override
  public void onFailure(Exception myException) {
   // Failed to obtain the AAID.
   Log.d(TAG, "getAAID failed, catch exceptio : " + myException);
  }
 });
}
```

![Image 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ro1cnciguoic56osfaa.png)
 
**6. Viewing the Experiment Report**

Click **View report** under the **Operation** column of a record to view the details for the experiment.

![Image 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a5y1c4yg9vqsewhqrjwz.png)

References

- [Service introduction of A/B Testing](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-abtest-introduction-0000001058210679)

- [Get started tutorials for Remote Configuration](https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-remoteconfig-android-getstarted-0000001056347165)

- [Get started tutorials of HUAWEI Analytics](https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/android-dev-process-0000001050163813)