Firebase total user count

13,445

Solution 1

There's no built-in method to do get the total user count.

You can keep an index of userIds and pull them down and count them. However, that would require downloading all of the data to get a count.

{
  "userIds": {
    "user_one": true,
    "user_two": true,
    "user_three": true 
  }
}

Then when downloading the data you can call snapshot.numChildren():

var ref = new Firebase('<my-firebase-app>/userIds');
ref.once('value', function(snap) {
  console.log(snap.numChildren());
});

If you don't want to download the data, you can maintain a total count using transactions.

var ref = new Firebase('<my-firebase-app>');
ref.createUser({ email: '', password: '', function() {
  var userCountRef = ref.child('userCount');
  userCountRef.transaction(function (current_value) {
    // increment the user count by one
    return (current_value || 0) + 1;
  });
});

Then you can listen for users in realtime:

var ref = new Firebase('<my-firebase-app>/userCount');
ref.on('value', function(snap) {
  console.log(snap.val());
});

Solution 2

Using Cloud Functions:

exports.updateUserCount = functions.auth.user().onCreate(user => {
  return admin.database().ref('userCount').transaction(userCount => (userCount || 0) + 1);
});

Just note that a Cloud Functions event is not triggered when a user is created using custom tokens. In that case, you would need to do something like this:

exports.updateUserCount = functions.database.ref('users/{userId}').onCreate(() => {
  return admin.database().ref('userCount').transaction(userCount => (userCount || 0) + 1);
});

Solution 3

Update 2021

I stumbled on this question and wanted to share three methods to get total number of signed-up users.

👀 Looking in the console

Go to the console, under Authentication tab, you can directly read the number of users under the list of users: enter image description here 56 users! yay!

📜 Using the admin SDK

For programmatic access to the number of users with potential filter on provider type, registration date, last connection date... you can write a script leveraging listUsers from the admin SDK.
For example, to count users registered since March 16:

const admin = require("firebase-admin");
const serviceAccount = require("./path/to/serviceAccountKey.json");
admin.initializeApp({ credential: admin.credential.cert(serviceAccount) });

async function countUsers(count, nextPageToken) {
    const listUsersResult = await admin.auth().listUsers(1000, nextPageToken);

    listUsersResult.users.map(user => {
        if (new Date(user.metadata.creationTime) > new Date("2021-03-16T00:00:00")) {
            count++;
        }
    });

    if (listUsersResult.pageToken) {
        count = await countUsers(count, listUsersResult.pageToken);
    }

    return count;
}

countUsers(0).then(count => console.log("total: ", count));

💾 Storing users in a DB

Your app maybe already stores user documents in Firestore, or the Realtime Database, or any other database. You can count these records to get the total number of registered users. (If you use Firestore, you can read my article on how to count documents)

Share:
13,445
Süha Boncukçu
Author by

Süha Boncukçu

Experienced in Winning with: Second place in Hackathon: Yeni Nesil Yazarkasa - #yazarkasahachathon http://www.eventbrite.com/e/hackathon-yeni-nesil-yazar-kasa-tickets-14696019189 Winner of Indra Future Minds Competition 2013 &amp; Top Rated Video - (http://www.indracompany.com/en/tu-carrera-en-indra/future-minds/edition-2013) Experienced in Management with: Yumag Studios [CTO] (Start-Up) (2013 - ) innOctopus Digital Production House [Technical Project Manager] (2014 - 2014) Experienced in Software Development with: innOctopus Digital Production House (http://innoctopus.com/) (2013 - 2014) puttikaa Digital Agency (http://puttikaa.com/) (2011 - 2013) Experienced in Organization with : Yıldız Technical University Information Technologies Days ( 2009 ) Improved in Knowledge and Human Network with : Yıldız Technical University - Computer Engineering (2008 - 2013 ) Universidad de Alicante - Ingeniería Informática ( 2012 - 2012 ) Amazing people I've met with. Interested in : System Design Natural Language Processing Web Data Mining Software Design "Passionate about technology. Looking for new and unique challenges every day. Highly motivated about international works and travelling. "

Updated on June 09, 2022

Comments

  • Süha Boncukçu
    Süha Boncukçu almost 2 years

    Is there a way to get all the users' count in firebase? (authenticated via password, facebook, twitter, etc.) Total of all social and email&password authenticated users.

  • Süha Boncukçu
    Süha Boncukçu over 8 years
    So you say I can't really rely on firebase's user auth system and I have to create something to save new userids all the time. Do I understand correctly?
  • David East
    David East over 8 years
    Firebase authentication works only for authenticating users. If you want to do something with that data you store it in your Firebase database.
  • Süha Boncukçu
    Süha Boncukçu over 8 years
    ok, fair enough. I would like to have access to at least all user ids or something like that though :) I'm accepting this as an answer
  • tom10271
    tom10271 almost 7 years
    For transaction, you can't ensure how user(hacker) manipulate the figure