菜单

ReactNative SDK

一、说明

1.功能概述

应用的文案或者运营策略更新经常需要通过发版解决,为了减少开发者频繁更新应用版本,热力引擎推出参数下发功能,通过参数下发可以随时远程调整应用中的参数值实现应用内容更新

2.适用范围

本文档适用于使用 ReactNative 来开发的移动端 APP 或产品。 本文档适用于 Android 5.0 及以上的安卓系统,及 iOS 11.0 及以上的 iOS 系统.  

二、接入SDK

由于参数下发SDK属于热力引擎SDK的一个插件,开发者在接入参数下发SDK时不需要再进行配置.

三、SDK 方法说明

1.初始化 SDK

参数下发SDK作为热力引擎SDK的一个插件,参数下发SDK初始化依赖于热力引擎SDK的初始化方法,具体初始化方法参考ReactNative 插件集成文档中的初始化方法


方法示例

export function initialize(appKey:string,options:SolarEngineInitiateOptions,completion: (result: InitiateCompletionInfo) => void) 


参数说明

remoteConfig参数说明

参数名称参数含义参数类型是否必传
enabled是否启用参数下发SDK,默认为关闭状态boolean
mergeTypeSDK配置合并策略,默认情况下服务端配置跟本地缓存配置合并,具体释义参考下方参数解释RemoteConfigMergeType
customIDProperties自定义ID,跟用户在后台设置的使用自定义ID匹配对应Object
customIDEventProperties自定义ID事件属性值,跟用户在后台设置的使用自定义ID事件属性值匹配对应,可选字段Object
customIDUserProperties自定义ID用户属性值,跟用户在后台设置的使用自定义ID用户属性值匹配对应,可选字段Object


RemoteConfigMergeType(合并策略)介绍

参数名称参数含义
Userapp首次启动或版本更新时, 使用服务端配置跟用户默认配置合并,此方法可以清除本地缓存配置
Cache使用服务端配置跟用户本地已有缓存配置合并,参数下发SDK默认合并策略

注:开发者使用参数下发SDK需要通过API预置一份默认配置配置到App中,SDK使用该默认配置用来兜底,具体方法参考设置默认配置


调用示例

async function Initiate(){
  log("Initiate" );

  let appKey = "";
  if (Platform.OS === 'ios') {
     appKey = "your_iOS_appKey";
  } else if (Platform.OS === 'android') {
     appKey = "your_Android_appKey";
  }    

  let remoteConfig:RemoteConfig = buildRemoteConfig();
  let initiateOptions:SolarEngineInitiateOptions = {
...
    remoteConfig:remoteConfig,
...
  }
  SolarEngine.initialize(appKey,initiateOptions,(result:InitiateCompletionInfo) => {
    if (result.success) {
      Alert.alert('SolarEngine SDK Initiate Complete!')
    }
  });
}
function buildRemoteConfig():RemoteConfig{
  let remoteConfig:RemoteConfig = {
    enabled:true,
    mergeType:RemoteConfigMergeType.Cache, 
    customIDProperties:{"name":"name_value"},
    customIDEventProperties:{"age":28},
    customIDUserProperties:{"key":"value"},
  }
  return remoteConfig;
}

2. 设置默认配置

参数下发SDK需要开发者预置一份默认配置到用户app中,方便参数下发SDK使用此默认配置进行兜底操作


方法示例

export function setDefaultConfig(configs:Array<ConfigItem>);


参数说明

参数名称参数含义参数类型是否必传
configs默认配置Array<ConfigItem>


调用示例

let defaultConfig:Array<ConfigItem> = new Array();
let item1 = SolarEngine.stringItem("key_string","stringText1");
let item2 = SolarEngine.numberItem("key_number",22);
let item3 = SolarEngine.booleanItem("key_boolean",true);

const obj = {
  name: 'John',
  age: 30,
  hobbies: ['reading', 'coding', 'running']
};
let item4 = SolarEngine.objectItem("key_object",obj);

defaultConfig.push(item1);
defaultConfig.push(item2);
defaultConfig.push(item3);
defaultConfig.push(item4);
SolarEngine.setDefaultConfig(defaultConfig);

3. 设置事件自定义属性

开发者可以为事件设置自定义属性,参数下发SDK在请求服务端配置时会带上该属性,用于服务端参数匹配


方法示例

export function setRemoteConfigEventProperties(properties:Object);


参数说明

参数名称参数含义参数类型是否必传
properties事件自定义属性Object


调用示例

const eventProperties = {
  "key1": "stringText2",
  "key2": 222,
  "key3":true
}
SolarEngine.setRemoteConfigEventProperties(eventProperties);


4. 设置用户自定义属性

开发者可以为用户设置自定义属性,参数下发SDK在请求服务端配置时会带上该属性,用于服务端参数匹配


方法示例

export function setRemoteConfigUserProperties(properties:Object)


参数说明

参数名称参数含义参数类型是否必传
properties用户自定义属性Object


调用示例

const userProperties = {
  "user_key1":"stringText3",
  "user_key2":2222,
  "user_key3":true
}
SolarEngine.setRemoteConfigUserProperties(userProperties);

注:给用户设置的自定义属性不支持开发者传入"_"下划线开头的key值,SDK会默认丢弃该条属性


5. 获取参数下发配置

参数下发SDK获取参数下发配置分为两种情况,一种是同步获取方法,通过此方法开发者可以直接获取返回结果。另一种是异步获取方法,返回结果需要等待回调完成才可以获取

注:通过同步或异步方式获取参数时,sdk 会触发上报用户命中试验的事件,所以获取参数的时机应该在用户到达试验场景时读取(获取参数代表用户到达试验场景,用户命中该参数对应的试验),请勿提前读取参数


5.1 同步获取参数下发配置

方法示例

export function fastFetchRemoteConfigWithKey(key:string,completion: (value:object|null) => void);


参数说明

参数名称参数含义参数类型是否必传
key参数下发key,跟用户在后台配置参数key对应string
completion可通过该匿名函数获取结果function


调用示例

SolarEngine.fastFetchRemoteConfigWithKey("key_string",(value:object|null) => {
  if (value != null) {
    log("fastFetchRemoteConfigWithKey, key_string: " + value + "  typeof: " + typeof(value));
  }
});

SolarEngine.fastFetchRemoteConfigWithKey("key_number",(value:object|null) => {
  if (value != null) {
    log("fastFetchRemoteConfigWithKey, key_number: " + value + "  typeof: " + typeof(value));
  }
});

SolarEngine.fastFetchRemoteConfigWithKey("key_boolean",(value:object|null) => {
  if (value != null) {
    log("fastFetchRemoteConfigWithKey, key_boolean: " + value + "  typeof: " + typeof(value));
  }
});

SolarEngine.fastFetchRemoteConfigWithKey("key_object",(value:object|null) => {
  if (value != null) {
    const key_objectValue = value as { name: string; hobbies: string[], age: number};
    const nameValue = key_objectValue.name;
    const ageValue = key_objectValue['age'];
    const hobbies = key_objectValue['hobbies'];
    log("fastFetchRemoteConfigWithKey, nameValue: " + nameValue);
    log("fastFetchRemoteConfigWithKey, ageValue: " + ageValue);
    log("fastFetchRemoteConfigWithKey, hobbies: " + hobbies);    
  }
});


5.2 同步获取所有参数下发配置

方法示例

export function fastFetchRemoteConfig(completion: (configs:Object|null) => void);

参数说明

参数名称参数含义参数类型是否必传
completion可通过该匿名函数获取结果function

调用示例

  SolarEngine.fastFetchRemoteConfig((value:Object|null) => {
  if (value != null){
    log("fastFetchRemoteConfig, JSON.stringify result: " + JSON.stringify(value));

    const typedValue = value as {key_string : string,key_boolean:boolean, key_object: object, key_number: string};
    log("fastFetchRemoteConfig, key_string: " + typedValue.key_string);
    log("fastFetchRemoteConfig, key_boolean: " + typedValue.key_boolean);
    log("fastFetchRemoteConfig, key_number typeOf: " + typeof typedValue.key_number);
    log("fastFetchRemoteConfig, key_number: " + typedValue.key_number);
    log("fastFetchRemoteConfig, key_object typeOf: " + typeof typedValue.key_object);

    const key_objectValue = typedValue.key_object as { name: string; hobbies: string[], age: number};
    const nameValue = key_objectValue.name;
    const ageValue = key_objectValue['age'];
    const hobbies = key_objectValue['hobbies'];
    log("fastFetchRemoteConfig, nameValue: " + nameValue);
    log("fastFetchRemoteConfig, ageValue: " + ageValue);
    log("fastFetchRemoteConfig, hobbies: " + hobbies);
  }
});

5.3 异步获取参数下发配置

方法示例

export function asyncFetchRemoteConfigWithKey(key:string,completion: (value:object|null) => void)


参数说明

参数名称参数含义参数类型是否必传
key参数下发key,跟用户在后台配置参数key对应string
completion可通过该匿名函数获取结果function


调用示例

  SolarEngine.asyncFetchRemoteConfigWithKey("key_object",(value:object|null) => {
    if (value != null) {

      const key_objectValue = value as { name: string; hobbies: string[], age: number};
      const nameValue = key_objectValue.name;
      const ageValue = key_objectValue['age'];
      const hobbies = key_objectValue['hobbies'];
      log("asyncFetchRemoteConfigWithKey, nameValue: " + nameValue);
      log("asyncFetchRemoteConfigWithKey, ageValue: " + ageValue);
      log("asyncFetchRemoteConfigWithKey, hobbies: " + hobbies);    
    }
  });


5.4 异步获取所有参数下发配置

方法示例

export function asyncFetchRemoteConfig(completion: (configs:Object|null) => void)

参数说明

参数名称参数含义参数类型是否必传
completion可通过该匿名函数获取结果function


调用示例

  SolarEngine.asyncFetchRemoteConfig((value:Object|null) => {
    if (value != null){
      log("asyncFetchRemoteConfig, JSON.stringify result: " + JSON.stringify(value));
      const typedValue = value as {key_string : string,key_boolean:boolean, key_object: object, key_number: number};
      log("asyncFetchRemoteConfig, key_string: " + typedValue.key_string);
      log("asyncFetchRemoteConfig, key_boolean: " + typedValue.key_boolean);
      log("asyncFetchRemoteConfig, key_number: " + typedValue.key_number);

      const key_objectValue = typedValue.key_object as { name: string; hobbies: string[], age: number};
      const nameValue = key_objectValue.name;
      const ageValue = key_objectValue['age'];
      const hobbies = key_objectValue['hobbies'];
      log("asyncFetchRemoteConfig, nameValue: " + nameValue);
      log("asyncFetchRemoteConfig, ageValue: " + ageValue);
      log("asyncFetchRemoteConfig, hobbies: " + hobbies);
    }
  });




最近修改: 2024-11-14Powered by