Skip to content

Commit 91fed75

Browse files
committed
add ability to get all views
1 parent 16d02da commit 91fed75

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@ Public analytics as a Node.js microservice.
44

55
With less than 100 lines of code this service is the smallest analytics you'll ever need. It does nothing except count the views of something and making the views accessible via an API.
66

7-
(there is currently no frontend to consume the statistics, though writing one is on the to-do list)
7+
(there is currently no frontend to display pretty graphs, feel free to build one!)
88

99
## Built with
1010

1111
- [`micro`](https://github.com/zeit/micro) to create the service.
1212
- [`flat-file-db`](https://github.com/mafintosh/flat-file-db) to store the data. (and [`promise`](https://github.com/then/promise) to promisify `flat-file-db`)
1313

14-
## Usage
15-
16-
### Starting the service
14+
## Setup
1715

1816
1. `git clone git@github.com:mxstbr/micro-analytics` to get the repo.
1917
2. `npm install` to install the dependencies.
2018
3. `npm start` to start the service.
2119

2220
And that's it! 🎉 (see [`deployment.md`](./deployment.md) for deployment instructions)
2321

24-
### Tracking views
22+
## Usage
2523

26-
To track a view simply send a request to `/<yourpath>`. If you send a `GET` request, the request will increment the views and 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.
24+
### Tracking views
2725

28-
If you don't want to increment the views during a `GET` request, set `inc` to `false` in your query parameter. (`/<yourpath>?inc=false`)
26+
To track a view, simply send a request to `/<yourpath>`. If you send a `GET` request, the request will increment the views and return the total views. If you send a `POST` request, the views will increment but you don't get the total views back.
2927

3028
This is how you'd track pageviews for a website: (though note that this can be used to track anything you want)
3129

@@ -39,6 +37,10 @@ This is how you'd track pageviews for a website: (though note that this can be u
3937
</script>
4038
```
4139

40+
If you just want to get the views for a path and don't want to increment the views during a `GET` request, set `inc` to `false` in your query parameter. (`/<yourpath>?inc=false`)
41+
42+
If you want to get all views for all paths, set the `all` query parameter to `true`. (`/?all=true`)
43+
4244
## Contributing
4345

4446
If you run `npm run dev` the server will restart every time you edit the code. Perfect for development of `micro-analytics`!

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,20 @@ const { send, createError, sendError } = require('micro')
44
const db = require('./db')
55

66
module.exports = async function (req, res) {
7-
// Check that a page is provided
87
const { pathname, query } = url.parse(req.url, /* parseQueryString */ true)
8+
// Send all views down if "?all" is true
9+
if (query.all === 'true' || query.all === true) {
10+
const data = {
11+
data: {},
12+
time: Date.now()
13+
}
14+
for (let key of db.keys()) {
15+
data.data[key] = db.get(key)
16+
}
17+
send(res, 200, data)
18+
return
19+
}
20+
// Check that a page is provided
921
if (pathname.length <= 1) {
1022
throw createError(400, 'Please include a path to a page.')
1123
}

0 commit comments

Comments
 (0)