问题
Currently in our realtime database, we have a planDetails
node for each uid
that has fields like startDate
, endDate
, planType
.
I want to track the number of users whose premium plan is active.
I am trying to do this by making a firebase function that runs daily and updates a boolean custom user property, say isPremiumActive
, to either true or false.
I cannot just track the users who have purchased the premium plan because there are many who take the plan once and then don't renew it. Hence the word 'Active'
I thought this could be achieved through the setUserProperty function in firebase SDK but turns out we can only use it when the user is logged in.
I came to this conclusion because this analytics method does not take any sort of userID as a parameter and saw it mostly being used during an authenticated user session
Then I tried to set custom properties on Google analytics but I guess that too follows the same trend. Also, I am yet to find a solution to how I will update the analytics data through the firebase function.
So basically I am on a dead-end here and would be happy if someone could provide an alternate solution or throw light on something that I missed out
回答1:
It's not possible to update user properties from a backend. You have to use the frontend SDK API.
The only option you have for adding data from a backend to export Firebase Analtyics data to BigQuery, then merge into that any addition data you want to add per-user from your backend. Your queries will have to merge the data from the export with the data you added on the backend. None of this extra data will appear in the Firebase console - you will have to do everything through BigQuery.
回答2:
I was able to do this by making a fetch request to the analytics endpoint by making a function like this. I know this is not recommended but it gets the job done.
let hitGA = () => {
const GA_TRACKING_ID = "your GA tracking ID"
const GA_CLIENT_ID = "steps to get this will be given in the resources below"
const event = {
category: "pricing",
action: "counted",
label: "num_active_users",
value: 71
}
let request = new XMLHttpRequest();
let message =
`v=1&tid=${GA_TRACKING_ID}&cid=${GA_CLIENT_ID}&aip=1&ds=add-on&t=event&ec=${event.category}&ea=${event.action}&el=${event.label}&ev=${event.value}`
request.open("POST", "https://www.google-analytics.com/collect", true);
request.send(message);
}
P.S. if you are curious to know how I stumbled into the solutiona and other references, read this post
来源:https://stackoverflow.com/questions/63808048/add-custom-user-property-through-firebase-function