Skip to content

Commit

Permalink
CORE-294 react native sdk add conversion event + version bump to 3.7.1 (
Browse files Browse the repository at this point in the history
#219)

* sendEvent --> logConversion (with optional revenue)

attempt to use obj c name

Fix completion handler

double --> nsnumber

Add *

* Bump native sdk dep to 3.6.1-beta.1

* android changes

* add back sendEvent in index

* putMap not putArray

* callback name

* callback name

* RadarLogConversionCallback

* LogConversion without name

* add revenue for logConversion (ios)

* add revenue to logConversion for android

* bump android sdk dependency to 3.6.1

* android sdk version

* android sdk version

* remove setAdIdEnabled

* add tests for conversions

* stop emitting events when no listeners are registered

* ios and android sdk version bump

* android sdk version bump

* bump android + ios versions

* fix indentation

* set RadarReceiver after initialize

---------

Co-authored-by: David Goodfellow <61531269+david-goodfellow@users.noreply.github.com>
Co-authored-by: Joey Chik <joey.chik@radar.com>
Co-authored-by: Joey <122315545+joeychik-radar@users.noreply.github.com>
  • Loading branch information
4 people committed Mar 27, 2023
1 parent 256b1e5 commit 4675c24
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 33 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ repositories {

dependencies {
api 'com.facebook.react:react-native:+'
api 'io.radar:sdk:3.5.12'
api 'io.radar:sdk:3.7.1'
}
57 changes: 44 additions & 13 deletions android/src/main/java/io/radar/react/RNRadarModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,29 @@ public class RNRadarModule extends ReactContextBaseJavaModule implements Permiss
private static final int PERMISSIONS_REQUEST_CODE = 20160525; // random request code (Radar's birthday!)
private Promise mPermissionsRequestPromise;

private RNRadarReceiver receiver;
private int listenerCount = 0;

public RNRadarModule(ReactApplicationContext reactContext) {
super(reactContext);
Radar.setReceiver(new RNRadarReceiver());
receiver = new RNRadarReceiver();
}

@ReactMethod
public void addListener(String eventName) {
if (listenerCount == 0) {
receiver.hasListeners = true;
}

listenerCount += 1;
}

@ReactMethod
public void removeListeners(Integer count) {
listenerCount -= count;
if (listenerCount == 0) {
receiver.hasListeners = false;
}
}

@Override
Expand All @@ -60,6 +80,7 @@ public String getName() {
@ReactMethod
public void initialize(String publishableKey) {
Radar.initialize(getReactApplicationContext(), publishableKey);
Radar.setReceiver(receiver);
}

@ReactMethod
Expand Down Expand Up @@ -1088,27 +1109,31 @@ public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarRouteMa
}

@ReactMethod
public void sendEvent(String customType, ReadableMap metadata, final Promise promise) throws JSONException {
public void logConversion(ReadableMap optionsMap, final Promise promise) throws JSONException {
if (promise == null) {
return;
}

if (!optionsMap.hasKey("name")) {
promise.reject(Radar.RadarStatus.ERROR_BAD_REQUEST.toString(), Radar.RadarStatus.ERROR_BAD_REQUEST.toString());

return;
}

String name = optionsMap.getString("name");
Double revenue = optionsMap.hasKey("revenue") ? new Double(optionsMap.getDouble("revenue")) : null;
ReadableMap metadata = optionsMap.hasKey("metadata") ? optionsMap.getMap("metadata") : null;

JSONObject metadataObj = RNRadarUtils.jsonForMap(metadata);
Radar.sendEvent(customType, metadataObj, new Radar.RadarSendEventCallback() {
Radar.RadarLogConversionCallback callback = new Radar.RadarLogConversionCallback() {
@Override
public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarEvent[] events, @Nullable RadarUser user) {
public void onComplete(@NonNull Radar.RadarStatus status, @Nullable RadarEvent event) {
try {
if (status == Radar.RadarStatus.SUCCESS) {
WritableMap map = Arguments.createMap();
map.putString("status", status.toString());
if (location != null) {
map.putMap("location", RNRadarUtils.mapForJson(Radar.jsonForLocation(location)));
}
if (events != null) {
map.putArray("events", RNRadarUtils.arrayForJson(RadarEvent.toJson(events)));
}
if (user != null) {
map.putMap("user", RNRadarUtils.mapForJson(user.toJson()));
if (event != null) {
map.putMap("event", RNRadarUtils.mapForJson(event.toJson()));
}
promise.resolve(map);
} else {
Expand All @@ -1119,7 +1144,13 @@ public void onComplete(@NonNull Radar.RadarStatus status, @Nullable Location loc
promise.reject(Radar.RadarStatus.ERROR_SERVER.toString(), Radar.RadarStatus.ERROR_SERVER.toString());
}
}
});
};

if (revenue != null) {
Radar.logConversion(name, revenue, metadataObj, callback);
} else {
Radar.logConversion(name, metadataObj, callback);
}
}

}
4 changes: 3 additions & 1 deletion android/src/main/java/io/radar/react/RNRadarReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.facebook.react.ReactNativeHost;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule;

Expand All @@ -25,11 +26,12 @@ public class RNRadarReceiver extends RadarReceiver {

private ReactNativeHost reactNativeHost;
private static final String TAG = "RNRadarReceiver";
protected boolean hasListeners = false;

private void sendEvent(final String eventName, final Object data) {
final ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
ReactContext reactContext = reactInstanceManager.getCurrentReactContext();
if (reactContext != null) {
if (reactContext != null && hasListeners) {
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ios/Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "radarlabs/radar-sdk-ios" "3.5.12"
github "radarlabs/radar-sdk-ios" "3.7.2"
43 changes: 31 additions & 12 deletions ios/RNRadar.m
Original file line number Diff line number Diff line change
Expand Up @@ -965,29 +965,48 @@ - (void)didLogMessage:(NSString *)message {
}];
}

RCT_EXPORT_METHOD(sendEvent:(NSString*) customType metadata:(NSDictionary *)metadata resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
RCT_EXPORT_METHOD(logConversion:(NSDictionary *)optionsDict resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
if (optionsDict == nil) {
if (reject) {
reject([Radar stringForStatus:RadarStatusErrorBadRequest], [Radar stringForStatus:RadarStatusErrorBadRequest], nil);
}

return;
}

NSString *name = optionsDict[@"name"];
NSNumber *revenue = optionsDict[@"revenue"];
NSDictionary *metadata = optionsDict[@"metadata"];
if (name == nil) {
if (reject) {
reject([Radar stringForStatus:RadarStatusErrorBadRequest], [Radar stringForStatus:RadarStatusErrorBadRequest], nil);
}

return;
}

__block RCTPromiseResolveBlock resolver = resolve;
__block RCTPromiseRejectBlock rejecter = reject;
[Radar sendEvent:customType withMetadata:metadata completionHandler:^(RadarStatus status, CLLocation * _Nullable location, NSArray<RadarEvent *> * _Nullable events, RadarUser * _Nullable user) {

RadarLogConversionCompletionHandler completionHandler = ^(RadarStatus status, RadarEvent * _Nullable event) {
if (status == RadarStatusSuccess && resolver) {
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setObject:[Radar stringForStatus:status] forKey:@"status"];
if (location) {
[dict setObject:[Radar dictionaryForLocation:location] forKey:@"location"];
}
if (events) {
[dict setObject:[RadarEvent arrayForEvents:events] forKey:@"events"];
}
if (user) {
[dict setObject:[user dictionaryValue] forKey:@"user"];
if (event) {
[dict setObject:[event dictionaryValue] forKey:@"event"];
}
resolver(dict);
} else if (rejecter) {
rejecter([Radar stringForStatus:status], [Radar stringForStatus:status], nil);
}
resolver = nil;
rejecter = nil;
}];
};

if (revenue) {
[Radar logConversionWithName:name metadata:metadata completionHandler:completionHandler];
} else {
[Radar logConversionWithName:name revenue:revenue metadata:metadata completionHandler:completionHandler];
}
}
@end
9 changes: 7 additions & 2 deletions js/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ const getMatrix = options => (
NativeModules.RNRadar.getMatrix(options)
);

const sendEvent = (customType, metadata) => (
NativeModules.RNRadar.sendEvent(customType, metadata)
const logConversion = options => (
NativeModules.RNRadar.logConversion(options)
)

const sendEvent = (name, metadata) => (
NativeModules.RNRadar.sendEvent(name, metadata)
)

const on = (event, callback) => (
Expand Down Expand Up @@ -221,6 +225,7 @@ const Radar = {
ipGeocode,
getDistance,
getMatrix,
logConversion,
sendEvent,
on,
off,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "React Native module for Radar, the leading geofencing and location tracking platform",
"homepage": "https://radar.com",
"license": "Apache-2.0",
"version": "3.5.11",
"version": "3.7.1",
"main": "js/index.js",
"files": [
"android",
Expand Down
2 changes: 1 addition & 1 deletion react-native-radar.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ Pod::Spec.new do |s|
s.platform = :ios, "10.0"

s.dependency "React"
s.dependency "RadarSDK", "~> 3.5.12"
s.dependency "RadarSDK", "~> 3.7.2"
end
10 changes: 9 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ jest.mock('NativeModules', () => ({
setAnonymousTrackingEnabled: jest.fn(),
isTracking: jest.fn(),
getTrackingOptions: jest.fn(),
getTripOptions: jest.fn()
getTripOptions: jest.fn(),
logConversion: jest.fn(),
},
}));

Expand Down Expand Up @@ -524,4 +525,11 @@ describe('calls native implementation', () => {

expect(mockModule.getTripOptions).toHaveBeenCalledTimes(1);
});

test('logConversions', () => {
const options = { name: 'name' };
Radar.logConversion(options);

expect(mockModule.logConversion).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 4675c24

Please sign in to comment.