Skip to content

Commit

Permalink
Release 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Apr 6, 2023
1 parent 010db2d commit df4108a
Show file tree
Hide file tree
Showing 38 changed files with 5,913 additions and 6,989 deletions.
4 changes: 2 additions & 2 deletions SensorsABTestSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'signing'
apply plugin: 'maven-publish'

version = "0.2.4"
version = "1.0.0"
android {
compileSdkVersion 29

Expand Down Expand Up @@ -47,7 +47,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.3.5'
compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.4.3'
// compileOnly files('libs/SensorsAnalyticsSDK-6.2.0.aar')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.sensorsdata.abtest.entity.SABErrorEnum;
import com.sensorsdata.abtest.store.StoreManagerFactory;
import com.sensorsdata.abtest.util.AppInfoUtils;
import com.sensorsdata.abtest.util.CommonUtils;
import com.sensorsdata.abtest.util.TaskRunner;
import com.sensorsdata.abtest.util.UrlUtil;
import com.sensorsdata.analytics.android.sdk.SALog;
Expand Down Expand Up @@ -241,7 +242,7 @@ private <T> void requestExperimentWithParams(final String paramName, final T def
}
SALog.i(TAG, "asyncFetchABTest request param name: " + paramName + ",default value: " + defaultValue + ",timeoutMillSeconds: " + timeoutMillSeconds);
final String distinctId = SensorsDataAPI.sharedInstance().getDistinctId();
final String loginId = SensorsDataAPI.sharedInstance().getLoginId();
final String loginId = CommonUtils.getLoginId();
final String anonymousId = SensorsDataAPI.sharedInstance().getAnonymousId();
final String customIds = SensorsABTestCustomIdsManager.getInstance().getCustomIdsString();
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(distinctId, loginId, anonymousId, customIds, paramName, defaultValue, properties, timeoutMillSeconds, callBack, mergeRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,45 +154,74 @@ public void onSuccess(Map<String, Experiment> experimentMap) {
OnABTestReceivedData<?> onABTestReceivedData = item.getResultCallBack();
String itemParamName = item.getParamName();
Object itemDefaultValue = item.getDefaultValue();
try {
if (experimentMap == null) {
SALog.i(TAG, "onSuccess response is empty and return default value: " + itemDefaultValue);
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
continue;
}
Experiment experiment = experimentMap.get(itemParamName);
if (experiment == null) {
SALog.i(TAG, "onSuccess experiment is empty and return default value: " + itemDefaultValue);
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
continue;
}
if (!experiment.checkTypeIsValid(itemParamName, itemDefaultValue)) {
if (itemDefaultValue != null) {
String variableType = "";
Experiment.Variable variable = experiment.getVariableByParamName(itemParamName);
if (variable != null) {
variableType = variable.type;
}
SABErrorDispatcher.dispatchSABException(SABErrorEnum.ASYNC_REQUEST_PARAMS_TYPE_NOT_VALID, itemParamName, variableType, itemDefaultValue.getClass().toString());
}
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
continue;
}
Object value = experiment.getVariableValue(itemParamName, itemDefaultValue);
if (value != null) {
SALog.i(TAG, "onSuccess return value: " + value);
doCallbackOnMainThread(onABTestReceivedData, value);
hitTestExperimentResult(experimentMap, onABTestReceivedData, itemParamName, itemDefaultValue);
hitTestOutListResult(itemParamName);
}
mHasCallback = true;
}

/**
* 根据 paramName 获取试验,如果获取到就也触发 ABTestTrigger 事件
*
* @param paramName 试验参数
*/
private void hitTestOutListResult(String paramName) {
Experiment experiment = SensorsABTestCacheManager.getInstance().getExperimentByParamNameFromOutList(paramName);
if (experiment != null) {
SALog.i(TAG, "Hit out list experiment: " + experiment);
SensorsABTestTrackHelper.getInstance().trackABTestTrigger(experiment, distinctId, loginId, anonymousId, customIDs);
}
}

if (!experiment.isWhiteList) {
SensorsABTestTrackHelper.getInstance().trackABTestTrigger(experiment, distinctId, loginId, anonymousId, customIDs);
/**
* 根据 paramName 获取试验
*
* @param experimentMap 试验 paramName 和 Experiment 映射
* @param onABTestReceivedData 回调
* @param itemParamName paramName
* @param itemDefaultValue 默认值
*/
private void hitTestExperimentResult(Map<String, Experiment> experimentMap,
OnABTestReceivedData<?> onABTestReceivedData,
String itemParamName,
Object itemDefaultValue) {
try {
if (experimentMap == null) {
SALog.i(TAG, "onSuccess response is empty and return default value: " + itemDefaultValue);
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
return;
}
Experiment experiment = experimentMap.get(itemParamName);
if (experiment == null) {
SALog.i(TAG, "onSuccess experiment is empty and return default value: " + itemDefaultValue);
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
return;
}
if (!experiment.checkTypeIsValid(itemParamName, itemDefaultValue)) {
if (itemDefaultValue != null) {
String variableType = "";
Experiment.Variable variable = experiment.getVariableByParamName(itemParamName);
if (variable != null) {
variableType = variable.type;
}
SABErrorDispatcher.dispatchSABException(SABErrorEnum.ASYNC_REQUEST_PARAMS_TYPE_NOT_VALID, itemParamName, variableType, itemDefaultValue.getClass().toString());
}
} catch (Exception e) {
SALog.i(TAG, "onSuccess Exception and return default value: " + itemDefaultValue);
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
return;
}
Object value = experiment.getVariableValue(itemParamName, itemDefaultValue);
if (value != null) {
SALog.i(TAG, "onSuccess return value: " + value);
doCallbackOnMainThread(onABTestReceivedData, value);

if (!experiment.isWhiteList) {
SensorsABTestTrackHelper.getInstance().trackABTestTrigger(experiment, distinctId, loginId, anonymousId, customIDs);
}
}
} catch (Exception e) {
SALog.i(TAG, "onSuccess Exception and return default value: " + itemDefaultValue);
doCallbackOnMainThread(onABTestReceivedData, itemDefaultValue);
}
mHasCallback = true;
}

@Override
Expand All @@ -216,6 +245,7 @@ public void onFailure(int errorCode, String message) {
});
}


void requestExperiments(Map<String, String> properties, String paramName, final IApiCallback<String> callBack) {
requestExperiments(properties, paramName, null, callBack);
}
Expand Down Expand Up @@ -291,18 +321,26 @@ public void onSuccess(String s) {
String status = response.optString("status");
if (TextUtils.equals(AppConstants.AB_TEST_SUCCESS, status)) {
SALog.i(TAG, String.format("获取试验成功:results:%s", JSONUtils.formatJson(response.toString())));
JSONArray array = response.optJSONArray("results");
JSONObject object = null;
if (array != null) {
object = new JSONObject();
object.put("experiments", array);
JSONArray experimentArray = response.optJSONArray("results");
JSONArray outListArray = response.optJSONArray("out_list");
JSONObject object = new JSONObject();
if (experimentArray != null) {
object.put("experiments", experimentArray);
if (TextUtils.isEmpty(mUserIdentifier)) {
mUserIdentifier = CommonUtils.getCurrentUserIdentifier();
}
object.put("identifier", mUserIdentifier);
}
hashMap = SensorsABTestCacheManager.getInstance().loadExperimentsFromCache(object != null ? object.toString() : "");
if (outListArray != null) {
object.put("outList", outListArray);
}
hashMap = SensorsABTestCacheManager.getInstance().updateExperimentsCache(object.toString());
SensorsABTestCacheManager.getInstance().saveFuzzyExperiments(response.optJSONArray("fuzzy_experiments"));
JSONObject trackConfigObj = response.optJSONObject("track_config");
if (trackConfigObj != null) {
trackConfigObj.put("identifier", mUserIdentifier);
}
SensorsABTestTrackConfigManager.getInstance().saveTrackConfig(trackConfigObj);
} else if (TextUtils.equals(AppConstants.AB_TEST_FAILURE, status)) {
SALog.i(TAG, String.format("获取试验失败:error_type:%s,error:%s", response.optString("error_type"), response.optString("error")));
}
Expand Down
Loading

0 comments on commit df4108a

Please sign in to comment.