“Next article” micro-analytics with Redis and Ruby

@elgrom has an interesting idea for predicting which article users are most likely to read next:

  • Every time a user navigates between articles, log it.
  • For each article, calculate the most popular “next” article

Here is how to do it in 12 lines of code with Redis and Ruby/Sinatra. (Next i’ll try it with Node for better scalability).

require 'rubygems'
require 'sinatra'
require 'redis'

# Log navigation from one location to the next
# @param [String] from one location
# @param [String] to the next location
# @return [Integer] How many times this from/to pair has been logged
get '/log' do
  redis = Redis.new
  redis.ZINCRBY(params[:from], 1, params[:to])
end

# Get the most popular "next" location
# @param [String] from location
# @return [String] the most popular "next" location
get '/get' do
  redis = Redis.new
  redis.zrevrange(params[:from], 0, 0)
end

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s