How to add Analytics in a Bare React Native app (using React Native Firebase)
Many articles talk about adding analytics to React Native projects made with React Native CLI, but not as many explain how to do it in Expo's bare workflow.
When I started working with Expo, using it came with challenges like large native binary sizes and difficulty adding custom native code without (“ejecting”) leaving the Expo ecosystem.
But now, thanks to config plugins, we can make changes to native projects during prebuild, with the command npx expo prebuild.
Running npx expo prebuild sets up the Android and iOS folders for your React code. But be careful: if you change these folders manually, you might lose your changes next time you run this command.
That's why it's best to use config plugins when working in a bare workflow.
Now, if you have an expo bare workflow and you want to implement react native firebase analytics here’s what you have to do:
Add a new Firebase Project, I called mine TestProject
Make sure Enable Google Analytics for this project is selected and press Continue
Choose an existing account or create new one
Wait a few seconds and then press Continue
Add an iOS app
Apple bundle ID (Bundle Identifier) can be find Xcode, in the General tab under Identity, or in Signing & Capabilities tab
If your app is register on the app store, add the App Store ID, that can be found in your app's URL (https://itunes.apple.com/us/app/yourapp/id123456789) and looks sth like 123456789
Press Register and download GoogleService-Info.plist
Add the file at the app level root directory. After that, just press Next until you reach the end
Now we have to do the same for Android. So let’s go to project overview and add an Android app
Android Package Name can be found in android => app => src => main => java =>MainApplication or MainActivity, the first row in the file
If you want to add a nickname or an SHA-1 certificate, you can add them, otherwise leave those field blank
Press Register and download google-services.json file and add it in the same folder (root directory) as the file for iOS
Press Next until the end
Going back to our code editor install @react-native-firebase/app and @react-native-firebase/analytics
In the app.json and app.config.js file we will do the following: first, add the @react-native-firebase/analytics module to the plugins property
-
Then, under the ios and android property add the path to the files we have previously added, like in the examples below
In the end, run npx expo prebuild command to recreate the iOS and Android folder.
To test if the installation worked correctly, run the builds on both iOS and Android devices and the data should appear on the Firebase console.
Good luck and let me know in the comments if you're facing any technical difficulties 🚀