Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
How do I store user's activity in firestore?

I am building a social media app (very simple) an I want to store user's activity in firestore database. I have a collection of "users" and I keep user's id, user's username, user's profile pic there. But I dont think that user's activity should be stored there as well (correct me if I am wrong?)

So I created a new collection called UserActivity where I store user's activity. I wanted to store if a user has been searching on a profile so I do the following:

const logUserSearch = async (term) => {
    await firebase
      .firestore()
      .collection("userActivity")
      .doc(firebase.auth().currentUser.uid)
      .collection("userSearch")
      .add({
        term: term,
        date: firebase.firestore.FieldValue.serverTimestamp(),
      })
  };

I think the above query solves the problem with user's search term's. However I want to store if a user has visited a profile. So here is my question: what is the correct way to store if a user visited a profile? Should I add new subcollection "profileVisit", something like that:

const logProfileVisit = async (searchTerm, profileId) => {
    await firebase
      .firestore()
      .collection("userActivity")
      .doc(firebase.auth().currentUser.uid)
      .collection("profileVisit")
      .doc(profileId) 
      .add({
        source: searchTerm,
        date: firebase.firestore.FieldValue.serverTimestamp(),
      })
  }; 

But then how do I calculate which are the most "popular" profiles? Should I create my database in another way, like this:

const logProfileVisit = async (searchTerm, profileId) => {
        await firebase
          .firestore()
          .collection("userActivity")
          .doc(profileId)
          .collection("profileVisit")
          .add({
            user: firebase.auth().currentUser.uid
            source: searchTerm,
            date: firebase.firestore.FieldValue.serverTimestamp(),
          })
      };

So that I can easily calculate which are the most "popular" profiles? What about the user case where I need to calculate "top 10 fan profiles" or something similar? I.e. How do I calculate who visited your profile most often?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

A root level collection "userActivity" (or a sub-collection) should be enough. You can store the activity type as a field instead of sub-collections as shown below:

users -> {userId} -> userActivity -> {logId}
(col)     (doc)        (col)          (doc)

But then how do I calculate which are the most "popular" profiles?

You can store a number field in that profile's document and whenever the logProfileVisit is called, increment that:

const usersCol =  firebase.firestore().collection("users")

const logProfileVisit = async (searchTerm, profileId) => {
  await Promise.all([
    usersCol
      .doc(currentUserId)
      .collection("userActivity")
      .add({
        source: searchTerm,
        date: firebase.firestore.FieldValue.serverTimestamp(),
        type: "profileVisit"
      }),
    usersCol
      .doc(profileUserId)
      .update({
        profileViews: firebase.firestore.FieldValue.increment(1),
      })
  ])
};

You can also use batch writes while updating these fields so either both the operations fail or pass.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!