npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

micro-analytics-cli

v3.1.0

Published

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

Downloads

13

Readme

micro-analytics 📈

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

A tiny analytics server, 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! 🎉

To deploy a server either refer to server-setup.md for instructions on manually acquiring and setting up a server or use now and deploy with a single command:

$ now micro-analytics/micro-analytics-cli

> Deployment complete! https://micro-analytics-asfdasdf.now.sh

Note: Since now deploys are stateless we have to store data in memory, which means after every new deploy or when the server sleeps your data will be lost. You'll need to use some remote database hosting service with one of the database adapters to persist data!

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 or POST request, the request will increment the views and return the total views for the id (in this case "x").

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)

POST to add metadata

You can add more metadata to the view by posting a JSON payload with the field meta. Everything in that meta field will be set on meta in the view object. You can read the data out with the all option, see below for more info. Example request that will post the browser useragent string:

<script>
  fetch('servicedomain.com' + window.location.pathname, {
    method: "POST",
    credentials: "include",
    headers: {"Content-Type": "application/json"},
    body: JSON.stringify({meta: { browser: navigator.userAgent }}),
  })
</script>

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.

Options

$ micro-analytics --help
Usage: micro-analytics [options] [command]

Commands:

  help  Display help

Options:

  -a, --adapter [value]  Database adapter used (defaults to "flat-file-db")
  -h, --help             Output usage information
  -H, --host [value]     Host to listen on (defaults to "0.0.0.0")
  -p, --port <n>         Port to listen on (defaults to 3000)
  -v, --version          Output the version number

Database adapters

By default, micro-analytics uses flat-file-db, a fast in-process flat file database, which makes for easy setup and backups.

This works fine for side-project usage, but for a production application with bajillions of visitors you might want to use a real database with a database adapter. Install the necessary npm package (e.g. micro-analytics-adapter-xyz) and then specify the DB_ADAPTER environment variable: $ DB_ADAPTER=xyz micro-analytics or use the --adapter cli option.

These are the available database adapters, made by the community:

Don't see your favorite database here? Writing your own adapter is pretty easy, we've even written the tests for you! See writing-adapters.md for a guide on how to write an adapter for your database of choice.

Live updates

micro-analytics let's you listen into updates live with server-sent events. That means you can e.g. build a realtime dashboard for your analytics!

Note: Make sure your database adapter supports this feature. If not, bug them to implement it! micro-analytics will tell you when it starts up if it is supported, so the easiest way to find out is to start it up.

The example below shows how you can listen for events in the browser, just swap my-deploy.now.sh with your own domain and give it a try:

const sse = new EventSource('https://my-deploy.now.sh/_realtime')
sse.onopen = function () { console.log('[sse] open') }
sse.onerror = function (error) { console.error('[sse error]', error) }
sse.addEventListener('micro-analytics-ping', function (e) { console.log('[sse]', e) })

Browser support

Server-sent events are not supported in all browsers. There are great, tiny polyfills available, but before you include one take a look at the caniuse table for server-sent events if you need one based on the browsers you support.

Polyfills that are supported:

Note: This list is from the documentation of the sse library we use rexxars/sse-channel, check that repo because it might have been updated.

Demo

We have a demo instance on demo.micro-analytics.io automatically deploys the master branch from this repository. Feel free to use it to test your clients.

License

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