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

v1.3.4

Published

Node client for µAnalytics

Downloads

13

Readme

micro-analytics

A small promise-based node client library for the µAnalytics service.

Install

$ npm install micro-analytics

Test

To test the client, set the client host in the /test/config.json file :

{
    "HOST": "http://localhost:7070"
}

Then simply run the tests :

$ npm test

Use

Create a client

To create a new client, you need to specify the µAnalytics host as a string.

var Analytics = require('micro-analytics');

var HOST = 'http://localhost:7070';
var analytics = new Analytics(HOST);

You can specify your credentials for µAnalytics basic authentication in an optional object passed as a second argument :

var Analytics = require('micro-analytics');

var HOST = 'http://localhost:7070';
var opts = {
  username: 'johan',
  password: 'myPass'
};

var analytics = new Analytics(HOST, opts);

By default, the client will use a cache key renewed each hour. You can set the cache interval using the cacheExpire key of the optional second argument. The value is the interval in seconds.

var Analytics = require('micro-analytics');

var HOST = 'http://localhost:7070';
var opts = {
  cacheExpire: 86400 // One day
};

var analytics = new Analytics(HOST, opts);

Get data from a database

All requests for data can be passed a parameters object to query over a time range :

var params = {
    start: new Date(2015, 0, 1),
    end: new Date(2015, 2, 1)
};

Get complete analytics

// Full query
analytics.list(DBNAME)
.then(function(result) {
    // result.list is an array containing the whole DB
    ...
});

// Example with optional time range
// The same applies for all data requests
analytics.list(DBNAME, params)
.then(function(result) { ... });

A full description for result can be found here.

Get the count of analytics

analytics.count(DBNAME)
.then(function(result) {
    // result looks like { total: 300, unique: 150 }
    ...
});

A full description for result can be found here.

Get aggregated analytics by countries

analytics.byCountries(DBNAME)
.then(function(countries) {
    // result.list is an array of aggregated analytics
    ...
});

A full description for countries can be found here.

Get aggregated analytics by platforms

analytics.byPlatforms(DBNAME)
.then(function(platforms) { ... });

A full description for platforms can be found here.

Get aggregated analytics by domains

analytics.byDomains(DBNAME)
.then(function(domains) { ... });

A full description for domains can be found here.

Get aggregated analytics by events

analytics.byEvents(DBNAME)
.then(function(events) { ... });

A full description for events can be found here.

Get aggregated analytics as a time serie

With overTime(), the parameter object can take an interval key to specify the time serie interval in seconds. By default, the service sets the interval to 86400 (which is equal to one day).

// Full query
analytics.overTime(DBNAME)
.then(function(timeSerie) { ... });

// With parameters
var params = {
    start: new Date(2015, 0, 1),
    end: new Date(2015, 2, 1),
    interval: 2592000 // one month
};

analytics.overTime(DBNAME, params)
.then(function(timeSerie) { ... });

A full description for timeSerie can be found here.

Insert data in a database

Simple insert

var data = {
    "time": new Date(), // optional
    "ip": "127.0.0.1",
    "event": "download",
    "path": "/somewhere",
    "headers": {
        "referer": "http://gitbook.com",
        "user-agent": "...",
        ...
    }
};

analytics.push(DBNAME, data)
.then(function() { ... });

Bulk insert

If you need to push a list of existing analytics, use this method:

var data = {
    "list": [
        {
            "time": 1450098642,
            "ip": "127.0.0.1",
            "event": "download",
            "path": "/somewhere",
            "platform": "Apple Mac",
            "refererDomain": "www.gitbook.com",
            "countryCode": "fr"
        },
        {
            "time": 0,
            "ip": "127.0.0.1",
            "event": "login",
            "path": "/someplace",
            "platform": "Linux",
            "refererDomain": "www.gitbook.com",
            "countryCode": "us"
        }
    ]
};

analytics.bulk(DBNAME, data)
.then(function() { ... });

The passed time value must be a Unix timestamp in sec. The countryCode will be reprocessed by the service based on the ip.

Multi-website bulk insert

If you need to push analytics for different websites, you can use:

var data = {
    "list": [
        {
            "website": "website-1.com",
            "time": 1450098642,
            "ip": "127.0.0.1",
            "event": "download",
            "path": "/somewhere",
            "platform": "Apple Mac",
            "refererDomain": "www.gitbook.com",
            "countryCode": "fr"
        },
        {
            "website": "website-2.com",
            "time": 0,
            "ip": "127.0.0.1",
            "event": "login",
            "path": "/someplace",
            "platform": "Linux",
            "refererDomain": "www.gitbook.com",
            "countryCode": "us"
        }
    ]
};

analytics.bulkMulti(DBNAME, data)
.then(function() { ... });

Delete a database

analytics.delete(DBNAME)
.then(function() { ... });

Auto Bulk Insert

THis module provides a small utility to easily bulk insert from multiple calls:

var bulk = new Analytics.BulkInsert(analytics, {
    // Flush after N elements
    flushAt: 100,

    // Max duration to wait (in ms)
    flushAfter: 10000
});

bulk.push('MYDB', data);
...
bulk.push('MYDB', data);