An interactive analytics dashboard serves several purposes. They allow you to share data and provide you with all those vital information to make game-changing decisions at a faster pace. Building a real-time dynamic dashboard using a traditional relational database might require a complex set of queries. By using a NoSQL database like Redis, you can build a powerful interactive and dynamic dashboard with a small number of Redis commands.
Let’s take a look at how this was achieved.
git clone https://github.com/redis-developer/basic-analytics-dashboard-redis-bitmaps-nodejs
First we will be setting up environment variables
Go to /server folder (cd ./server) and then execute the below command: cp .env.example .env
PORT=3000
# Host and a port. Can be with `redis://` or without.
# Host and a port encoded in redis uri take precedence over other environment variable.
# preferable
REDIS_ENDPOINT_URI=redis://redis-XXXX.c212.ap-south-1-1.ec2.cloud.redislabs.com:15564
# Or you can set it here (ie. for docker development)
REDIS_HOST=redis-XXXX.c212.ap-south-1-1.ec2.cloud.redislabs.com
REDIS_PORT=XXXX
# You can set password here
REDIS_PASSWORD=reXXX
COMPOSE_PROJECT_NAME=redis-analytics-bitmaps
npm install
npm run dev
Go to the client
folder (cd ./client
) and then:
cp .env.example .env
VUE_APP_API_URL=http://localhost:3000
npm install
npm run serve
The event data is stored in various keys and data types which is discussed below:
It generates keys with the following naming convention:
rab:{type}[:custom:{customName}][:user:{userId}][:source:{source}][:action:{action}][:page:{page}]:timeSpan:{timeSpan}
where values in [] are optional.
For each generated key like rab:count:*
, data is stored like: INCR {key}
INCR rab:count:action:addToCart:timeSpan:2015-12/3
rab:set:*
, data is stored like: SADD {key} {userId}
SADD rab:set:action:addToCart:timeSpan:2015-12/3 8
rab:bitmap:*
, data is stored like: SETBIT {key} {userId} 1
SETBIT rab:bitmap:action:addToCart:timeSpan:2015-12/3 8 1
SETBIT rab:bitmap:custom:cohort-buy:timeSpan:{timeSpan} {userId} 1
SETBIT rab:bitmap:custom:cohort-buy:timeSpan:2015-12 10 1
Retention means users who bought on two different dates
For each buy action we check if user bought more products anytime than bought on particular day (current purchase not included).
If so, we add user id to set like:
SADD rab:set:custom:retention-buy:timeSpan:{timeSpan} {userId}
Example - User Id 5 bought 3 products on 2015-12-15. His retention won't be stored (products bought on particular day: 2, products bought anytime: 0).
Example - User Id 3 bought 1 product on 2015-12-15 and before - 1 product on 2015-12-13. His retention will be stored (products bought on particular day: 0, products bought anytime: 1) like:
SADD rab:set:custom:retention-buy:timeSpan:2015-12
BITCOUNT rab:bitmap:custom:global:timeSpan:2015-12```
BITCOUNT rab:bitmap:custom:global:timeSpan:2015-12/{X}
BITCOUNT rab:bitmap:custom:global:timeSpan:2015-12/3
BITCOUNT rab:bitmap:action:visit:page:{page}:timeSpan:2015-12
BITCOUNT rab:bitmap:action:visit:page:homepage:timeSpan:2015-12
BITCOUNT rab:bitmap:action:visit:page:{page}:timeSpan:2015-12/{X}
BITCOUNT rab:bitmap:action:visit:page:product1:timeSpan:2015-12/2
BITCOUNT rab:bitmap:source:{source}:timeSpan:2015-12
BITCOUNT rab:bitmap:source:referral:timeSpan:2015-12
BITCOUNT rab:bitmap:source:{source}:timeSpan:2015-12/{X}
BITCOUNT rab:bitmap:source:google:timeSpan:2015-12/1
BITCOUNT rab:bitmap:action:visit:{page}:timeSpan:2015-12-01
BITCOUNT rab:bitmap:action:visit:{page}:timeSpan:2015-12-31
BITCOUNT rab:bitmap:action:visit:homepage:timeSpan:2015-12-29 => BITCOUNT rab:bitmap:action:visit:homepage:timeSpan:2015-12-30 => BITCOUNT rab:bitmap:action:visit:homepage:timeSpan:2015-12-31
GET rab:count:action:buy:timeSpan:2015-12
GET rab:count:action:buy:timeSpan:2015-12/{X}
GET rab:count:action:buy:timeSpan:2015-12/1