Firebase analytics from remote REST API?
Is it possible to have Firebase analytics hook into a remote REST API endpoint?
More concretely, suppose I have a remote REST API, with an endpoint that computes the average number of videos each of my users has uploaded, /api/videos/get_average_count
. Can I somehow integrate that statistic into Firebase analytics? (I don't think there is a reporting API in Firebase analytics?)
Alternatively, is there anyway, I can take the Firebase data from the other analytics (tracked at the frontend) and integrate/embed that data into my own custom admin hosted on my own backend?
Solution 1:
There's no available REST API where you can upload, download or even analyze analytics data. You could reach out to the Firebase Support team and file a feature request for this. I believe that it's not only you who's looking for this kind of functionality in Google Analytics for Firebase.
You could directly download your event's analytics data as CSV using the Firebase console. Or, if you're on a blaze plan, link your Firebase to BigQuery, and the analytics data will be exported to a corresponding data set in a daily basis.
Solution 2:
This is now in the early stages of being possible with Measurement Protocol (Google Analytics 4) but it's literally in the process of being rolled out as we speak. I have implemented server-to-server events to my iOS firebase data stream. Example curl-like request.
Faraday.new(url: "https://www.google-analytics.com/mp/collect?firebase_app_id=#{your_app_id}&api_secret=#{secret_from_ga4_stream_details}").post do
req.headers["Content-Type"] = "application/json"
req.body = {app_instance_id: firebase_instance_id, user_id: user.id, events: [{}]}.to_json
end
This is a slightly different signature from a standard G4 property where the query params include measurement_id and client id. For firebase project you pass firebase_app_id which you get from your firebase project settings. firebase_instance_id is something you're probably not familiar with and something you will have integrate from device to the backend. For example, the instance id from ios would be Analytics.appInstanceID()
Solution 3:
The following curl command will log to Firebase analytics.
curl -v -X POST \
'https://www.google-analytics.com/g/collect?v=2&tid=YOUR-MEASUREMENT-ID&_dbg=1&cid=YOUR-CID&en=testing&ep.origin=firebase' \
-H "Content-Type: text/plain;charset=UTF-8" -H "sec-fetch-mode: no-cors" -H "sec-fetch-site: cross-site" -H "sec-fetch-dest: empty" \
-H "pragma: no-cache" -H "cache-control: no-cache" -H "origin: http://localhost:3000" \
-H "content-length: 0" -H "accept-language: en-US,en;q=0.9" -H "accept-encoding: gzip, deflate, br" -H "user-agent: dummy"
There are two things you need in order to make this curl command work:
- You need to provide a value for the
tid
URL parameter. You should use themeasurementId
for your Firebase project. This link tells you how to get that. - 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 thecid
will be included as a cookie in requests made to your web server, so when actually logging from your server you should getcid
from that cookie.
You should be able to translate that curl command into code in the server language of your choice. For more info about this approach, check out https://pencilflip.medium.com/using-firebase-analytics-server-side-64ffacafa6c3.