Skip to content

Commit 46bd345

Browse files
committed
simplify and use proxy addEvent call
1 parent 7cc69e7 commit 46bd345

File tree

10 files changed

+79
-56
lines changed

10 files changed

+79
-56
lines changed

android/src/main/java/com/urbanairship/reactnative/AirshipModule.kt

+8-1
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,17 @@ class AirshipModule internal constructor(val context: ReactApplicationContext) :
454454
}
455455
}
456456

457+
@ReactMethod
458+
override fun addCustomEvent(event: ReadableMap?, promise: Promise) {
459+
promise.resolveResult {
460+
proxy.analytics.addEvent(Utils.convertMap(event).toJsonValue())
461+
}
462+
}
463+
457464
@ReactMethod
458465
override fun actionRun(action: ReadableMap, promise: Promise) {
459466
promise.resolveDeferred<ActionValue> { callback ->
460-
proxy.actions.runAction(requireNotNull(action.getString("_name")), Utils.convertDynamic(action.getDynamic("_value")))
467+
proxy.actions.runAction(requireNotNull(action.getString("name")), Utils.convertDynamic(action.getDynamic("value")))
461468
.addResultCallback { actionResult ->
462469
if (actionResult != null && actionResult.status == ActionResult.STATUS_COMPLETED) {
463470
callback(actionResult.value, null)

android/src/oldarch/java/com/urbanairship/reactnative/AirshipSpec.kt

+7
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,13 @@ abstract class AirshipSpec internal constructor(context: ReactApplicationContext
237237
promise: Promise
238238
)
239239

240+
@ReactMethod
241+
@com.facebook.proguard.annotations.DoNotStrip
242+
abstract fun addCustomEvent(
243+
event: ReadableMap?,
244+
promise: Promise
245+
)
246+
240247
@ReactMethod
241248
@com.facebook.proguard.annotations.DoNotStrip
242249
abstract fun actionRun(

ios/AirshipReactNative.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,8 @@ public extension AirshipReactNative {
330330
public extension AirshipReactNative {
331331
func actionsRun(action: [String: Any]) async throws-> Any? {
332332
return try await AirshipProxy.shared.action.runAction(
333-
action["_name"] as! String,
334-
value: action["_value"] is NSNull ? nil : try AirshipJSON.wrap(action["_value"])
333+
action["name"] as! String,
334+
value: action["value"] is NSNull ? nil : try AirshipJSON.wrap(action["value"])
335335
)
336336
}
337337
}
@@ -350,6 +350,10 @@ public extension AirshipReactNative {
350350
key: key
351351
)
352352
}
353+
354+
func addCustomEvent(_ json: Any) throws {
355+
try AirshipProxy.shared.analytics.addEvent(json)
356+
}
353357
}
354358

355359
// Contact

ios/RTNAirship.mm

+11
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,17 @@ + (BOOL)requiresMainQueueSetup {
374374
[self handleResult:nil error:error resolve:resolve reject:reject];
375375
}
376376

377+
RCT_REMAP_METHOD(addCustomEvent,
378+
addCustomEvent:(NSDictionary *)event
379+
resolve:(RCTPromiseResolveBlock)resolve
380+
reject:(RCTPromiseRejectBlock)reject) {
381+
NSError *error;
382+
[AirshipReactNative.shared addCustomEvent:event
383+
error:&error];
384+
385+
[self handleResult:nil error:error resolve:resolve reject:reject];
386+
}
387+
377388
RCT_REMAP_METHOD(contactEditAttributes,
378389
contactEditAttributes:(NSArray *)operations
379390
resolve:(RCTPromiseResolveBlock)resolve

src/Action.ts

-34
This file was deleted.

src/AirshipActions.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { JsonValue } from './types';
2-
import { Action } from './Action';
32

43
/**
54
* Airship actions.
@@ -10,13 +9,15 @@ export class AirshipActions {
109
/**
1110
* Runs an Airship action.
1211
*
13-
* @param action The Airship Action.
12+
* @param actionName The name of the action.
13+
* @param actionValue The action's value.
1414
* @return A promise that returns the action result if the action
1515
* successfully runs, or the Error if the action was unable to be run.
1616
*/
1717
public run(
18-
action: Action
18+
actionName: String,
19+
actionValue: JsonValue
1920
): Promise<JsonValue | null | undefined> {
20-
return this.module.actionRun(action);
21+
return this.module.actionRun({name: actionName, value: actionValue});
2122
}
2223
}

src/AirshipAnalytics.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { Action } from "./Action";
21
import { CustomEvent } from "./CustomEvent";
32

43
/**
@@ -33,15 +32,7 @@ export class AirshipAnalytics {
3332
* @return A promise that returns null if resolved, or an Error if the
3433
* custom event is rejected.
3534
*/
36-
public addCustomEvent(event: CustomEvent): Promise<null | Error> {
37-
let action = new Action("add_custom_event_action", event.toJsonValue())
38-
39-
return new Promise((resolve, reject) => {
40-
this.module.actionRun(action).then(() => {
41-
resolve(null)
42-
}, (error: Error) => {
43-
reject(error)
44-
})
45-
})
35+
public addCustomEvent(event: CustomEvent): Promise<void> {
36+
return this.module.addCustomEvent(event.toJsonValue());
4637
}
4738
}

src/CustomEvent.ts

+39-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export class CustomEvent {
1212
_value?: number;
1313
_properties: JsonObject;
1414
_transactionId?: string;
15+
_interactionId?: string;
16+
_interactionType?: string;
1517

1618
/**
1719
* Custom event constructor.
@@ -39,6 +41,34 @@ export class CustomEvent {
3941
this._transactionId = value;
4042
}
4143

44+
/**
45+
* Gets the event's interaction ID.
46+
*/
47+
get interactionId(): string | undefined {
48+
return this._interactionId;
49+
}
50+
51+
/**
52+
* Sets the event's interaction ID.
53+
*/
54+
set interactionId(value: string | undefined) {
55+
this._interactionId = value;
56+
}
57+
58+
/**
59+
* Gets the event's interaction Type.
60+
*/
61+
get interactionType(): string | undefined {
62+
return this._interactionType;
63+
}
64+
65+
/**
66+
* Sets the event's interaction Type.
67+
*/
68+
set interactionType(value: string | undefined) {
69+
this._interactionType = value;
70+
}
71+
4272
/**
4373
* Adds a property to the custom event.
4474
*
@@ -56,13 +86,19 @@ export class CustomEvent {
5686
*/
5787
toJsonValue(): JsonValue {
5888
let jsonObject: JsonObject = {};
59-
jsonObject.event_name = this._name;
89+
jsonObject.eventName = this._name;
6090
if (this._value) {
61-
jsonObject.event_value = this._value;
91+
jsonObject.eventValue = this._value;
6292
}
6393
jsonObject.properties = this._properties;
6494
if (this._transactionId) {
65-
jsonObject.transaction_id = this._transactionId;
95+
jsonObject.transactionId = this._transactionId;
96+
}
97+
if (this._interactionId) {
98+
jsonObject.interactionId = this._interactionId;
99+
}
100+
if (this._interactionType) {
101+
jsonObject.interactionType = this._interactionType;
66102
}
67103
return jsonObject;
68104
}

src/NativeRTNAirship.ts

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ export interface Spec extends TurboModule {
6666
// Analytics
6767
analyticsTrackScreen(screen?: string): Promise<void>;
6868
analyticsAssociateIdentifier(key: string, identifier?: string): Promise<void>;
69+
addCustomEvent(event: Object): Promise<void>;
6970

7071
// Action
7172
actionRun(action: Object): Promise<Object | Error>;

src/index.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export { SubscriptionListEditor } from './SubscriptionListEditor';
1919
export { TagGroupEditor } from './TagGroupEditor';
2020
export { ScopedSubscriptionListEditor } from './ScopedSubscriptionListEditor';
2121
export { AttributeEditor } from './AttributeEditor';
22-
export { Action } from './Action';
2322

2423
export * from './types';
2524
export * from './MessageView';

0 commit comments

Comments
 (0)