Skip to content

Commit

Permalink
Release 2.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
weiqiangliu committed Jul 31, 2021
1 parent cd19744 commit 9b5c2af
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 240 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.1.2"
s.version = "2.1.3"
s.summary = "The official React Native SDK of Sensors Analytics."
s.description = <<-DESC
神策分析 RN 组件
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.1.2";
public static final String VERSION = "2.1.3";
@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 @@ -20,17 +20,15 @@

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
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.property.PluginVersionInterceptor;
import com.sensorsdata.analytics.property.RNPropertyManager;
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 @@ -47,13 +45,13 @@
* ReadableArray -> Array
*/

public class RNSensorsDataModule extends ReactContextBaseJavaModule{
public class RNSensorsDataModule extends ReactContextBaseJavaModule {

public RNSensorsDataModule(ReactApplicationContext reactContext) {
super(reactContext);
try{
try {
reactContext.addLifecycleEventListener(new SensorsDataLifecycleListener());
}catch(Exception e){
} catch (Exception e) {

}
RNAgent.ignoreView();
Expand All @@ -79,23 +77,23 @@ public void trackViewClick(int viewId) {

@ReactMethod
public void trackViewScreen(ReadableMap params) {
try{
try {
if (params != null) {
JSONObject jsonParams = RNUtils.convertToJSONObject(params);
JSONObject properties = null;
if(jsonParams.has("sensorsdataparams")){
if (jsonParams.has("sensorsdataparams")) {
properties = jsonParams.optJSONObject("sensorsdataparams");
}
String url = null;
if(jsonParams.has("sensorsdataurl")){
if (jsonParams.has("sensorsdataurl")) {
url = jsonParams.getString("sensorsdataurl");
}
if(url == null){
if (url == null) {
return;
}
RNAgent.trackViewScreen(url, properties, true);
}
}catch(Exception e){
} catch (Exception e) {
SALog.printStackTrace(e);
}
}
Expand All @@ -107,13 +105,11 @@ public void saveViewProperties(int viewId, boolean clickable, ReadableMap viewPr

class SensorsDataLifecycleListener implements LifecycleEventListener {
public void onHostResume() {
RNViewUtils.setScreenVisiable(true);
RNViewUtils.setCurrentActivity(getCurrentActivity());
RNViewUtils.onActivityResumed(getCurrentActivity());
}

public void onHostPause() {
RNViewUtils.setScreenVisiable(false);
RNViewUtils.clearCurrentActivityReference();
RNViewUtils.onActivityPaused();
}

public void onHostDestroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,40 @@
package com.sensorsdata.analytics.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;
import android.view.View;

import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.UIImplementation;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.UIViewOperationQueue;
import java.lang.ref.SoftReference;
import com.sensorsdata.analytics.android.sdk.SALog;

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import android.view.ViewGroup;
import android.view.View;
import android.view.ViewParent;

import org.json.JSONObject;

import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

public class RNViewUtils {

private static SoftReference mSoftCurrentActivityReference;
private static WeakReference mWeakCurrentActivityReference;
private static String currentTitle;
private static String currentScreenName;
public static boolean isScreenVisiable = false;
private static JSONObject properties = new JSONObject();
private static JSONObject screenProperties;
private static WeakReference onTouchViewReference;

private static WeakHashMap<Activity, JSONObject> mScreenMap = new WeakHashMap<>();

public static void setOnTouchView(View nativeTargetView) {
onTouchViewReference = new WeakReference(nativeTargetView);
}

public static View getViewByTag(int viewTag) {
View clickView = null;
try{
try {
Activity currentActivity = getCurrentActivity();
if(currentActivity != null){
if (currentActivity != null) {
clickView = currentActivity.findViewById(viewTag);
}
if(clickView == null){
if (clickView == null) {
clickView = getTouchViewByTag(viewTag);
}
}catch (Exception ignored){
} catch (Exception ignored) {

}
return clickView;
Expand Down Expand Up @@ -113,56 +103,80 @@ private static View getClickViewInChild(int viewId, ViewGroup currentView) {
return null;
}

public static void saveScreenAndTitle(String screenName,String title){
public static void saveScreenAndTitle(String screenName, String title) {
currentScreenName = screenName;
currentTitle = title;
try{
properties.put("$title", title);
properties.put("$screen_name", screenName);
}catch (Exception ignored){
try {
screenProperties = new JSONObject();
screenProperties.put("$title", title);
screenProperties.put("$screen_name", screenName);
} catch (Exception ignored) {

}
Activity currentActivity;
if ((currentActivity = getCurrentActivity()) != null) {
mScreenMap.put(currentActivity, screenProperties);
}
}

public static String getTitle(){
public static String getTitle() {
return currentTitle;
}

public static String getScreenName(){
public static String getScreenName() {
return currentScreenName;
}

/**
* 供可视化调用,返回 $title,$screen_name,勿删
*
* @return json 格式
*/
public static String getVisualizeProperties(){
if(!isScreenVisiable){
public static String getVisualizeProperties() {
//当前页面不可见或无 $screen_name $title 时,可视化获取原生页面信息
if (!isScreenVisiable || screenProperties == null) {
return "";
}
return properties.toString();
return screenProperties.toString();
}

public static void setScreenVisiable(boolean isVisiable){
public static void setScreenVisiable(boolean isVisiable) {
isScreenVisiable = isVisiable;
}

public static void setCurrentActivity(Activity currentActivity) {
private static void setCurrentActivity(Activity currentActivity) {
clearCurrentActivityReference();
mSoftCurrentActivityReference = new SoftReference(currentActivity);
mWeakCurrentActivityReference = new WeakReference(currentActivity);
JSONObject properties = mScreenMap.get(currentActivity);
if (properties != null && properties.has("$screen_name")) {
saveScreenAndTitle(properties.optString("$screen_name"), properties.optString("$title"));
} else {
currentScreenName = null;
currentTitle = null;
screenProperties = null;
}
}

private static Activity getCurrentActivity(){
if(mSoftCurrentActivityReference == null){
private static Activity getCurrentActivity() {
if (mWeakCurrentActivityReference == null) {
return null;
}
return (Activity)mSoftCurrentActivityReference.get();
return (Activity) mWeakCurrentActivityReference.get();
}

public static void clearCurrentActivityReference() {
if(mSoftCurrentActivityReference != null){
mSoftCurrentActivityReference.clear();
mSoftCurrentActivityReference = null;
if (mWeakCurrentActivityReference != null) {
mWeakCurrentActivityReference.clear();
mWeakCurrentActivityReference = null;
}
}

public static void onActivityResumed(Activity currentActivity) {
setScreenVisiable(true);
setCurrentActivity(currentActivity);
}

public static void onActivityPaused() {
setScreenVisiable(false);
}
}
2 changes: 1 addition & 1 deletion ios/RNSensorsAnalyticsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#import "SAReactNativeManager.h"
#import "SAReactNativeEventProperty.h"

NSString *const kSAReactNativePluginVersion = @"react_native:2.1.2";
NSString *const kSAReactNativePluginVersion = @"react_native:2.1.3";

@implementation RNSensorsAnalyticsModule

Expand Down
6 changes: 0 additions & 6 deletions ios/RNSensorsAnalyticsModule.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
A18E9FB421A6AEDD00A66E41 /* RNSensorsAnalyticsModule.m in Sources */ = {isa = PBXBuildFile; fileRef = A18E9FB221A6AEDD00A66E41 /* RNSensorsAnalyticsModule.m */; };
FC045CC2246D191C007117C1 /* SAReactNativeswizzler.m in Sources */ = {isa = PBXBuildFile; fileRef = FC045CC1246D191C007117C1 /* SAReactNativeswizzler.m */; };
FC39004C244715AE00F486A7 /* SAReactNativeManager.m in Sources */ = {isa = PBXBuildFile; fileRef = FC39004B244715AE00F486A7 /* SAReactNativeManager.m */; };
FC6EF7A9259DA6A40099EEB5 /* SAReactNativeEventProperty.m in Sources */ = {isa = PBXBuildFile; fileRef = FC6EF7A8259DA6A40099EEB5 /* SAReactNativeEventProperty.m */; };
FC9EE671243731C000C45D16 /* RNSensorsDataModule.m in Sources */ = {isa = PBXBuildFile; fileRef = FC9EE670243731C000C45D16 /* RNSensorsDataModule.m */; };
Expand All @@ -31,8 +30,6 @@
A18E9FA621A6AB4300A66E41 /* libRNSensorsAnalyticsModule.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNSensorsAnalyticsModule.a; sourceTree = BUILT_PRODUCTS_DIR; };
A18E9FB221A6AEDD00A66E41 /* RNSensorsAnalyticsModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNSensorsAnalyticsModule.m; sourceTree = "<group>"; };
A18E9FB321A6AEDD00A66E41 /* RNSensorsAnalyticsModule.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNSensorsAnalyticsModule.h; sourceTree = "<group>"; };
FC045CC0246D191C007117C1 /* SAReactNativeswizzler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SAReactNativeswizzler.h; sourceTree = "<group>"; };
FC045CC1246D191C007117C1 /* SAReactNativeswizzler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SAReactNativeswizzler.m; sourceTree = "<group>"; };
FC39004A244715AE00F486A7 /* SAReactNativeManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SAReactNativeManager.h; sourceTree = "<group>"; };
FC39004B244715AE00F486A7 /* SAReactNativeManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SAReactNativeManager.m; sourceTree = "<group>"; };
FC6EF7A7259DA6A40099EEB5 /* SAReactNativeEventProperty.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SAReactNativeEventProperty.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -67,8 +64,6 @@
FC6EF7A8259DA6A40099EEB5 /* SAReactNativeEventProperty.m */,
FCD3CF2925466B7800758260 /* SAReactNativeCategory.h */,
FCD3CF2A25466B7800758260 /* SAReactNativeCategory.m */,
FC045CC0246D191C007117C1 /* SAReactNativeswizzler.h */,
FC045CC1246D191C007117C1 /* SAReactNativeswizzler.m */,
A18E9FA721A6AB4300A66E41 /* Products */,
);
sourceTree = "<group>";
Expand Down Expand Up @@ -138,7 +133,6 @@
buildActionMask = 2147483647;
files = (
A18E9FB421A6AEDD00A66E41 /* RNSensorsAnalyticsModule.m in Sources */,
FC045CC2246D191C007117C1 /* SAReactNativeswizzler.m in Sources */,
FC39004C244715AE00F486A7 /* SAReactNativeManager.m in Sources */,
FC6EF7A9259DA6A40099EEB5 /* SAReactNativeEventProperty.m in Sources */,
FCD3CF2B25466B7800758260 /* SAReactNativeCategory.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion ios/RNSensorsDataModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ @implementation RNSensorsDataModule
*
* @param reactTag 当前控件唯一标识符
* @param clickable 当前控件可点击状态
* @param paramters 当前控件自定义参数 (预留字段,暂不支持)
* @param paramters 当前控件自定义参数
*
*/
RCT_EXPORT_METHOD(saveViewProperties:(NSInteger)reactTag clickable:(BOOL)clickable paramters:(NSDictionary *)paramters) {
Expand Down
24 changes: 23 additions & 1 deletion ios/SAReactNativeCategory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,36 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "SAReactNativeManager.h"

NS_ASSUME_NONNULL_BEGIN

#pragma mark - View Property
@interface SAReactNativeViewProperty : NSObject

/// View 唯一标识符
@property (nonatomic, strong) NSNumber *reactTag;
/// View 可点击状态
@property (nonatomic, assign) BOOL clickable;
/// View 自定义属性
@property (nonatomic, strong) NSDictionary *properties;

@end

@interface UIView (SAReactNative)

/// 用于记录 view 关联的页面信息
@property (nonatomic, copy) NSDictionary *sa_reactnative_screenProperties;

@end

@interface UIViewController (SAReactNative)

/// 触发页面浏览时, 记录页面信息 (和 RCTRootView 关联: rctRootView.reactViewController)
@property (nonatomic, copy) NSDictionary *sa_reactnative_screenProperties;

/// 用于记录 view 自定义属性 (和 RCTRootView 关联: rctRootView.reactViewController)
@property (nonatomic, copy) NSSet<SAReactNativeViewProperty *> *sa_reactnative_viewProperties;

@end

NS_ASSUME_NONNULL_END
Loading

0 comments on commit 9b5c2af

Please sign in to comment.