目录

Example

Function Call

A custom variable of the Function call type can call a class method of an app and pass relevant key values. For example, the event for buying a product in a shopping app contains only the product name. To report the product inventory in this event, perform the following steps:

  1. Implement the class for obtaining the product inventory. Assume that the shopping app has a common class Inventory.java (create it if it does not exist) for obtaining the product inventory, and the query(String goodName) method is called to obtain the product inventory based on the passed product name. Then, add the following code to your app to implement com.huawei.hms.dtm.ICustomVariable, and call the Inventory class using the getValue method to obtain the product inventory.
    package com.example.dtm;
    
    import com.huawei.hms.dtm.ICustomVariable;
    import java.util.Map;
    import com.example.goods.Inventory;
    
    public class InventoryDTMVariable implements ICustomVariable {
        @Override
        public String getValue(Map<String, Object> params) {
            if (params == null) {
                return "";    
            } else {
                return new Inventory().query(params.get("goodName"));
            }
        }
    }
  2. Create a custom variable of the Event parameter type, which is used to obtain the name of the bought product.

  3. Create a custom variable of the Function call type, which is used to obtain the product inventory.