1

I am using react native firebase and I was looking to accomplish 2 things with the analytics package.

  1. For each event call that I add to my codebase to also automatically print out to the terminal while debugging, much in the same way a console.log() call would do.
  2. I want to attach a log level to each event (log, debug, warning, error).

Basically I want it to behave a bit more like a logger. Is this possible? How could I achieve something similar to this?

1 Answer 1

0

There are two main routes to do that.

First, you can set the debug flag for the Firebase SDK. That will cause it to start logging a lot of things into the console, events included. However, they will be not very readable that way. When the SDK is in the debug mode, it starts logging a lot more than just the events that are being sent since there's quite a lot of things that happen in it. For example, it will start logging state changes, and all the internal app events that it has capabilities to listen to.

Here, this goes into depth about it: Firebase iOS Debug Console not logging anything

I, however, prefer to do custom logging. I'm more on the side of the Analytics department, so I like to rely on custom logging to be able to more efficiently test native apps (React Native too) analytics and talk to the native devs whenever anything goes awry.

And custom logging is very easy to accomplish. By wrapping all calls to the analytics SDK, whether it's Firebase, Adobe Analytics, Mixpanel, Log Rocket you name it. Wrapping SDKs is a normal development practice since that way you're able to effectively extend the functionality of the SDK.

I prefer for the analytics department to take full ownership of the analytics wrappers and their maintenance. That allows analytics a degree of control not only by logging events in the local console, but also forcing certain standards, like lowercasing everything, replacing multiple \s with one space, sanitizing to get rid of certain characters that aren't expected in analytics, finally just checking the types and throwing errors or do some data debugging in prod if it's not what's expected. Plus certain QA automation integrations for Analytics. And, of course, simplification of the tracking functions signatures. This is also a great spot to populate event parameters with some globals like build version, environment, even user id, consent values or deeplink marketing campaign ids and so on.

I typically add Unicode hearts to the logs to make them more visible (and loveable) in the console, making them blue for regular events and green for pageviews, then adding a keyword that allows for quick filtering (I typically use "Firelytics" for firebase) then I normally log the screen name or the event name for events, and then finally the whole payload, but it's collapsed by default in the console. Obviously the implementations would be different for Android Studio and Xcode since their consoles work differently, but the idea is to make them as similar as possible. So the initial logs would look something like:

💚💚💚 Firelytics: "winter tires cross-sell splash screen" screenView sent!
💙💙💙 Firelytics: "navigation // view-level // decline" event sent!

This used to be the category // action // label in the GA UA times, but can be any of the similar dimensions in the new world of GA4.

I sometimes also add a short timestamp if it's not there automatically. Usually just minutes:seconds:milliseconds. Helps debugging in some cases.

And then the payload of the event with all the properties in a separate log. I found that different logging libraries treat logging objects differently, but usually you can make it log the object collapsed to avoid polluting the console too much.

Not the answer you're looking for? Browse other questions tagged or ask your own question.