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

true-micro-analytics

v1.0.0

Published

Simple analytics server using micro.

Downloads

4

Readme

true-micro-analytics

Simple analytics server using micro. Track views and more by performing a plain get request. Heavily inspired by micro-analytics from Max Stoiber.

Get started

Simply install true-micro-analytics globally and start it using micro-analytics command.

npm install -g true-micro-analytics
true-micro-analytics

Your server is now listening for requests. To keep the server running without having the shell open all the time you can use pm2 or forever

All cli arguments are passed through to micro. That said you can set a different host or port.

Make it available

In production you should set up a Nginx Reverse Proxy (or something similar) to make the Node server public. Digital Ocean has a splendid tutorial on how to set up a server with Node.js, Nginx, and pm2.

How to Track Data

To track a page view, command execution or anything else just make a get request to the corresponding path.

fetch(http://example.com/mypage)
  .catch((err) => {
    console.error('Could not track view.', err);
    });

The request will return a 204 HTTP status code without any content.

Get page count

To get the page view count of any endpoint call the same URL as above followed by ?count.

http://example.com/mypage?count

This will return a JSON object containing the accumulated views.

{"views": 236}

Get page views

To get the page view data for a specific path call the same URL again with ?views at the end.

http://example.com/page?views

The response is a JSON object containing an object for every view. For now the timestamp of the view is included. This can be expanded with more stats in the future.

{"views":[{"ts":1484323190294}]}

Get views for all paths

Calling the root URL will return data for all tracked paths.

http://example.com/

Motivation & goals

Max Stoiber came up with the awesome idea of some minimal analytics Node server. As I dived into the code I came across some things I would have done different. Therefore I wanted to make something very similar that suits my needs. The following were the goals I wanted to achieve.

True micro

I wanted to make a really small and consistent module. That means get along with a single file but keep it easily readable. The result are less than 100 LoC (with comments!) in a single file.

Note: Neither the original module nor my implementation are real microservices. Per definition microservices have to be stateless. The analytics servers are persisting data in a databases and are therefore stateful.

Consistent URL structure

I wanted to have a consisten and easy to remember URL structure. Firstly the server only accepts get requests. There is no need to allow more methods. Otherwise you might have to enable CORS features to enable cross-origin requests. To utilize the root path it returns all data. For path specific data there are query args (see above).

Return value of tracking URLs

The tracking URLs are returning HTTP status code 204. This tells us the server processed the request and doesn't return any data. To get the views we call the URL next off ?count. This makes it very clear which url is tracking the view by calling it and which is not.