Skip to content

Commit

Permalink
Release 2.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
chenru committed Mar 1, 2021
1 parent 736f853 commit 90bda7c
Show file tree
Hide file tree
Showing 23 changed files with 373 additions and 39 deletions.
2 changes: 1 addition & 1 deletion RNSensorsAnalyticsModule.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Pod::Spec.new do |s|
s.name = "RNSensorsAnalyticsModule"
s.version = "2.0.7"
s.version = "2.0.8"
s.summary = "The official React Native SDK of Sensors Analytics."
s.description = <<-DESC
神策分析 RN 组件
Expand Down
13 changes: 12 additions & 1 deletion SensorsDataRNHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ var sensorsdataSwitchHookCode = "if(this.props.onChange != null || this.props.on
+" })(this); /* SENSORSDATA HOOK */}";
var sensorsdataImportReactNativeHookCode ="import ReactNative from 'react-native';\n";
var sensorsdataNavigation5HookCode = `
function getCurrentRouteName(){
let state = getRootState();
if (state === undefined) {
return undefined;
}
while (state.routes[state.index].state !== undefined) {
state = state.routes[state.index].state as NavigationState;
}
return state.routes[state.index].name;
}
function getParams(state:any):any{
if(!state){
return null;
Expand All @@ -113,7 +124,7 @@ var sensorsdataNavigation5HookCode = `
trackViewScreen(route.state);
return;
}
var screenName = getCurrentRoute()?.name;
var screenName = getCurrentRouteName();
var params = getParams(state);
var saProperties = {};
if (params) {
Expand Down
5 changes: 3 additions & 2 deletions android/src/main/java/com/sensorsdata/analytics/RNAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.sensorsdata.analytics.RNSensorsAnalyticsModule;
import com.sensorsdata.analytics.utils.RNViewUtils;
import com.sensorsdata.analytics.data.SAViewProperties;
import com.sensorsdata.analytics.property.RNPropertyManager;

import com.facebook.react.uimanager.JSTouchDispatcher;
import com.facebook.react.uimanager.events.EventDispatcher;
Expand Down Expand Up @@ -75,7 +76,7 @@ public static void trackViewScreen(String url, JSONObject properties, boolean is
if (isAuto && SensorsDataAPI.sharedInstance().isAutoTrackEventTypeIgnored(SensorsDataAPI.AutoTrackEventType.APP_VIEW_SCREEN)) {
return;
}
SensorsDataAPI.sharedInstance().trackViewScreen(url, properties);
SensorsDataAPI.sharedInstance().trackViewScreen(url, RNPropertyManager.mergeProperty(properties, isAuto));
}catch(Exception e){
SALog.printStackTrace(e);
}
Expand Down Expand Up @@ -108,7 +109,7 @@ public static void trackViewClick(int viewId){
viewProperties.properties.remove("ignore");
SensorsDataUtils.mergeJSONObject(viewProperties.properties, properties);
}
SensorsDataAPI.sharedInstance().trackViewAppClick(clickView, properties);
SensorsDataAPI.sharedInstance().trackViewAppClick(clickView, RNPropertyManager.mergeProperty(properties ,true));
}
} catch (Exception e) {
SALog.printStackTrace(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableMap;
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import com.sensorsdata.analytics.property.RNPropertyManager;
import com.sensorsdata.analytics.utils.RNUtils;

import org.json.JSONObject;
Expand Down Expand Up @@ -95,7 +96,7 @@ public String getName() {
@ReactMethod
public void track(String eventName, ReadableMap properties) {
try {
SensorsDataAPI.sharedInstance().track(eventName, RNUtils.convertToJSONObject(properties));
SensorsDataAPI.sharedInstance().track(eventName, RNPropertyManager.mergeProperty(RNUtils.convertToJSONObject(properties)));
} catch (Exception e) {
e.printStackTrace();
Log.e(LOGTAG, e.toString() + "");
Expand Down Expand Up @@ -167,7 +168,7 @@ public void trackTimerBegin(String eventName) {
@ReactMethod
public void trackTimerEnd(String eventName, ReadableMap properties) {
try {
SensorsDataAPI.sharedInstance().trackTimerEnd(eventName, RNUtils.convertToJSONObject(properties));
SensorsDataAPI.sharedInstance().trackTimerEnd(eventName, RNPropertyManager.mergeProperty(RNUtils.convertToJSONObject(properties)));
} catch (Exception e) {
e.printStackTrace();
Log.e(LOGTAG, e.toString() + "");
Expand Down Expand Up @@ -211,7 +212,7 @@ public void clearTrackTimer() {
@ReactMethod
public void login(String loginId) {
try {
SensorsDataAPI.sharedInstance().login(loginId);
SensorsDataAPI.sharedInstance().login(loginId, RNPropertyManager.mergeProperty(null));
} catch (Exception e) {
e.printStackTrace();
Log.e(LOGTAG, e.toString() + "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import java.util.List;

public class RNSensorsAnalyticsPackage implements ReactPackage {
public static final String VERSION = "2.0.7";
public static final String VERSION = "2.0.8";
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
import com.facebook.react.bridge.ReadableMap;
import com.sensorsdata.analytics.android.sdk.SensorsDataAPI;
import com.sensorsdata.analytics.android.sdk.SALog;
import com.sensorsdata.analytics.property.LibMethodInterceptor;
import com.sensorsdata.analytics.utils.RNUtils;
import com.sensorsdata.analytics.utils.RNViewUtils;
import com.sensorsdata.analytics.property.RNPropertyManager;
import com.sensorsdata.analytics.property.PluginVersionInterceptor;

import org.json.JSONObject;

Expand All @@ -54,6 +57,8 @@ public RNSensorsDataModule(ReactApplicationContext reactContext) {

}
RNAgent.ignoreView();
RNPropertyManager.addInterceptor(new PluginVersionInterceptor());
RNPropertyManager.addInterceptor(new LibMethodInterceptor());
}

private static final String MODULE_NAME = "RNSensorsDataModule";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Created by chenru on 2021/01/05.
* Copyright 2015-2021 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sensorsdata.analytics.property;

import com.sensorsdata.analytics.property.RNPropertyManager.Interceptor;

import org.json.JSONObject;

public class LibMethodInterceptor implements Interceptor {

public JSONObject proceed(JSONObject properties, boolean isAuto) {
if (properties == null) {
properties = new JSONObject();
}
try {
if (!"autoTrack".equals(properties.optString("$lib_method"))) {
if (isAuto) {
properties.put("$lib_method", "autoTrack");
} else {
properties.put("$lib_method", "code");
}
}
} catch (Exception ignored) {

}
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Created by chenru on 2020/12/27.
* Copyright 2015-2021 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sensorsdata.analytics.property;

import com.sensorsdata.analytics.RNSensorsAnalyticsPackage;
import com.sensorsdata.analytics.property.RNPropertyManager.Interceptor;

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

public class PluginVersionInterceptor implements Interceptor {
private static boolean isMergePluginVersion = false;

public JSONObject proceed(JSONObject properties, boolean isAuto){
if(!isMergePluginVersion){
if(properties == null){
properties = new JSONObject();
}else if(properties.has("$lib_plugin_version")){
return properties;
}
try{
JSONArray array = new JSONArray();
array.put("react_native:" + RNSensorsAnalyticsPackage.VERSION);
properties.put("$lib_plugin_version",array);
}catch (Exception ignored){
//ignore
}
isMergePluginVersion = true;
}
return properties;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Created by chenru on 2020/12/27.
* Copyright 2015-2021 Sensors Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.sensorsdata.analytics.property;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class RNPropertyManager {

public static List<Interceptor> interceptors = new ArrayList<>();

public static void addInterceptor(Interceptor interceptor){
interceptors.add(interceptor);
}

public static JSONObject mergeProperty(JSONObject properties){
return mergeProperty(properties, false);
}

public static JSONObject mergeProperty(JSONObject properties, boolean isAuto){
for(Interceptor interceptor:interceptors){
properties = interceptor.proceed(properties, isAuto);
}
return properties;
}

interface Interceptor{
JSONObject proceed(JSONObject properties, boolean isAuto);
}
}
22 changes: 18 additions & 4 deletions ios/RNSensorsAnalyticsModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@
// RNSensorsAnalyticsModule
//
// Created by 肖彦敏 on 2017/4/14.
// Copyright © 2017年 Facebook. All rights reserved.
// Copyright © 2017-2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import <Foundation/Foundation.h>
#if __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#if __has_include(<React/RCTBridgeModule.h>)
#import <React/RCTBridgeModule.h>
#else
#import "RCTBridgeModule.h"
#endif

extern NSString *const kSAReactNativePluginVersion;

@interface RNSensorsAnalyticsModule : NSObject<RCTBridgeModule>

@end
Expand Down
39 changes: 30 additions & 9 deletions ios/RNSensorsAnalyticsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@
// RNSensorsAnalyticsModule
//
// Created by 肖彦敏 on 2017/4/14.
// Copyright © 2017年 Facebook. All rights reserved.
// Copyright © 2017-2021 Sensors Data Co., Ltd. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#import "RNSensorsAnalyticsModule.h"
#import "SAReactNativeManager.h"
#if ! __has_feature(objc_arc)
#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag on this file.
#endif

#if __has_include("SensorsAnalyticsSDK.h")
#import "SensorsAnalyticsSDK.h"
#else
#if __has_include(<SensorsAnalyticsSDK/SensorsAnalyticsSDK.h>)
#import <SensorsAnalyticsSDK/SensorsAnalyticsSDK.h>
#else
#import "SensorsAnalyticsSDK.h"
#endif

#import "RNSensorsAnalyticsModule.h"
#import "SAReactNativeManager.h"
#import "SAReactNativeEventProperty.h"

NSString *const kSAReactNativePluginVersion = @"react_native:2.0.8";

@implementation RNSensorsAnalyticsModule

Expand All @@ -36,7 +54,8 @@ @implementation RNSensorsAnalyticsModule

RCT_EXPORT_METHOD(track:(NSString *)event withProperties:(NSDictionary *)propertyDict){
@try {
[[SensorsAnalyticsSDK sharedInstance] track:event withProperties:propertyDict];
NSDictionary *properties = [SAReactNativeEventProperty eventProperties:propertyDict];
[[SensorsAnalyticsSDK sharedInstance] track:event withProperties:properties];
} @catch (NSException *exception) {
NSLog(@"[RNSensorsAnalytics] error:%@",exception);
}
Expand Down Expand Up @@ -97,7 +116,8 @@ @implementation RNSensorsAnalyticsModule
*/
RCT_EXPORT_METHOD(trackTimerEnd:(NSString *)event withProperties:(NSDictionary *)propertyDict){
@try {
[[SensorsAnalyticsSDK sharedInstance] trackTimerEnd:event withProperties:propertyDict];
NSDictionary *properties = [SAReactNativeEventProperty eventProperties:propertyDict];
[[SensorsAnalyticsSDK sharedInstance] trackTimerEnd:event withProperties:properties];
} @catch (NSException *exception) {
NSLog(@"[RNSensorsAnalytics] error:%@",exception);
}
Expand Down Expand Up @@ -165,7 +185,8 @@ @implementation RNSensorsAnalyticsModule
*/
RCT_EXPORT_METHOD(login:(NSString *)loginId){
@try {
[[SensorsAnalyticsSDK sharedInstance] login:loginId];
NSDictionary *properties = [SAReactNativeEventProperty eventProperties:nil];
[[SensorsAnalyticsSDK sharedInstance] login:loginId withProperties:properties];
} @catch (NSException *exception) {
NSLog(@"[RNSensorsAnalytics] error:%@",exception);
}
Expand Down
Loading

0 comments on commit 90bda7c

Please sign in to comment.