Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase total user count

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.

like image 871
Süha Boncukçu Avatar asked Dec 04 '15 15:12

Süha Boncukçu


1 Answers

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)

like image 162
Louis Coulet Avatar answered Sep 28 '22 02:09

Louis Coulet