Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(analytics): add method to retrieve the app instance id from the … #5210

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/analytics/usage/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Task<Void> setSessionTimeoutDuration(long milliseconds) {
});
}

Task<String> getAppInstanceId() {
return FirebaseAnalytics.getInstance(getContext()).getAppInstanceId();
}

Task<Void> setUserId(String id) {
return Tasks.call(() -> {
FirebaseAnalytics.getInstance(getContext()).setUserId(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down
6 changes: 6 additions & 0 deletions packages/analytics/e2e/analytics.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions packages/analytics/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,19 @@ export namespace FirebaseAnalyticsTypes {
*/
setSessionTimeoutDuration(milliseconds?: number): Promise<void>;

/**
* 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<string | null>;

/**
* Gives a user a unique identification.
*
Expand Down
4 changes: 4 additions & 0 deletions packages/analytics/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand Down