Skip to content

Commit a51e97b

Browse files
authored
feat(analytics, appInstanceId): implement getAppIntanceId() method for GA4 use (#5210)
1 parent 589fcb0 commit a51e97b

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

docs/analytics/usage/index.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ with any of the following event names will throw an error.
120120
| `ad_click` | `ad_query` | `ad_exposure` |
121121
| `adunit_exposure` | `ad_activeiew` |
122122

123+
## App instance id
124+
125+
Below is an example showing how to retrieve the app instance id of the application. This will return null on android
126+
if FirebaseAnalytics.ConsentType.ANALYTICS_STORAGE has been set to FirebaseAnalytics.ConsentStatus.DENIED and null on
127+
iOS if ConsentType.analyticsStorage has been set to ConsentStatus.denied.
128+
129+
```jsx
130+
import analytics from '@react-native-firebase/analytics';
131+
// ...
132+
const appInstanceId = await analytics().getAppInstanceId();
133+
```
134+
123135
# firebase.json
124136

125137
## Disable Auto-Initialization

packages/analytics/android/src/main/java/io/invertase/firebase/analytics/UniversalFirebaseAnalyticsModule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ Task<Void> setSessionTimeoutDuration(long milliseconds) {
5959
});
6060
}
6161

62+
Task<String> getAppInstanceId() {
63+
return FirebaseAnalytics.getInstance(getContext()).getAppInstanceId();
64+
}
65+
6266
Task<Void> setUserId(String id) {
6367
return Tasks.call(() -> {
6468
FirebaseAnalytics.getInstance(getContext()).setUserId(id);

packages/analytics/android/src/reactnative/java/io/invertase/firebase/analytics/ReactNativeFirebaseAnalyticsModule.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public void setSessionTimeoutDuration(double milliseconds, Promise promise) {
7575
});
7676
}
7777

78+
@ReactMethod
79+
public void getAppInstanceId(Promise promise) {
80+
module.getAppInstanceId().addOnCompleteListener(task -> {
81+
if (task.isSuccessful()) {
82+
promise.resolve(task.getResult());
83+
} else {
84+
rejectPromiseWithExceptionMap(promise, task.getException());
85+
}
86+
});
87+
}
88+
7889
@ReactMethod
7990
public void setUserId(String id, Promise promise) {
8091
module.setUserId(id).addOnCompleteListener(task -> {

packages/analytics/e2e/analytics.e2e.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ describe('analytics()', function () {
5858
});
5959
});
6060

61+
describe('getAppInstanceId()', function () {
62+
it('calls native fn without error', async function () {
63+
await firebase.analytics().getAppInstanceId();
64+
});
65+
});
66+
6167
describe('setUserId()', function () {
6268
it('allows a null values to be set', async function () {
6369
await firebase.analytics().setUserId(null);

packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ - (dispatch_queue_t)methodQueue {
135135
return resolve([NSNull null]);
136136
}
137137

138+
RCT_EXPORT_METHOD(getAppInstanceId:
139+
(RCTPromiseResolveBlock) resolve
140+
rejecter:
141+
(RCTPromiseRejectBlock) reject) {
142+
return resolve([FIRAnalytics appInstanceID]);
143+
}
144+
138145
#pragma mark -
139146
#pragma mark Private methods
140147

packages/analytics/lib/index.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,19 @@ export namespace FirebaseAnalyticsTypes {
682682
*/
683683
setSessionTimeoutDuration(milliseconds?: number): Promise<void>;
684684

685+
/**
686+
* Retrieve the app instance id of the application.
687+
*
688+
* #### Example
689+
*
690+
* ```js
691+
* const appInstanceId = await firebase.analytics().getAppInstanceId();
692+
* ```
693+
*
694+
* @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.
695+
*/
696+
getAppInstanceId(): Promise<string | null>;
697+
685698
/**
686699
* Gives a user a unique identification.
687700
*

packages/analytics/lib/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ class FirebaseAnalyticsModule extends FirebaseModule {
130130
return this.native.setSessionTimeoutDuration(milliseconds);
131131
}
132132

133+
getAppInstanceId() {
134+
return this.native.getAppInstanceId();
135+
}
136+
133137
setUserId(id) {
134138
if (!isNull(id) && !isString(id)) {
135139
throw new Error("firebase.analytics().setUserId(*) 'id' expected a string value.");

0 commit comments

Comments
 (0)