Rails current visitor count

1.1k views Asked by At

How does one implement a current visitors count for individual pages in Rails?

For example, a property website has a list of properties and a remark that says:- "there are 6 people currently looking at this property" for each individual listing.

I'm aware of the impressionist gem, which is able to log unique impressions for each controller. Just wondering if there is a better way than querying

impressions.where("created_at <= ?", 5.minutes.ago).count 

for each object in the array.

1

There are 1 answers

5
Richard Peck On BEST ANSWER

Before you get downvoted, I'll give you an idea of how to do it

Recording visitors is in the realm of analytics, of which Google Analytics is the most popular & recognized


Analytics

Analytics systems work with 3 parts:

  • Capture
  • Processing
  • Display

The process of capturing & processing data is fundamentally the same -- put a JS widget on your site to send a query to the server with attached user data. Processing the data puts it into your database


Displaying The Data

The difference for many people is the display of the data they capture

  • Google Analytics displays the data in their dashboard
  • Ebay displays the data as x people bought in the past hour
  • You want to show the number of people viewing an item

The way to do this is to hard-code the processing aspect of the data into your app

I can't explain the exact way to do this, because it's highly dependent on your stack, but this is the general way to do it