Skip to content

Commit beb9d6e

Browse files
author
weiqiangliu
committed
Release 2.2.3
1 parent 55adb19 commit beb9d6e

19 files changed

+393
-17
lines changed

RNSensorsAnalyticsModule.podspec

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Pod::Spec.new do |s|
33
s.name = "RNSensorsAnalyticsModule"
4-
s.version = "2.2.2"
4+
s.version = "2.2.3"
55
s.summary = "The official React Native SDK of Sensors Analytics."
66
s.description = <<-DESC
77
神策分析 RN 组件
@@ -14,7 +14,6 @@ Pod::Spec.new do |s|
1414
s.source_files = "ios/*.{h,m}"
1515
s.requires_arc = true
1616
s.dependency "React"
17-
s.dependency "SensorsAnalyticsSDK", ">= 1.11.15"
17+
s.dependency "SensorsAnalyticsSDK", ">= 4.2.1"
1818

1919
end
20-

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ dependencies {
4040
compileOnly 'com.facebook.react:react-native:+'
4141

4242
//添加 Sensors Analytics SDK 依赖
43-
compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.0.0'
43+
compileOnly 'com.sensorsdata.analytics.android:SensorsAnalyticsSDK:6.2.1'
4444
}

android/src/main/java/com/sensorsdata/analytics/RNAgent.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
import org.json.JSONObject;
3737

3838
import java.lang.reflect.Field;
39+
import java.util.HashMap;
3940
import java.util.WeakHashMap;
4041

4142
public class RNAgent {
4243
private static final WeakHashMap jsTouchDispatcherViewGroupWeakHashMap = new WeakHashMap();
4344
private static SparseArray<SAViewProperties> viewPropertiesArray = new SparseArray();
45+
private static HashMap<String,Object> mDynamicSuperProperties;
4446

4547
public static void handleTouchEvent(
4648
JSTouchDispatcher jsTouchDispatcher, MotionEvent event, EventDispatcher eventDispatcher) {
@@ -98,9 +100,9 @@ static void trackViewScreen(String url, JSONObject properties, boolean isAuto) {
98100
}
99101
RNViewUtils.saveScreenAndTitle(screenName, title);
100102
if (isAuto && (properties.optBoolean("SAIgnoreViewScreen", false)
101-
|| !SensorsDataAPI.sharedInstance().isAutoTrackEnabled()
102-
|| SensorsDataAPI.sharedInstance().isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_VIEW_SCREEN))) {
103-
return;
103+
|| !SensorsDataAPI.sharedInstance().isAutoTrackEnabled()
104+
|| SensorsDataAPI.sharedInstance().isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_VIEW_SCREEN))) {
105+
return;
104106
}
105107
SensorsDataAPI.sharedInstance().trackViewScreen(url, RNPropertyManager.mergeProperty(properties, isAuto));
106108
} catch (Exception e) {
@@ -182,4 +184,12 @@ static void ignoreView() {
182184
//ignored
183185
}
184186
}
187+
188+
public static HashMap<String, Object> getDynamicSuperProperties(){
189+
return mDynamicSuperProperties;
190+
}
191+
192+
static void setDynamicSuperProperties(HashMap<String,Object> dynamicSuperProperties){
193+
mDynamicSuperProperties = dynamicSuperProperties;
194+
}
185195
}

android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsModule.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,4 +995,36 @@ public void enableDataCollect() {
995995
Log.e(LOGTAG, e.toString() + "");
996996
}
997997
}
998+
999+
/**
1000+
* 绑定业务
1001+
*
1002+
* @param key ID
1003+
* @param value 值
1004+
*/
1005+
@ReactMethod
1006+
public void bind(String key, String value) {
1007+
try {
1008+
SensorsDataAPI.sharedInstance().bind(key, value);
1009+
} catch (Exception e) {
1010+
e.printStackTrace();
1011+
Log.e(LOGTAG, e.toString() + "");
1012+
}
1013+
}
1014+
1015+
/**
1016+
* 解绑业务
1017+
*
1018+
* @param key ID
1019+
* @param value 值
1020+
*/
1021+
@ReactMethod
1022+
public void unbind(String key, String value) {
1023+
try {
1024+
SensorsDataAPI.sharedInstance().unbind(key, value);
1025+
} catch (Exception e) {
1026+
e.printStackTrace();
1027+
Log.e(LOGTAG, e.toString() + "");
1028+
}
1029+
}
9981030
}

android/src/main/java/com/sensorsdata/analytics/RNSensorsAnalyticsPackage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
import java.util.List;
2929

3030
public class RNSensorsAnalyticsPackage implements ReactPackage {
31-
public static final String VERSION = "2.2.2";
31+
public static final String VERSION = "2.2.3";
32+
3233
@Override
3334
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
3435
List<NativeModule> modules = new ArrayList<>();

android/src/main/java/com/sensorsdata/analytics/RNSensorsDataModule.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@
2424
import com.facebook.react.bridge.ReactMethod;
2525
import com.facebook.react.bridge.ReadableMap;
2626
import com.sensorsdata.analytics.android.sdk.SALog;
27+
import com.sensorsdata.analytics.android.sdk.plugin.property.SensorsDataPropertyPluginManager;
2728
import com.sensorsdata.analytics.property.LibMethodInterceptor;
2829
import com.sensorsdata.analytics.property.PluginVersionInterceptor;
30+
import com.sensorsdata.analytics.property.RNDynamicPropertyPlugin;
2931
import com.sensorsdata.analytics.property.RNPropertyManager;
3032
import com.sensorsdata.analytics.utils.RNUtils;
3133
import com.sensorsdata.analytics.utils.RNViewUtils;
34+
import com.sensorsdata.analytics.utils.VersionUtils;
3235

3336
import org.json.JSONObject;
3437

@@ -47,6 +50,8 @@
4750

4851
public class RNSensorsDataModule extends ReactContextBaseJavaModule {
4952

53+
private RNDynamicPropertyPlugin mDynamicPlugin;
54+
5055
public RNSensorsDataModule(ReactApplicationContext reactContext) {
5156
super(reactContext);
5257
try {
@@ -103,6 +108,18 @@ public void saveViewProperties(int viewId, boolean clickable, ReadableMap viewPr
103108
RNAgent.saveViewProperties(viewId, clickable, viewProperties);
104109
}
105110

111+
@ReactMethod
112+
public void setDynamicSuperProperties(ReadableMap dynamicSuperProperties) {
113+
if (!VersionUtils.checkSAVersion("6.2.1")) {
114+
return;
115+
}
116+
if (mDynamicPlugin == null) {
117+
mDynamicPlugin = new RNDynamicPropertyPlugin();
118+
SensorsDataPropertyPluginManager.getInstance().registerPropertyPlugin(mDynamicPlugin);
119+
}
120+
RNAgent.setDynamicSuperProperties(RNUtils.convertToHashMap(dynamicSuperProperties));
121+
}
122+
106123
class SensorsDataLifecycleListener implements LifecycleEventListener {
107124
public void onHostResume() {
108125
RNViewUtils.onActivityResumed(getCurrentActivity());
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sensorsdata.analytics.property;
2+
3+
import com.sensorsdata.analytics.R;
4+
import com.sensorsdata.analytics.RNAgent;
5+
import com.sensorsdata.analytics.android.sdk.internal.beans.EventType;
6+
import com.sensorsdata.analytics.android.sdk.plugin.property.SAPropertyPlugin;
7+
import com.sensorsdata.analytics.android.sdk.plugin.property.SAPropertyPluginPriority;
8+
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
public class RNDynamicPropertyPlugin extends SAPropertyPlugin {
13+
@Override
14+
public void appendProperties(Map<String, Object> properties) {
15+
16+
}
17+
18+
@Override
19+
public void appendDynamicProperties(Map<String, Object> dynamicProperties) {
20+
if (dynamicProperties != null) {
21+
Map<String, Object> properties = RNAgent.getDynamicSuperProperties();
22+
if (properties != null && !properties.isEmpty()) {
23+
dynamicProperties.putAll(properties);
24+
}
25+
}
26+
}
27+
28+
@Override
29+
public void eventTypeFilter(Set<EventType> eventTypeFilter) {
30+
eventTypeFilter.add(EventType.TRACK);
31+
eventTypeFilter.add(EventType.TRACK_ID_BIND);
32+
eventTypeFilter.add(EventType.TRACK_SIGNUP);
33+
eventTypeFilter.add(EventType.TRACK_ID_UNBIND);
34+
}
35+
36+
@Override
37+
public SAPropertyPluginPriority priority() {
38+
return SAPropertyPluginPriority.LOW;
39+
}
40+
}

android/src/main/java/com/sensorsdata/analytics/utils/RNUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.facebook.react.bridge.ReadableMap;
2323
import com.facebook.react.bridge.ReadableNativeMap;
2424
import com.facebook.react.bridge.WritableMap;
25+
26+
import java.util.HashMap;
2527
import java.util.Iterator;
2628
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
2729
import com.sensorsdata.analytics.android.sdk.SALog;
@@ -73,4 +75,20 @@ public static WritableMap convertToMap(JSONObject json) {
7375
}
7476
return writableMap;
7577
}
78+
79+
/**
80+
* ReadableMap 转换成 HashMap
81+
*/
82+
public static HashMap<String, Object> convertToHashMap(ReadableMap properties) {
83+
if (properties == null) {
84+
return null;
85+
}
86+
HashMap<String, Object> map = null;
87+
try {
88+
map = ((ReadableNativeMap)properties).toHashMap();
89+
}catch (Exception e){
90+
SALog.printStackTrace(e);
91+
}
92+
return map;
93+
}
7694
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Created by chenru on 2022/02/17.
3+
* Copyright 2015-2022 Sensors Data Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.sensorsdata.analytics.utils;
19+
20+
import android.text.TextUtils;
21+
22+
import com.sensorsdata.analytics.android.sdk.SALog;
23+
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
24+
25+
import java.lang.reflect.Field;
26+
27+
public class VersionUtils {
28+
private static final String TAG = "SA.RNVersionUtils";
29+
30+
public static boolean checkSAVersion(final String requiredVersion) {
31+
try {
32+
SensorsDataAPI sensorsDataAPI = SensorsDataAPI.sharedInstance();
33+
Field field = sensorsDataAPI.getClass().getDeclaredField("VERSION");
34+
field.setAccessible(true);
35+
String version = (String) field.get(sensorsDataAPI);
36+
String compareVersion = version;
37+
if (!TextUtils.isEmpty(version)) {
38+
if (version.contains("-")) {
39+
compareVersion = compareVersion.substring(0, compareVersion.indexOf("-"));
40+
}
41+
if (!isVersionValid(compareVersion, requiredVersion)) {
42+
SALog.i(TAG, "当前神策 Android 埋点 SDK 版本 " + version + " 过低,请升级至 " + requiredVersion + " 及其以上版本后进行使用");
43+
return false;
44+
}
45+
}
46+
} catch (Exception ex) {
47+
SALog.printStackTrace(ex);
48+
return false;
49+
}
50+
return true;
51+
}
52+
53+
/**
54+
* 比较当前实际的 SA 版本和期望的 SA 最低版本
55+
*
56+
* @param saVersion 当前实际的 SA 版本
57+
* @param requiredVersion 期望的 SA 最低版本
58+
* @return false 代表实际 SA 的比期望低,true 代表符合预期
59+
*/
60+
public static boolean isVersionValid(String saVersion, String requiredVersion) {
61+
try {
62+
if (saVersion.equals(requiredVersion)) {
63+
return true;
64+
} else {
65+
String[] saVersions = saVersion.split("\\.");
66+
String[] requiredVersions = requiredVersion.split("\\.");
67+
for (int index = 0; index < requiredVersions.length; index++) {
68+
int saVersionsNum = Integer.parseInt(saVersions[index]);
69+
int requiredVersionsNum = Integer.parseInt(requiredVersions[index]);
70+
if (saVersionsNum != requiredVersionsNum) {
71+
return saVersionsNum > requiredVersionsNum;
72+
}
73+
}
74+
return false;
75+
}
76+
} catch (Exception ex) {
77+
// ignore
78+
return false;
79+
}
80+
}
81+
}

index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,29 @@ declare module 'sensorsdata-analytics-react-native'{
273273
*/
274274
export function trackAppInstall(properties: PropertiesObjectType | null)
275275

276+
/**
277+
* 注册事件动态公共属性
278+
* @return 动态公共属性监听对象
279+
*/
280+
export function registerDynamicSuperProperties():Object
281+
282+
283+
/**
284+
* 绑定业务 ID
285+
*
286+
* @param key ID
287+
* @param value 值
288+
*/
289+
export function bind(key: string, value: string);
290+
291+
/**
292+
* 解绑业务 ID
293+
*
294+
* @param key ID
295+
* @param value 值
296+
*/
297+
export function unbind(key: string, value: string);
298+
276299
/************** Android only start *****************/
277300
/**
278301
* 设置 App 切换到后台与下次事件的事件间隔

0 commit comments

Comments
 (0)