relekang / micro-analytics

Public analytics as a Node.js microservice, no sysadmin experience required. πŸ“ˆ

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

micro-analytics πŸ“ˆ

Public analytics as a Node.js microservice, no sysadmin experience required.

Build Status

A tiny analytics server with less than 100 lines of code, easy to run and hack around on. It does one thing, and it does it well: count the views of something and making the views publicly accessible via an API.

(there is currently no frontend to display pretty graphs, feel free to build one yourself!)

Setup

Running your own micro-analytics is just two tiny commands away:

npm install -g micro-analytics-cli

micro-analytics

That's it, the analytics server is now running at localhost:3000! πŸŽ‰

See server-setup.md for instructions on acquiring a server and setting up nginx to make your micro-analytics publicly available.

Note: You can pass any option to the micro-analytics command that you can pass to micro. As an example, to change the host you'd do micro-analytics -H 127.0.0.1

Database adapters

micro-analytics supports custom database adapters. They can be configured with the environment variable DB_ADAPTER. Setting it to redis will make it require micro-analytics-adapter-redis. Leaving it unset will make micro-analytics use the builtin flat-file-db adapter.

Usage

Tracking views

To track a view of x, simply send a request to /x. This is how you'd track page views for a website: (though note that this can be used to track anything you want)

<script>
  fetch('servicedomain.com' + window.location.pathname)
    // Log total pageviews for current page to console
    .then(response => response.json())
    .then(json => console.log(json.views))
    .catch(err => console.log('Something went wrong:', err))
</script>

If you send a GET request, the request will increment the views and return the total views for the id. (in this case "x") If you send a POST request, the views will increment but you don't get the total views back.

GET the views without incrementing

If you just want to get the views for an id and don't want to increment the views during a GET request, set inc to false in your query parameter. (/x?inc=false)

Getting all views

If you want to get all views for all ids, set the all query parameter to true on a root request. (i.e. /?all=true) If you pass the all parameter to an id, all ids starting with that pathname will be included. E.g. /x?all=true will match views for /x, /xyz but not /y.

Built with

  • micro to create the service.

    micro is a lightweight wrapper around Nodes http.Server which makes it easy to write ultra-high performance, asynchronous microservices. Perfect for our use case!

  • flat-file-db to store the data. (and promise to promisify flat-file-db)

    flat-file-db is a fast in-process flat file database that caches all data in memory and persists it to an open file using an append-only algorithm ensuring compact file sizes and strong consistency. By using the filesystem for storage setup is easy and backups are only a copy & paste away. (in case you need more advanced features of a real database, swapping out flat-file-db for a real db shouldn't take long)

If you want to change the path the database file is saved as pass it as an env variable called DB_PATH. E.g. DB_PATH=storage/analytics.db micro-analytics.

License

Copyright ©️ 2017 Maximilian Stoiber, licensed under the MIT License. See license.md for more information.

About

Public analytics as a Node.js microservice, no sysadmin experience required. πŸ“ˆ

License:MIT License


Languages

Language:JavaScript 100.0%