Building a product sometimes requires some insight into some very specific user metrics that aren't always handled well by 3rd party products. Let's talk about how to add custom metrics and caching to your custom backend. We will use the popular in-memory datastore Redis with Ruby on Rails. Alternatively, you can use Redis with any developer platform.
Using analytics platforms is great for understanding how users interact with your product. Analytics platforms are generally standalone products which require fairly complex integrations to seamlessly work with a custom backend. What if you wanted to know which users were generally active and actually using their account? You could re-target them with push notifications, text messages, or an email blast. Of course, all three of those re-marketing and outreach methods have their own APIs depending on the platform you chose which means, you will need to find and use an API to glue them all together.
However, the data of this discussion is somewhat disposable and does not need to be kept for long periods of time. It is also more than likely that you don't want to write to your database or datastore on every GET request, this isn't very scalable and becomes very costly at scale.
Lets consider the following use cases:
1. Caching the last time a user interacted with the app. (DateTime as a string)
2. Caching the number of actions that a user does to their account to have a high level litmus test for activity. (Integer)
3. Reading and Reporting on this information in a useful way.
Setup
For development: You can Install redis locally using Homebrew.
> brew install redis
For production: I recommend Heroku redis or another service provider to get started. Heroku provides a free tier for this service which enables you to try it out for free.
Install the gem into your rails app using the instructions provided.
> gem install redis
Or you can add to your Gemfile.
Create a connection to Redis that you can use throughout your app. I happen to be using Sidekiq which is a great job processing library for Ruby. I’m using the REDIS_URL construct to automatically hook up your instance of redis if using locally or on heroku. For other configurations, please see this page.
Create your Key schema
To start gathering this data, you will need to decide on a namespace to use to use for your keys. Redis is a key value store which means it has very shallow, non hierarchal keys.
In my user class, I added the following constants which will act as namespaces. An example key for the last time a user logged in for example would be user_last_update:445566. USER_ACTIVITY = 'user_activity:<user_id>' USER_LAST_UPDATE = 'user_last_update:<user_id>'
Redis Cache in Ruby
class RedisCache def initialize(redis) self.redis = redis end def self.set(key, value) return redis.set(key, value) end def self.incr(key) return redis.incr(key) end def self.get(key) return redis.get(key) end def self.keys(namespace) return redis.keys(namespace) end def self.del(key) return redis.del(key) end end
Caching User events
Using the RedisCache class above, you can cache valuable and actionable user information by simply setting the key for the data you would like to save. For example:
Given your user class, picking certain actions such as log in, sign up, search, etc you can cache values per call. @current_user = current user if using devise, or whichever user object that you have. Devise is a user authentication system for Rails.
RedisCache.set(“#{User::USER_LAST_UPDATE}#{@current_user.id}”, Time.now.utc)
To save the number of events that occurred you can simply call the incr method each time the user does an action that you would like to count. For example: Log in, update their profile, etc.
RedisCache.incr(“#{User::USER_ACTIVITY}#{@current_user.id}”)
To get the information for reporting and usage simply use the get method.
user_activity_count = RedisCache.get(“#{User::USER_ACTIVITY}#{@current_user.id}”) user_last_update = RedisCache.get(“#{User::USER_LAST_UPDATE}#{@current_user.id}”)