Skip to content

Custom Tag Usage

Build Configuration Method

  1. Create multiple productFlavors in build.gradle to differentiate tags.

    android{
        //…
        productFlavors {
            prodTest {
                buildConfigField "String", "CUSTOM_VALUE", "\"Custom Test Value\""
                //…
            }
            prodPublish {
                buildConfigField "String", "CUSTOM_VALUE", "\"Custom Publish Value\""
                //…
            }
        }
    }
    
  2. Add the corresponding BuildConfig constant in the RUM configuration.

    FTSdk.initRUMWithConfig(
            new FTRUMConfig()
                .addGlobalContext(CUSTOM_STATIC_TAG, BuildConfig.CUSTOM_VALUE)
                //... Add other configuration
    );
    
    FTSdk.initRUMWithConfig(
                FTRUMConfig()
                    .addGlobalContext(CUSTOM_STATIC_TAG, BuildConfig.CUSTOM_VALUE)
                    //… Add other configuration
            )
    

Runtime File Read/Write Method

  1. Store data in file-based storage, such as SharedPreferences, configure the SDK to use it, and add code at the configuration location to retrieve the tag data.

    SharedPreferences sp = context.getSharedPreferences(SP_STORE_DATA, MODE_PRIVATE);
    String customDynamicValue = sp.getString(CUSTOM_DYNAMIC_TAG, "not set");
    
    // Configure RUM
    FTSdk.initRUMWithConfig(
         new FTRUMConfig().addGlobalContext(CUSTOM_DYNAMIC_TAG, customDynamicValue)
         //… Add other configuration
    );
    
    val sp = context.getSharedPreferences(SP_STORE_DATA, MODE_PRIVATE)
    val customDynamicValue = sp.getString(CUSTOM_DYNAMIC_TAG, "not set")
    
    // Configure RUM
    FTSdk.initRUMWithConfig(
         FTRUMConfig().addGlobalContext(CUSTOM_DYNAMIC_TAG, customDynamicValue!!)
         //… Add other configuration
    )
    
  2. Add a method for modifying the file data when needed.

    public void setDynamicParams(Context context, String value) {
        SharedPreferences sp = context.getSharedPreferences(SP_STORE_DATA, MODE_PRIVATE);
        sp.edit().putString(CUSTOM_DYNAMIC_TAG, value).apply();
    }
    
    fun setDynamicParams(context: Context, value: String) {
                val sp = context.getSharedPreferences(SP_STORE_DATA, MODE_PRIVATE)
                sp.edit().putString(CUSTOM_DYNAMIC_TAG, value).apply()
    
            }
    
  3. Finally, restart the app. For details, see SDK Demo

Add at SDK Runtime

After the SDK is initialized, use FTSdk.appendGlobalContext(globalContext), FTSdk.appendRUMGlobalContext(globalContext), and FTSdk.appendLogGlobalContext(globalContext) to dynamically add tags. The settings take effect immediately after they are configured. After that, data subsequently reported by RUM or Log will automatically carry the corresponding tags.

This usage is suitable for scenarios where data is obtained with a delay, for example, when tag data is only available after login, an API request, or local asynchronous initialization.

Warning

Avoid using the same key as globalContext in the SDK initialization configuration or as an SDK built-in field; when the names are the same, static tags in the initialization configuration take precedence over dynamically added tags.

Usage

/**
 * Dynamically set global tag
 * @param globalContext
 */
public static void appendGlobalContext(HashMap<String,Object> globalContext)

/**
 * Dynamically set RUM global tag
 * @param globalContext
 */
public static void appendRUMGlobalContext(HashMap<String,Object> globalContext)

/**
 * Dynamically set log global tag
 * @param globalContext
 */
public static void appendLogGlobalContext(HashMap<String,Object> globalContext)
/**
 * Dynamically set global tag
 * @param globalContext
 */
fun appendGlobalContext(globalContext: HashMap<String, Any>)

/**
 * Dynamically set RUM global tag
 * @param globalContext
 */
fun appendRUMGlobalContext(globalContext: HashMap<String, Any>)

/**
 * Dynamically set log global tag
 * @param globalContext
 */
fun appendLogGlobalContext(globalContext: HashMap<String, Any>)
//SDK initialization pseudocode: set tags after obtaining parameters from the network

FTSdk.init() 

getInfoFromNet(info){
    HashMap<String, Object> globalContext = new HashMap<>();
    globalContext.put("delay_key", info.value);
    FTSdk.appendGlobalContext(globalContext)
}

Code Example

HashMap<String, Object> globalContext = new HashMap<>();
globalContext.put("global_key", "global_value");
FTSdk.appendGlobalContext(globalContext);

HashMap<String, Object> rumGlobalContext = new HashMap<>();
rumGlobalContext.put("rum_key", "rum_value");
FTSdk.appendRUMGlobalContext(rumGlobalContext);

HashMap<String, Object> logGlobalContext = new HashMap<>();
logGlobalContext.put("log_key", "log_value");
FTSdk.appendLogGlobalContext(logGlobalContext);
val globalContext = hashMapOf<String, Any>(
    "global_key" to "global_value"
)
FTSdk.appendGlobalContext(globalContext)

val rumGlobalContext = hashMapOf<String, Any>(
    "rum_key" to "rum_value"
)
FTSdk.appendRUMGlobalContext(rumGlobalContext)

val logGlobalContext = hashMapOf<String, Any>(
    "log_key" to "log_value"
)
FTSdk.appendLogGlobalContext(logGlobalContext)

Feedback

Is this page helpful?