Skip to content

Refactors Firebase plugin to migrate to Modular API #1105

@Jack-Gill-TH

Description

@Jack-Gill-TH

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @segment/[email protected] for the project I'm working on.

I recently noticed that I was seeing various firebase deprecation warnings in Sentry breadcrumbs for our project. For example:

This method is deprecated (as well as all React Native Firebase namespaced API) and will be removed in the next major release as part of move to match Firebase Web modular SDK API. Please see migration guide for more details: https://rnfirebase.io/migrating-to-v22 Please use getApp() instead.

The instructions as described at that URL are fairly easy to follow, so the replacement wasn't difficult, though it's worth noting I've only tested this patch on my project.

I also think you might want to set higher versions of the '@react-native-firebase/analytics' and '@react-native-firebase/app' peer dependencies in package.json. Our project is using version 22.4.0 for both.

Here is the diff that solved my problem:

diff --git a/node_modules/@segment/analytics-react-native-plugin-firebase/src/FirebasePlugin.tsx b/node_modules/@segment/analytics-react-native-plugin-firebase/src/FirebasePlugin.tsx
index cb47226..7b16c3e 100644
--- a/node_modules/@segment/analytics-react-native-plugin-firebase/src/FirebasePlugin.tsx
+++ b/node_modules/@segment/analytics-react-native-plugin-firebase/src/FirebasePlugin.tsx
@@ -10,14 +10,29 @@ import {
 import screen from './methods/screen';
 import track from './methods/track';
 import reset from './methods/reset';
-import firebaseAnalytics from '@react-native-firebase/analytics';
+import { getAnalytics, setUserId, setUserProperties } from '@react-native-firebase/analytics';
 export class FirebasePlugin extends DestinationPlugin {
   type = PluginType.destination;
   key = 'Firebase';
 
   async identify(event: IdentifyEventType) {
+    let firebaseAnalyticsInstance;
+
+    try {
+      firebaseAnalyticsInstance = await getAnalytics();
+    } catch (error) {
+      this.analytics?.reportInternalError(
+        new SegmentError(
+          ErrorType.PluginError,
+          'Error on Firebase Identify',
+          error
+        )
+      );
+      return event;
+    }
+
     if (event.userId !== undefined) {
-      await firebaseAnalytics().setUserId(event.userId);
+      await setUserId(firebaseAnalyticsInstance, event.userId);
     }
     if (event.traits) {
       const eventTraits = event.traits;
@@ -45,7 +60,7 @@ export class FirebasePlugin extends DestinationPlugin {
         {}
       );
 
-      await firebaseAnalytics().setUserProperties(safeTraits);
+      await setUserProperties(firebaseAnalyticsInstance, safeTraits);
     }
     return event;
   }
diff --git a/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/reset.ts b/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/reset.ts
index 6f4d6a7..38e2e6e 100644
--- a/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/reset.ts
+++ b/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/reset.ts
@@ -1,5 +1,6 @@
-import firebaseAnalytics from '@react-native-firebase/analytics';
+import { getAnalytics, resetAnalyticsData } from '@react-native-firebase/analytics';
 
 export default async () => {
-  await firebaseAnalytics().resetAnalyticsData();
+  const firebaseAnalyticsInstance = await getAnalytics();
+  await resetAnalyticsData(firebaseAnalyticsInstance);
 };
diff --git a/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/screen.ts b/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/screen.ts
index a0e8c1e..6bbd1ae 100644
--- a/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/screen.ts
+++ b/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/screen.ts
@@ -1,4 +1,4 @@
-import firebaseAnalytics from '@react-native-firebase/analytics';
+import { getAnalytics, logEvent } from '@react-native-firebase/analytics';
 import type { ScreenEventType } from '@segment/analytics-react-native';
 
 export default async (event: ScreenEventType) => {
@@ -8,5 +8,6 @@ export default async (event: ScreenEventType) => {
     ...event.properties,
   };
 
-  await firebaseAnalytics().logScreenView(screenProps);
+  const firebaseAnalyticsInstance = await getAnalytics();
+  await logEvent(firebaseAnalyticsInstance, 'screen_view', screenProps);
 };
diff --git a/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/track.ts b/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/track.ts
index 3926228..9c8e112 100644
--- a/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/track.ts
+++ b/node_modules/@segment/analytics-react-native-plugin-firebase/src/methods/track.ts
@@ -1,4 +1,4 @@
-import firebaseAnalytics from '@react-native-firebase/analytics';
+import { getAnalytics, logEvent } from '@react-native-firebase/analytics';
 import {
   generateMapTransform,
   TrackEventType,
@@ -22,5 +22,6 @@ export default async (event: TrackEventType) => {
   if (safeEventName.length > 40) {
     safeEventName = safeEventName.substring(0, 40);
   }
-  await firebaseAnalytics().logEvent(safeEventName, safeProps);
+  const firebaseAnalyticsInstance = await getAnalytics();
+  await logEvent(firebaseAnalyticsInstance, safeEventName, safeProps);
 };

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions