Using Firebase Analytics Server-Side

Matt Lim
ITNEXT
Published in
4 min readJan 18, 2021

--

The Problem

In general, Firebase focuses on empowering clients (e.g. web clients and mobile clients) with useful libraries and APIs. Unfortunately, this means their support for server-side environments is not as robust. The typical way to use Firebase on a server is via the Admin SDK; however, it doesn’t have feature parity with the client-side APIs. The missing feature we’re concerned with in this post is the lack of server-side Firebase Analytics support. Note that the Firebase team is aware of this problem; check out this Github issue and this Github issue.

Let’s refresh our memories. Here’s how you log an event from a web client using the Firebase JavaScript SDK.

import firebase from "firebase";const firebaseConfig = { ... };
firebase.initializeApp(firebaseConfig);
firebase.analytics().logEvent("test_event", { some_param: 5 });

So, how do we do the same thing from a server?

The Solution, With Curl

There are only two requirements our implementation needs to meet:

  1. It needs to actually be able to log things… of course.
  2. It needs to be able to log things in debug mode, so the logged events can be viewed in the DebugView. Otherwise it would be too hard to debug things!

Without further ado, here’s you you do it:

There are two things you need in order to make this curl command work:

  1. You need to provide a value for the tid URL parameter. You should use the measurementId for your Firebase project. This link tells you how to get that.
  2. You need to provide a value for the cid URL parameter. If you’re just testing things out, you can find this by looking at the requests the Firebase JS SDK sends from the browser in the network tab and copying the cid from one of them. Note that the cid will be included as a cookie in requests made to your web server, so when actually logging from your server you should get cid from that cookie.

Some other important things to note:

  1. _dbg=1 makes the log show up in the DebugView. So, if you run this curl with the right tid and cid, you should be able to (almost) instantly verify whether it’s working.
  2. I’m not sure if all the headers are necessary, but I know at least some of them are (e.g. user-agent). I’ll leave it to you to figure this out if you’re curious.
  3. en is the event name.
  4. You can log event-specific params by using ep. E.g. if you include ep.param1=5&ep.param2=6 in the request, it will look like this in DebugView.

Finally, note that the request I’m sending mostly adheres to Google’s Measurement Protocol (Universal Analytics). Their reference isn’t complete though—for example, they don’t mention the ep query parameter. I figured the rest out by manually inspecting the requests sent by the Firebase JS SDK from the browser.

The Solution, With Code

Now that you have the curl command, you should be able to translate it into code in the server language of your choice. Below, I’ve provided a Node.js reference implementation to help you out with this.

What About the Measurement Protocol (Google Analytics 4)?

This protocol is supposed to provide the following functionality:

The Google Analytics Measurement Protocol for Google Analytics 4 allows developers to make HTTP requests to send events directly to Google Analytics servers. This allows developers to measure how users interact with their business from any HTTP-enabled environment. Notably, this makes it easy to measure interactions that happen server-to-server.

However, I ran into some issues when following their documentation, which is why I took the approach described above instead. Let’s take a look at their instructions:

Ok, doesn’t seem too bad. However, if you navigate to Admin > Data Streams for a Google analytics property that is linked to Firebase, you see this:

Guess I can’t create a new secret.

Let’s assume we somehow get past that problem and move to the next step:

Ok, so we need an app_instance_id. But there doesn’t appear to be any way to get an app_instance_id for web clients?

Lastly, I didn’t see any documentation for how to get events logged with this protocol to show up in the DebugView.

To be fair to Google, they do warn you that “This is an alpha API and subject to change.”

That’s it. Hope you found this useful!

--

--

Writer for

Software Engineer. Tweeting @pencilflip. Mediocre boulderer, amateur tennis player, terrible at Avalon. https://www.mattlim.me/