Skip to content

Commit b011497

Browse files
committed
rename to micro-analytics
1 parent 9413a5e commit b011497

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

README.md

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,49 @@
1-
# `mxstbr.blog` view counter
1+
# `micro-analytics`
22

3-
A simple Node microservice to count the views of blogposts on `mxstbr.blog`.
3+
Analytics as a simple Node.js microservice.
44

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
610

711
- [`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! 🎉
1023

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+
```
1239

1340
## Gotchas
1441

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`!
1647

1748
## License
1849

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const { send, createError, sendError } = require('micro')
66
const db = then(level('blog-views'))
77

88
module.exports = async function (req, res) {
9-
// Check that a blogpost is provided
9+
// Check that a page is provided
1010
const { pathname } = url.parse(req.url)
1111
if (pathname.length <= 1) {
12-
throw createError(400, 'Please include a path to a blogpost.')
12+
throw createError(400, 'Please include a path to a page.')
1313
}
1414
if (req.method !== 'GET') {
1515
throw createError(400, 'Please make a GET request.')
@@ -21,7 +21,7 @@ module.exports = async function (req, res) {
2121
send(res, 200, { views: views + 1 })
2222
} catch (err) {
2323
if (err.notFound) {
24-
// Initialise the post with one view
24+
// Initialise the page with one view
2525
await db.put(pathname, 1)
2626
send(res, 200, { views: 1 })
2727
} else {

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"name": "blog-view-counter",
3-
"version": "1.0.0",
4-
"description": "A simple node microservice to count the views of a blogpost.",
2+
"name": "micro-analytics",
3+
"version": "0.1.0",
4+
"description": "Analytics as a Node.js microservice.",
55
"main": "index.js",
66
"scripts": {
7-
"start": "micro -p 3000 -H localhost index.js",
7+
"start": "micro index.js",
88
"dev": "NODE_ENV=development nodemon --config package.json index.js"
99
},
1010
"author": "Max Stoiber <contact@mxstbr.com> (http://mxstbr.com/)",

0 commit comments

Comments
 (0)