|
| 1 | +--- |
| 2 | +id: index-analytics |
| 3 | +title: Building an Analytics dashboard app using Redis |
| 4 | +sidebar_label: Building an Analytics dashboard app using Redis |
| 5 | +slug: /howtos/analytics |
| 6 | +--- |
| 7 | + |
| 8 | +This tutorial shows a basic analytics dashboard app that uses Redis Bitmap written in NodeJS (JavaScript) |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +### Step 1. Prepare the environment |
| 13 | + |
| 14 | +- Install Node - v12.19.0 |
| 15 | +- Install NPM - v6.14.8 |
| 16 | +- Install Docker - v19.03.13 (optional) |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +### Step 2. Clone the repository |
| 21 | + |
| 22 | + ```bash |
| 23 | + git clone https://github.com/redis-developer/basic-analytics-dashboard-redis-bitmaps-nodejs |
| 24 | + ``` |
| 25 | + |
| 26 | + |
| 27 | +### Step 3. Setting up backend |
| 28 | + |
| 29 | + |
| 30 | +First we will be settin up environmental variables |
| 31 | + |
| 32 | +Go to /server folder (cd ./server) and then execute the below command: |
| 33 | + |
| 34 | + ```bash |
| 35 | + cp .env.example .env |
| 36 | + ``` |
| 37 | + |
| 38 | + |
| 39 | +### Step 4. Install dependencies |
| 40 | + |
| 41 | + ```bash |
| 42 | + npm install |
| 43 | + ``` |
| 44 | + |
| 45 | +### Step 5. Run Docker Compose to install Redis |
| 46 | + |
| 47 | + ```bash |
| 48 | + docker network create global |
| 49 | + docker-compose up -d --build |
| 50 | + ``` |
| 51 | + |
| 52 | +### Step 6. Run the backend |
| 53 | + |
| 54 | + ```bash |
| 55 | + npm run dev |
| 56 | + ``` |
| 57 | + |
| 58 | +### Step 7. Setting up the frontend |
| 59 | + |
| 60 | +Go to /client folder (cd ./client) and then: |
| 61 | + |
| 62 | + ```bash |
| 63 | + cp .env.example .env |
| 64 | + ``` |
| 65 | + |
| 66 | +### Step 8. Install dependencies |
| 67 | + |
| 68 | + ```bash |
| 69 | + npm install |
| 70 | + ``` |
| 71 | + |
| 72 | +### Step 9. Run the frontend |
| 73 | + |
| 74 | + ```bash |
| 75 | + npm run serve |
| 76 | + ``` |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +### How does it work? |
| 81 | + |
| 82 | +#### How the data is stored: |
| 83 | + |
| 84 | +The event data is stored in various keys and various data types. |
| 85 | + |
| 86 | +For each of time spans: |
| 87 | + |
| 88 | +- year: like 2021 |
| 89 | +- month: like 2021-03 (means March of 2021) |
| 90 | +- day: like 2021-03-03 (means 3rd March of 2021) |
| 91 | +- weekOfMonth: like 2021-03/4 (means 4th week of March 2021) |
| 92 | +- anytime |
| 93 | + |
| 94 | +and for each of scopes: |
| 95 | + |
| 96 | +- source |
| 97 | +- action |
| 98 | +- source + action |
| 99 | +- action + page |
| 100 | +- userId + action |
| 101 | +- global |
| 102 | + |
| 103 | +and for each of data types (types): |
| 104 | + |
| 105 | +- count (Integer stored as String) |
| 106 | +- bitmap |
| 107 | +- set |
| 108 | + |
| 109 | +Is generated key like: |
| 110 | + |
| 111 | + |
| 112 | + ```bash |
| 113 | + rab:{type}[:custom:{customName}][:user:{userId}][:source:{source}][:action:{action}][:page:{page}]:timeSpan:{timeSpan} |
| 114 | + ``` |
| 115 | + |
| 116 | +where values in [] are optional. |
| 117 | + |
| 118 | +- For each generated key like rab:count:*, data is stored like: INCR {key} |
| 119 | +Example: |
| 120 | + |
| 121 | + ```bash |
| 122 | + INCR rab:count:action:addToCart:timeSpan:2015-12/3 |
| 123 | + ``` |
| 124 | +- For each generated key like: rab:set:*, data is stored like: SADD {key} {userId} |
| 125 | +Example: |
| 126 | + |
| 127 | + ```bash |
| 128 | + SADD rab:set:action:addToCart:timeSpan:2015-12/3 8 |
| 129 | + ``` |
| 130 | +- For each generated key like rab:bitmap:*, data is stored like: SETBIT {key} {userId} 1. |
| 131 | +Example: |
| 132 | + |
| 133 | + ```bash |
| 134 | + SETBIT rab:bitmap:action:addToCart:timeSpan:2015-12/3 8 1 |
| 135 | + ``` |
| 136 | +### Cohort data |
| 137 | + |
| 138 | +- We store users who register and then bought some products (action order matters). |
| 139 | +- For each buy action in December we check if user performed register action before (register counter must be greater than zero). |
| 140 | +- If so, we set user bit to 1 like: SETBIT rab:bitmap:custom:cohort-buy:timeSpan:{timeSpan} {userId} 1 |
| 141 | + |
| 142 | +- E.g User Id 2 bought 2 products on 2015-12-17. It won't be stored. |
| 143 | +- E.g User Id 10 bought 1 product on 2015-12-17 and registered on 2015-12-16. |
| 144 | + It will be stored like: SETBIT rab:bitmap:custom:cohort-buy:timeSpan:2015-12 10 1. |
| 145 | +- We assume that user cannot buy without register. |
| 146 | + |
| 147 | +#### Retention data |
| 148 | + |
| 149 | +- Retention means users who bought on two different dates |
| 150 | +- For each buy action we check if user bought more products anytime than bought on particular day (current purchase not included). |
| 151 | +- If so, we add user id to set like: SADD rab:set:custom:retention-buy:timeSpan:{timeSpan} {userId} |
| 152 | +- E.g 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). |
| 153 | +- E.g 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 3. |
| 154 | + |
| 155 | +#### How the data is accessed: |
| 156 | + |
| 157 | +- Total Traffic: |
| 158 | + |
| 159 | +December: ```BITCOUNT rab:bitmap:custom:global:timeSpan:2015-12``` |
| 160 | +X week of December: ```BITCOUNT rab:bitmap:custom:global:timeSpan:2015-12/{X}``` |
| 161 | +Example: |
| 162 | + |
| 163 | + ```bash |
| 164 | + BITCOUNT rab:bitmap:custom:global:timeSpan:2015-12/3 |
| 165 | + ``` |
| 166 | + |
| 167 | +- Traffic per Page ({page} is one of: homepage, product1, product2, product3): |
| 168 | + |
| 169 | +December: BITCOUNT rab:bitmap:action:visit:page:{page}:timeSpan:2015-12 |
| 170 | +Example: |
| 171 | + |
| 172 | + ```bash |
| 173 | + BITCOUNT rab:bitmap:action:visit:page:homepage:timeSpan:2015-12 |
| 174 | + ``` |
| 175 | + |
| 176 | +- X week of December: BITCOUNT rab:bitmap:action:visit:page:{page}:timeSpan:2015-12/{X} |
| 177 | +Example: |
| 178 | + |
| 179 | + ```bash |
| 180 | + BITCOUNT rab:bitmap:action:visit:page:product1:timeSpan:2015-12/2 |
| 181 | + ``` |
| 182 | + |
| 183 | +- Traffic per Source ({source} is one of: google, Facebook, email, direct, referral, none): |
| 184 | + |
| 185 | +December: BITCOUNT rab:bitmap:source:{source}:timeSpan:2015-12 |
| 186 | +Example: |
| 187 | + |
| 188 | + ```bash |
| 189 | + BITCOUNT rab:bitmap:source:referral:timeSpan:2015-12 |
| 190 | + ``` |
| 191 | + |
| 192 | +- X week of December: BITCOUNT rab:bitmap:source:{source}:timeSpan:2015-12/{X} |
| 193 | +Example: |
| 194 | + |
| 195 | + ```bash |
| 196 | + BITCOUNT rab:bitmap:source:google:timeSpan:2015-12/1 |
| 197 | + ``` |
| 198 | + |
| 199 | +- Trend traffic ({page} is one of: homepage, product1, product2, product3): |
| 200 | + |
| 201 | +- December: from BITCOUNT rab:bitmap:action:visit:{page}:timeSpan:2015-12-01 to BITCOUNT rab:bitmap:action:visit:{page}:timeSpan:2015-12-31 |
| 202 | +- 1 Week of December: Similar as above, but from 2015-12-01 to 2015-12-07 |
| 203 | +- 2 Week of December: Similar as above, but from 2015-12-08 to 2015-12-14 |
| 204 | +- 3 Week of December: Similar as above, but from 2015-12-15 to 2015-12-21 |
| 205 | +- 4 Week of December: Similar as above, but from 2015-12-22 to 2015-12-28 |
| 206 | +- 5 Week of December: Similar as above, but from 2015-12-29 to 2015-12-31 |
| 207 | +- Example: |
| 208 | + |
| 209 | + ```bash |
| 210 | + 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 |
| 211 | + ``` |
| 212 | + |
| 213 | +- Total products bought: |
| 214 | + |
| 215 | +- December: GET rab:count:action:buy:timeSpan:2015-12 |
| 216 | +- X week of December: GET rab:count:action:buy:timeSpan:2015-12/{X} |
| 217 | +Example: |
| 218 | + |
| 219 | + ```bash |
| 220 | + GET rab:count:action:buy:timeSpan:2015-12/1 |
| 221 | + ``` |
| 222 | + |
| 223 | +- Total products added to cart: |
| 224 | + |
| 225 | +December: GET rab:count:action:addToCart:timeSpan:2015-12 |
| 226 | +X week of December: GET rab:count:action:addToCart:timeSpan:2015-12/{X} |
| 227 | +Example: |
| 228 | + |
| 229 | + ```bash |
| 230 | + GET rab:count:action:addToCart:timeSpan:2015-12/1 |
| 231 | + ``` |
| 232 | + |
| 233 | +- Shares of products bought ({productPage} is on of product1, product2, product3): |
| 234 | + |
| 235 | +December: GET rab:count:action:buy:page:{productPage}:timeSpan:2015-12 |
| 236 | +Example: |
| 237 | + |
| 238 | + ```bash |
| 239 | + GET rab:count:action:buy:page:product3:timeSpan:2015-12 |
| 240 | + ``` |
| 241 | + |
| 242 | +- X week of December: GET rab:count:action:buy:page:{productPage}:timeSpan:2015-12/{X} |
| 243 | +Example: |
| 244 | + |
| 245 | + ```bash |
| 246 | + GET rab:count:action:buy:page:product1:timeSpan:2015-12/2 |
| 247 | + ``` |
| 248 | + |
| 249 | +#### Customer and Cohort Analysis |
| 250 | + |
| 251 | +- People who registered: BITCOUNT rab:bitmap:action:register:timeSpan:2015-12 |
| 252 | +- People who register then bought (order matters): BITCOUNT rab:bitmap:custom:cohort-buy:timeSpan:2015-12 |
| 253 | +- Dropoff: (People who register then bought / People who register) * 100 [%] |
| 254 | +- Customers who bought only specified product ({productPage} is one of: product1, product2, product3): SMEMBERS rab:set:action:buy:page:{productPage}:timeSpan:2015-12 |
| 255 | + |
| 256 | +Example: |
| 257 | + |
| 258 | + ```bash |
| 259 | + SMEMBERS rab:set:action:buy:page:product2:timeSpan:2015-12 |
| 260 | + ``` |
| 261 | + |
| 262 | +- Customers who bought Product1 and Product2: SINTER rab:set:action:buy:page:product1:timeSpan:anytime rab:set:action:buy:page:product2:timeSpan:anytime |
| 263 | + |
| 264 | +- Customer Retention (customers who bought on the different dates): SMEMBERS rab:set:custom:retention-buy:timeSpan:anytime |
| 265 | + |
| 266 | + |
0 commit comments