In this episode, we pick up from our basic articles app and dive into tracking unique views for each article. The idea is to get a running count of how many unique visitors each article receives. We use Redis's HyperLogLog feature for this, which is great for counting unique items efficiently.
You'll see how to log a view for each article by creating a handy logView
method on the model, and we use the user's IP address as the 'unique' value. Don't worry if you're not familiar with Redis HyperLogLog, we walk through how to add and count unique views step-by-step.
We also test the setup by faking different IP addresses, just to prove that only unique views increase the count. Then, we display these counts next to each article back on the index page.
At the end, we point out that while we can now see unique view counts, we still need a way to order articles by most viewed—something we'll tackle in the next episode by syncing view counts from Redis to the database. Stay tuned!