|
1 | | -# `mxstbr.blog` view counter |
| 1 | +# `micro-analytics` |
2 | 2 |
|
3 | | -A simple Node microservice to count the views of blogposts on `mxstbr.blog`. |
| 3 | +Analytics as a simple Node.js microservice. |
4 | 4 |
|
5 | | -## Uses |
| 5 | +With less than 50 lines of code this service is the smallest analytics you'll ever need. It does nothing except count the views of a page on your site via an API. |
| 6 | + |
| 7 | +(there is currently no frontend to consume the statistics, though writing one is on the to-do list) |
| 8 | + |
| 9 | +## Built with |
6 | 10 |
|
7 | 11 | - [`micro`](https://github.com/zeit/micro) to create the service. |
8 | | -- [`level`](https://github.com/level/level) to store the data. |
9 | | -- [`then-levelup`](https://github.com/then/then-levelup) to promisify `level`. |
| 12 | +- [`level`](https://github.com/level/level) to store the data. (and [`then-levelup`](https://github.com/then/then-levelup) to promisify `level`) |
| 13 | + |
| 14 | +## Usage |
| 15 | + |
| 16 | +### Starting the service |
| 17 | + |
| 18 | +1. `git clone git@github.com:mxstbr/micro-analytics` to get the repo. |
| 19 | +2. `npm install` to install the dependencies. |
| 20 | +3. `npm start` to start the service. |
| 21 | + |
| 22 | +And that's it! 🎉 |
10 | 23 |
|
11 | | -`views.mxstbr.blog` is hosted on a [DigitalOcean droplet](https://m.do.co/c/d371ed7f99af) (ref link) of the smallest size (5EUR/month), using `pm2` for running it on startup and nginx as a reverse proxy. |
| 24 | +### Tracking views |
| 25 | + |
| 26 | +To track a view simply send a request to `/<yourpagepath>`. If you send a `GET` request, the request will return the total views. If you send a `POST` request, the views will increment but you're not going to get the total views back. |
| 27 | + |
| 28 | +This is how you'd track pageviews for a website: (though note that this can be used to track anything you want) |
| 29 | + |
| 30 | +```HTML |
| 31 | +<script> |
| 32 | + fetch('servicedomain.com' + window.location.pathname) |
| 33 | + // Log total pageviews for current page to console |
| 34 | + .then(response => response.json()) |
| 35 | + .then(json => console.log(json.views)) |
| 36 | + .catch(err => console.log('Something went wrong:', err)) |
| 37 | +</script> |
| 38 | +``` |
12 | 39 |
|
13 | 40 | ## Gotchas |
14 | 41 |
|
15 | | -Currently this uses promisified `level` for data storage, which doesn't have atomic operations. |
| 42 | +Currently this uses promisified `level` for data storage, which doesn't have atomic operations. If you have a recommendation for a filesystem-based data storage module for Node that has atomic operations, please let us know! |
| 43 | + |
| 44 | +## Contributing |
| 45 | + |
| 46 | +If you run `npm run dev` the server will restart every time you edit the code. Perfect for development of `micro-analytics`! |
16 | 47 |
|
17 | 48 | ## License |
18 | 49 |
|
|
0 commit comments