From 5341b7ef85922c36082d91b2fb0d9d332363196d Mon Sep 17 00:00:00 2001 From: Ren6 Date: Tue, 4 Jun 2024 15:03:54 +0300 Subject: [PATCH] add paywallShown and paywallClosed methods --- .../ApphudDataTransformer.kt | 1 - .../reactnativeapphudsdk/ApphudSdkModule.kt | 21 ++++++++++++++-- example/ios/.xcode.env | 1 + .../project.pbxproj | 2 +- example/ios/Podfile.lock | 2 +- example/src/screens/ActionsScreen.tsx | 10 +++----- example/src/screens/LoginScreen.tsx | 3 +++ example/src/screens/PaywallScreen.tsx | 1 + ios/.xcode.env | 1 + ios/ApphudSdk.m | 2 ++ ios/ApphudSdk.swift | 24 ++++++++++++++++++- package.json | 2 +- src/ApphudSdkType.ts | 14 ++++++++++- 13 files changed, 69 insertions(+), 15 deletions(-) create mode 100644 example/ios/.xcode.env create mode 100644 ios/.xcode.env diff --git a/android/src/main/java/com/reactnativeapphudsdk/ApphudDataTransformer.kt b/android/src/main/java/com/reactnativeapphudsdk/ApphudDataTransformer.kt index f85fae8..791cb9e 100644 --- a/android/src/main/java/com/reactnativeapphudsdk/ApphudDataTransformer.kt +++ b/android/src/main/java/com/reactnativeapphudsdk/ApphudDataTransformer.kt @@ -52,7 +52,6 @@ class ApphudDataTransformer { fun getApphudPaywallMap(paywall: ApphudPaywall): WritableNativeMap { val result: WritableNativeMap = WritableNativeMap() - Apphud.enableDebugLogs() result.putString("identifier", paywall.identifier) result.putBoolean("isDefault", paywall.default) result.putString("experimentName", paywall.experimentName) diff --git a/android/src/main/java/com/reactnativeapphudsdk/ApphudSdkModule.kt b/android/src/main/java/com/reactnativeapphudsdk/ApphudSdkModule.kt index 62f2b2a..322cad0 100644 --- a/android/src/main/java/com/reactnativeapphudsdk/ApphudSdkModule.kt +++ b/android/src/main/java/com/reactnativeapphudsdk/ApphudSdkModule.kt @@ -22,7 +22,7 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ init { HeadersInterceptor.X_SDK = "reactnative" - HeadersInterceptor.X_SDK_VERSION = "2.0.0" + HeadersInterceptor.X_SDK_VERSION = "2.1.0" listener = ApphudListenerHandler(reactContext) listener?.let { Apphud.setListener(it) } } @@ -73,6 +73,22 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ } } + @ReactMethod + fun paywallShown(identifier: String) { + val paywall = Apphud.paywalls().firstOrNull { it.identifier == identifier } + paywall?.let { + Apphud.paywallShown(it) + } + } + + @ReactMethod + fun paywallClosed(identifier: String) { + val paywall = Apphud.paywalls().firstOrNull { it.identifier == identifier } + paywall?.let { + Apphud.paywallClosed(it) + } + } + @ReactMethod fun purchase(args: ReadableMap, promise: Promise) { val productId = args.getString("productId") @@ -96,7 +112,7 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ val isSub = product?.productDetails?.productType?.lowercase() == "subs" val offerToken = args.getString("offerToken") - val isConsumable = args.getBoolean("isConsumable") + val isConsumable = if (args.hasKey("isConsumable")) args.getBoolean("isConsumable") else false if (product == null) { promise.reject("Error", "Product not found") @@ -270,6 +286,7 @@ class ApphudSdkModule(reactContext: ReactApplicationContext) : ReactContextBaseJ Apphud.collectDeviceIdentifiers() } + @ReactMethod fun enableDebugLogs() { Apphud.enableDebugLogs() diff --git a/example/ios/.xcode.env b/example/ios/.xcode.env new file mode 100644 index 0000000..67dfdf7 --- /dev/null +++ b/example/ios/.xcode.env @@ -0,0 +1 @@ +export NODE_BINARY=/opt/homebrew/opt/node@20/bin/node \ No newline at end of file diff --git a/example/ios/ApphudSdkExample.xcodeproj/project.pbxproj b/example/ios/ApphudSdkExample.xcodeproj/project.pbxproj index ae4ff11..ac73e72 100644 --- a/example/ios/ApphudSdkExample.xcodeproj/project.pbxproj +++ b/example/ios/ApphudSdkExample.xcodeproj/project.pbxproj @@ -399,7 +399,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh\n"; + shellScript = "export NODE_BINARY=/opt/homebrew/opt/node@20/bin/node\n../node_modules/react-native/scripts/react-native-xcode.sh\n"; }; 2D02E4CB1E0B4B27006451C7 /* Bundle React Native Code And Images */ = { isa = PBXShellScriptBuildPhase; diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 426fa30..5ac9712 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -580,4 +580,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 47a16e2930bdcf8baadcf3fe4873c7f378dc927c -COCOAPODS: 1.14.2 +COCOAPODS: 1.15.2 diff --git a/example/src/screens/ActionsScreen.tsx b/example/src/screens/ActionsScreen.tsx index baaa277..3e0e7ee 100644 --- a/example/src/screens/ActionsScreen.tsx +++ b/example/src/screens/ActionsScreen.tsx @@ -44,17 +44,13 @@ export default function ActionsScreen({ navigation }: Props) { const [paywalls, setPaywalls] = React.useState>(); const callAll = () => { - // ApphudSdk.enableDebugLogs(); + ApphudSdk.enableDebugLogs(); // ApphudSdk.setAdvertisingIdentifier('42ed88fd-b446-4eb1-81ae-83e3025c04cf') // ApphudSdk.userId().then((userId) => console.log(`Apphud: userId: ${userId}`)); - // ApphudSdk.hasActiveSubscription().then((hasActiveSubscription) => - // console.log(`Apphud: hasActiveSubscription: ${hasActiveSubscription}`) - // ); - // ApphudSdk.hasPremiumAccess().then((hasPremiumAccess) => - // console.log(`Apphud: hasPremiumAccess: ${hasPremiumAccess}`) - // ); + ApphudSdk.hasActiveSubscription().then((active) => console.log('START Has Active Subscription: = ' + active)); + // ApphudSdk.setUserProperty({key: 'some_string_key2', value: 'some_string_valueee', setOnce: true}) // ApphudSdk.setUserProperty({key: 'some_float_key3', value: 1.45, setOnce: true}) diff --git a/example/src/screens/LoginScreen.tsx b/example/src/screens/LoginScreen.tsx index 7c7a9ea..6b40d0c 100644 --- a/example/src/screens/LoginScreen.tsx +++ b/example/src/screens/LoginScreen.tsx @@ -20,6 +20,9 @@ export default function LoginScreen({ navigation }: Props) { const onStartHandler = () => { ApphudSdk.start({ apiKey, userId, deviceId, observerMode: false }) + + // ApphudSdk.hasActiveSubscription().then((active) => console.log('START Has Active Subscription: = ' + hasActive)); + navigation.navigate('Actions') } diff --git a/example/src/screens/PaywallScreen.tsx b/example/src/screens/PaywallScreen.tsx index d44195f..17a958c 100644 --- a/example/src/screens/PaywallScreen.tsx +++ b/example/src/screens/PaywallScreen.tsx @@ -53,6 +53,7 @@ export default function PaywallScreen({ route, navigation}: { route: any, naviga for (const paywall of paywalls) { if (paywall.identifier === route.params.paywallId) { setCurrentPaywall(paywall); + await ApphudSdk.paywallShown(paywall.identifier); const productsPropsList: ProductProps[] = preparedProducts(paywall.products); setProductsProps(productsPropsList); return paywall; diff --git a/ios/.xcode.env b/ios/.xcode.env new file mode 100644 index 0000000..67dfdf7 --- /dev/null +++ b/ios/.xcode.env @@ -0,0 +1 @@ +export NODE_BINARY=/opt/homebrew/opt/node@20/bin/node \ No newline at end of file diff --git a/ios/ApphudSdk.m b/ios/ApphudSdk.m index 06b0943..ce3b66c 100644 --- a/ios/ApphudSdk.m +++ b/ios/ApphudSdk.m @@ -46,6 +46,8 @@ @interface RCT_EXTERN_MODULE(ApphudSdk, NSObject) RCT_EXTERN_METHOD(collectDeviceIdentifiers) RCT_EXTERN_METHOD(setAdvertisingIdentifier:(NSString*)idfa) +RCT_EXTERN_METHOD(paywallShown:(NSString*)identifier) +RCT_EXTERN_METHOD(paywallClosed:(NSString*)identifier) RCT_EXTERN_METHOD(optOutOfTracking) RCT_EXTERN_METHOD(enableDebugLogs) RCT_EXTERN_METHOD(logout:(RCTPromiseResolveBlock)resolve) diff --git a/ios/ApphudSdk.swift b/ios/ApphudSdk.swift index 8bf3567..81eabc6 100644 --- a/ios/ApphudSdk.swift +++ b/ios/ApphudSdk.swift @@ -6,7 +6,7 @@ class ApphudSdk: NSObject { override init() { ApphudHttpClient.shared.sdkType = "reactnative"; - ApphudHttpClient.shared.sdkVersion = "2.0.0"; + ApphudHttpClient.shared.sdkVersion = "2.1.0"; } @objc(start:) @@ -15,6 +15,9 @@ class ApphudSdk: NSObject { let userID = options["userId"] as? String; let observerMode = options["observerMode"] as? Bool ?? true; DispatchQueue.main.async { + #if DEBUG + ApphudUtils.enableAllLogs() + #endif Apphud.start(apiKey: apiKey, userID: userID, observerMode: observerMode); } } @@ -130,6 +133,25 @@ class ApphudSdk: NSObject { } } + @objc(paywallShown:) + func paywallShown(identifier: String) { + print("Paywall Shown: \(identifier)") + Task { + if let paywall = await Apphud.paywalls().first(where: { $0.identifier == identifier }) { + Apphud.paywallShown(paywall) + } + } + } + + @objc(paywallClosed:) + func paywallClosed(identifier: String) { + Task { + if let paywall = await Apphud.paywalls().first(where: { $0.identifier == identifier }) { + Apphud.paywallClosed(paywall) + } + } + } + @objc(subscription:withRejecter:) func subscription(resolve: RCTPromiseResolveBlock, reject:RCTPromiseRejectBlock) -> Void { guard let subscription = Apphud.subscription() else { diff --git a/package.json b/package.json index 316c16d..5756e9e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@apphud/react-native-apphud-sdk", - "version": "2.0.0", + "version": "2.1.0", "description": "Apphud SDK for react native", "main": "lib/commonjs/index", "module": "lib/module/index", diff --git a/src/ApphudSdkType.ts b/src/ApphudSdkType.ts index a8a02d3..04070da 100644 --- a/src/ApphudSdkType.ts +++ b/src/ApphudSdkType.ts @@ -34,7 +34,19 @@ export type ApphudSdkType = { * Method returns immediately if paywalls are cached or already loaded. * @returns paywalls configured in Apphud Dashboard > Product Hub > Paywalls. */ - paywalls(): Promise>; + paywalls(): Promise>; + + /** + * Available on iOS and Android + * Logs a "Paywall Shown" (Paywall View) event which is required for A/B Testing Analytics. + */ + paywallShown(identifier: string): void; + + /** + * Available on iOS and Android + * Logs a "Paywall Closed" event. Optional. + */ + paywallClosed(identifier: string): void; /** * Available on iOS and Android.