Skip to content

Commit

Permalink
Release 0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Apr 26, 2021
1 parent 1f5c6d1 commit f8f8ac1
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 24 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ SensorsAnalytics SDK 是国内第一家开源商用版用户行为采集 SDK,
## 基本要求
Android 4.0 及以上。

## 贡献

* 1. 在您的 GitHub 账户下 fork sa-sdk-android 开源项目;
* 2. 根据您的需求在本地 clone 一份 sa-sdk-android 源码;
* 3. 您修改或者新增功能后,push 到您 fork 的远程分支;
* 4. 创建 pull request,向 sa-sdk-android 官方开发分支提交合入请求;
* 5. 神策 SDK 研发团队会及时 review 代码,测试通过后合入。

## 规划

可参考 [ROADMAP](ROADMAP.md).
Expand All @@ -36,6 +44,11 @@ Android 4.0 及以上。
| ------ | ------ | ------ |
| [![《数据驱动:从方法到实践》](https://opensource.sensorsdata.cn/wp-content/uploads/data_driven_book_1.jpg)](https://item.jd.com/12322322.html) | [![《Android 全埋点解决方案》](https://opensource.sensorsdata.cn/wp-content/uploads/Android-全埋点thumbnail_1.png)](https://item.jd.com/12574672.html) | [![《iOS 全埋点解决方案》](https://opensource.sensorsdata.cn/wp-content/uploads/iOS-全埋点thumbnail_1.png)](https://item.jd.com/12867068.html)

## 感谢
- [mixpanel-android](https://github.com/mixpanel/mixpanel-android)
- [TiDB](https://github.com/pingcap/tidb)
- [Knight-ZXW](https://github.com/Knight-ZXW)

## License

Copyright 2015-2021 Sensors Data Inc.
Expand Down
4 changes: 1 addition & 3 deletions SensorsABTestSDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'com.jfrog.bintray'


version = "0.0.3"

version = "0.0.4"
android {
compileSdkVersion 29

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import com.sensorsdata.abtest.util.AppInfoUtils;
import com.sensorsdata.abtest.util.UrlUtil;
import com.sensorsdata.analytics.android.sdk.SALog;
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import com.sensorsdata.analytics.android.sdk.TrackTaskManager;

import java.lang.reflect.Method;

public class SensorsABTest implements ISensorsABTestApi {

Expand Down Expand Up @@ -142,7 +146,20 @@ public <T> void asyncFetchABTest(String paramName, T defaultValue, OnABTestRecei
}

@Override
public <T> void asyncFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack) {
public <T> void asyncFetchABTest(final String paramName, final T defaultValue, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
addTrackEventTask(new Runnable() {
@Override
public void run() {
asyncFetchABTestInner(SensorsDataAPI.sharedInstance().getDistinctId(), paramName, defaultValue, timeoutMillSeconds, callBack);
}
});
} catch (Exception e) {
SALog.printStackTrace(e);
}
}

private <T> void asyncFetchABTestInner(final String distinctId, final String paramName, final T defaultValue, int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
if (timeoutMillSeconds > 0) {
SALog.i(TAG, "timeoutMillSeconds minimum value is 1000ms");
Expand All @@ -152,7 +169,7 @@ public <T> void asyncFetchABTest(String paramName, T defaultValue, int timeoutMi
timeoutMillSeconds = TIMEOUT_REQUEST;
}
SALog.i(TAG, "asyncFetchABTest request param name: " + paramName + ",default value: " + defaultValue + ",timeoutMillSeconds: " + timeoutMillSeconds);
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(paramName, defaultValue, timeoutMillSeconds, callBack);
new SensorsABTestApiRequestHelper<T>().requestExperimentByParamName(distinctId, paramName, defaultValue, timeoutMillSeconds, callBack);
} catch (Exception e) {
SALog.printStackTrace(e);
}
Expand All @@ -168,13 +185,37 @@ public <T> void fastFetchABTest(String paramName, T defaultValue, OnABTestReceiv
}

@Override
public <T> void fastFetchABTest(String paramName, T defaultValue, int timeoutMillSeconds, OnABTestReceivedData<T> callBack) {
public <T> void fastFetchABTest(final String paramName, final T defaultValue, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
try {
T t = SensorsABTestCacheManager.getInstance().getExperimentVariableValue(paramName, defaultValue);
if (t != null && callBack != null) {
callBack.onResult(t);
} else {
asyncFetchABTest(paramName, defaultValue, timeoutMillSeconds, callBack);
addTrackEventTask(new Runnable() {
@Override
public void run() {
try {
String distinctId = SensorsDataAPI.sharedInstance().getDistinctId();
T t = SensorsABTestCacheManager.getInstance().getExperimentVariableValue(paramName, defaultValue);
if (t != null && callBack != null) {
callBack.onResult(t);
} else {
asyncFetchABTestInner(distinctId, paramName, defaultValue, timeoutMillSeconds, callBack);
}
} catch (Exception e) {
SALog.printStackTrace(e);
}
}
});
} catch (Exception e) {
SALog.printStackTrace(e);
}
}

private static void addTrackEventTask(Runnable runnable) {
try {
Object obj = TrackTaskManager.getInstance();
if (obj != null) {
Class<?> clazz = obj.getClass();
Method addTrackEventTask = clazz.getDeclaredMethod("addTrackEventTask", Runnable.class);
addTrackEventTask.setAccessible(true);
addTrackEventTask.invoke(obj, runnable);
}
} catch (Exception e) {
SALog.printStackTrace(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.sensorsdata.abtest.util.TaskRunner;
import com.sensorsdata.abtest.util.UrlUtil;
import com.sensorsdata.analytics.android.sdk.SALog;
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import com.sensorsdata.analytics.android.sdk.TrackTaskManager;
import com.sensorsdata.analytics.android.sdk.network.HttpCallback;
import com.sensorsdata.analytics.android.sdk.network.HttpMethod;
import com.sensorsdata.analytics.android.sdk.network.RequestHelper;
Expand All @@ -48,8 +50,9 @@ public class SensorsABTestApiRequestHelper<T> {

private static final String TAG = "SAB.SensorsABTestApiRequestHelper";
private boolean mHasCallback = false;
private String mDistinctId;

public void requestExperimentByParamName(final String paramName, final T defaultValue, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
public void requestExperimentByParamName(final String distinctId, final String paramName, final T defaultValue, final int timeoutMillSeconds, final OnABTestReceivedData<T> callBack) {
// callback 为 null
if (callBack == null) {
SALog.i(TAG, "试验 callback 不正确,试验 callback 不能为空!");
Expand Down Expand Up @@ -81,6 +84,7 @@ public void requestExperimentByParamName(final String paramName, final T default
// 启动定时器
final TimeoutRunnable runnable = new TimeoutRunnable(callBack, defaultValue);
TaskRunner.getBackHandler().postDelayed(runnable, timeoutMillSeconds);
mDistinctId = distinctId;

requestExperimentsAndUpdateCache(new IApiCallback<Map<String, Experiment>>() {
@Override
Expand Down Expand Up @@ -216,7 +220,13 @@ public void onSuccess(String s) {
if (TextUtils.equals(AppConstants.AB_TEST_SUCCESS, status)) {
SALog.i(TAG, String.format("获取试验成功:results:%s", JSONUtils.formatJson(response.toString())));
JSONArray array = response.optJSONArray("results");
hashMap = SensorsABTestCacheManager.getInstance().loadExperimentsFromCache(array != null ? array.toString() : "");
JSONObject object = null;
if (array != null) {
object = new JSONObject();
object.put("experiments", array);
object.put("distinct_id", mDistinctId);
}
hashMap = SensorsABTestCacheManager.getInstance().loadExperimentsFromCache(object != null ? object.toString() : "");
} 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import com.sensorsdata.abtest.entity.SABErrorEnum;
import com.sensorsdata.abtest.util.SPUtils;
import com.sensorsdata.analytics.android.sdk.SALog;
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import com.sensorsdata.analytics.android.sdk.util.JSONUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
Expand All @@ -38,7 +38,7 @@ public class SensorsABTestCacheManager implements IExperimentCacheAPI {

private static final String TAG = "SAB.SensorsABTestCacheManager";
public ConcurrentHashMap<String, Experiment> mHashMap;
private static final String KEY_EXPERIMENT = "key_experiment";
private static final String KEY_EXPERIMENT = "key_experiment_with_distinct_id";

private SensorsABTestCacheManager() {
mHashMap = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -86,7 +86,25 @@ public ConcurrentHashMap<String, Experiment> getExperimentsFromMemoryCache(Strin
return hashMap;
}
try {
JSONArray array = new JSONArray(result);
JSONObject jsonObject = new JSONObject(result);
String experiments = jsonObject.optString("experiments");
if (TextUtils.isEmpty(experiments)) {
mHashMap.clear();
return hashMap;
}
parseCache(experiments, hashMap);
} catch (Exception e) {
SALog.printStackTrace(e);
}
mHashMap.clear();
mHashMap.putAll(hashMap);
return hashMap;
}

private void parseCache(String result, ConcurrentHashMap<String, Experiment> hashMap) {
JSONArray array = null;
try {
array = new JSONArray(result);
if (array.length() > 0) {
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.optJSONObject(i);
Expand Down Expand Up @@ -121,12 +139,9 @@ public ConcurrentHashMap<String, Experiment> getExperimentsFromMemoryCache(Strin
}
SALog.i(TAG, "saveExperiments2MemoryCache | experiments:\n" + JSONUtils.formatJson(hashMap.toString()));
}
} catch (JSONException e) {
} catch (Exception e) {
SALog.printStackTrace(e);
}
mHashMap.clear();
mHashMap.putAll(hashMap);
return hashMap;
}

@Override
Expand All @@ -136,6 +151,13 @@ public ConcurrentHashMap<String, Experiment> loadExperimentsFromCache(String res
return hashMap;
}

/**
* 清除文件缓存
*/
void clearCache() {
loadExperimentsFromCache("");
}

/**
* 从内存缓存中获取命中的试验实体
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,33 @@ public void trackEvent(JSONObject jsonObject) {
@Override
public void login() {
SALog.i(TAG, "login");
new SensorsABTestApiRequestHelper<>().requestExperimentsAndUpdateCache();
onDistinctIdChanged();
}

@Override
public void logout() {
SALog.i(TAG, "logout");
new SensorsABTestApiRequestHelper<>().requestExperimentsAndUpdateCache();
onDistinctIdChanged();
}

@Override
public void identify() {
SALog.i(TAG, "identify");
new SensorsABTestApiRequestHelper<>().requestExperimentsAndUpdateCache();
onDistinctIdChanged();
}

@Override
public void resetAnonymousId() {
SALog.i(TAG, "resetAnonymousId");
new SensorsABTestApiRequestHelper<>().requestExperimentsAndUpdateCache();
onDistinctIdChanged();
}

private void onDistinctIdChanged() {
try {
SensorsABTestCacheManager.getInstance().clearCache();
new SensorsABTestApiRequestHelper<>().requestExperimentsAndUpdateCache();
} catch (Exception e) {
SALog.printStackTrace(e);
}
}
}

0 comments on commit f8f8ac1

Please sign in to comment.