From 3ec776ad4259d8e1ccbf283f10870a4f13c0b75b Mon Sep 17 00:00:00 2001 From: Gabriel Bull Date: Fri, 23 Apr 2021 22:44:42 -0400 Subject: [PATCH] feat(analytics): add method to retrieve the app instance id from the service --- docs/analytics/usage/index.md | 12 ++++++++++++ .../analytics/UniversalFirebaseAnalyticsModule.java | 4 ++++ .../ReactNativeFirebaseAnalyticsModule.java | 11 +++++++++++ packages/analytics/e2e/analytics.e2e.js | 6 ++++++ .../ios/RNFBAnalytics/RNFBAnalyticsModule.m | 7 +++++++ packages/analytics/lib/index.d.ts | 13 +++++++++++++ packages/analytics/lib/index.js | 4 ++++ 7 files changed, 57 insertions(+) diff --git a/docs/analytics/usage/index.md b/docs/analytics/usage/index.md index f38d73987c..1485a588e5 100644 --- a/docs/analytics/usage/index.md +++ b/docs/analytics/usage/index.md @@ -120,6 +120,18 @@ with any of the following event names will throw an error. | `ad_click` | `ad_query` | `ad_exposure` | | `adunit_exposure` | `ad_activeiew` | +## App instance id + +Below is an example showing how to retrieve the app instance id of the application. This will return null on android +if FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE has been set to FirebaseAnalytics.ConsentStatus.DENIED and null on +iOS if ConsentType.analyticsStorage has been set to ConsentStatus.denied. + +```jsx +import analytics from '@react-native-firebase/analytics'; +// ... +const appInstanceId = await analytics().getAppInstanceId(); +``` + # firebase.json ## Disable Auto-Initialization diff --git a/packages/analytics/android/src/main/java/io/invertase/firebase/analytics/UniversalFirebaseAnalyticsModule.java b/packages/analytics/android/src/main/java/io/invertase/firebase/analytics/UniversalFirebaseAnalyticsModule.java index 7fbb9e60ae..bad38fc1ec 100644 --- a/packages/analytics/android/src/main/java/io/invertase/firebase/analytics/UniversalFirebaseAnalyticsModule.java +++ b/packages/analytics/android/src/main/java/io/invertase/firebase/analytics/UniversalFirebaseAnalyticsModule.java @@ -59,6 +59,10 @@ Task setSessionTimeoutDuration(long milliseconds) { }); } + Task getAppInstanceId() { + return FirebaseAnalytics.getInstance(getContext()).getAppInstanceId(); + } + Task setUserId(String id) { return Tasks.call(() -> { FirebaseAnalytics.getInstance(getContext()).setUserId(id); diff --git a/packages/analytics/android/src/reactnative/java/io/invertase/firebase/analytics/ReactNativeFirebaseAnalyticsModule.java b/packages/analytics/android/src/reactnative/java/io/invertase/firebase/analytics/ReactNativeFirebaseAnalyticsModule.java index 638d98991a..2d1ab6e6f8 100644 --- a/packages/analytics/android/src/reactnative/java/io/invertase/firebase/analytics/ReactNativeFirebaseAnalyticsModule.java +++ b/packages/analytics/android/src/reactnative/java/io/invertase/firebase/analytics/ReactNativeFirebaseAnalyticsModule.java @@ -75,6 +75,17 @@ public void setSessionTimeoutDuration(double milliseconds, Promise promise) { }); } + @ReactMethod + public void getAppInstanceId(Promise promise) { + module.getAppInstanceId().addOnCompleteListener(task -> { + if (task.isSuccessful()) { + promise.resolve(task.getResult()); + } else { + rejectPromiseWithExceptionMap(promise, task.getException()); + } + }); + } + @ReactMethod public void setUserId(String id, Promise promise) { module.setUserId(id).addOnCompleteListener(task -> { diff --git a/packages/analytics/e2e/analytics.e2e.js b/packages/analytics/e2e/analytics.e2e.js index bfbb3f7f1b..1dbd7c9602 100644 --- a/packages/analytics/e2e/analytics.e2e.js +++ b/packages/analytics/e2e/analytics.e2e.js @@ -58,6 +58,12 @@ describe('analytics()', function () { }); }); + describe('getAppInstanceId()', function () { + it('calls native fn without error', async function () { + await firebase.analytics().getAppInstanceId(); + }); + }); + describe('setUserId()', function () { it('allows a null values to be set', async function () { await firebase.analytics().setUserId(null); diff --git a/packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m b/packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m index 74d12dad13..b682bce972 100644 --- a/packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m +++ b/packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m @@ -135,6 +135,13 @@ - (dispatch_queue_t)methodQueue { return resolve([NSNull null]); } + RCT_EXPORT_METHOD(getAppInstanceId: + (RCTPromiseResolveBlock) resolve + rejecter: + (RCTPromiseRejectBlock) reject) { + return resolve([FIRAnalytics appInstanceID]); + } + #pragma mark - #pragma mark Private methods diff --git a/packages/analytics/lib/index.d.ts b/packages/analytics/lib/index.d.ts index 79eaab790b..d3bbbce951 100644 --- a/packages/analytics/lib/index.d.ts +++ b/packages/analytics/lib/index.d.ts @@ -682,6 +682,19 @@ export namespace FirebaseAnalyticsTypes { */ setSessionTimeoutDuration(milliseconds?: number): Promise; + /** + * Retrieve the app instance id of the application. + * + * #### Example + * + * ```js + * const appInstanceId = await firebase.analytics().getAppInstanceId(); + * ``` + * + * @returns Returns the app instance id or null on android if FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE has been set to FirebaseAnalytics.ConsentStatus.DENIED and null on iOS if ConsentType.analyticsStorage has been set to ConsentStatus.denied. + */ + getAppInstanceId(): Promise; + /** * Gives a user a unique identification. * diff --git a/packages/analytics/lib/index.js b/packages/analytics/lib/index.js index 4439024ec3..89de9caae5 100644 --- a/packages/analytics/lib/index.js +++ b/packages/analytics/lib/index.js @@ -130,6 +130,10 @@ class FirebaseAnalyticsModule extends FirebaseModule { return this.native.setSessionTimeoutDuration(milliseconds); } + getAppInstanceId() { + return this.native.getAppInstanceId(); + } + setUserId(id) { if (!isNull(id) && !isString(id)) { throw new Error("firebase.analytics().setUserId(*) 'id' expected a string value.");