“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

Leave a Reply Cancel reply

Exit mobile version
%%footer%%