You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
Tiana Lemesle b6f0c516ab 🔧 Add link to repository 2 years ago
.gitignore 🎉 Initial commit 3 years ago
README.md 📝 Add README.md 2 years ago
index.js ♻️ 2 years ago
package.json 🔧 Add link to repository 2 years ago
yarn.lock 🎉 Initial commit 3 years ago

README.md

anonymous-analytics

A privacy-friendly server-side analytics library.

Getting started

Install

From npm

  • yarn install anonymous-analytics

or

  • npm install anonymous analytics

Usage

Create a new analytics database (e.g. named test).

const
    Analytics = require('anonymous-analytics'),
    testAnalytics = new Analytics('test');

Populate the database with your endpoint's data.

  • Default fingerprint : IP address (+ eventual reverse proxy IP) + user agent.
  • Default extra payload : IP address country.

Express

const
    express = require('express'),
    app = express();
app.get('/test', async (request, response) => {
    await testAnalytics.addFromExpress(request);
    res.status(204).send();
});

Fastify

const
    fastify = require('fastify'),
    app = fastify();
app.get('/test', async (request, reply) => {
    await testAnalytics.addFromFastify(request);
    reply.code(204).send();
});

Custom

myTestHandler((myRequestContext, myResponseHandler) => {
    const {
        uniqueProperty1,
        uniqueProperty2,
        usefulPropertyA,
        usefulPropertyB
    } = myResponseHandler;
    testAnalytics.add(
        uniqueProperty1 + uniqueProperty2, // Fingerprint (string)
        { usefulPropertyA, usefulPropertyB } // Extra payload (object)
    );
    myResponseHandler(myResponseContext);
});

Read the data as stats.

await testAnalytics.getStats(
    fromDayTimestamp,
    toDayTimestamp,
    convertToYYYYMMDD
);
{
    "uniqueCount": 321,
    "totalCount": 654,
    "days": {
        "2020-01-01": {
            "uniqueCount": 123,
            "totalCount": 456
        }
    },
    "countries": {
        "France": {
            "uniqueCount": 12,
            "totalCount": 345
        }
    }
}

FAQ

How is user privacy protected ?

User fingerprints are hashed using SHA256 and timestamps are rounded to the day.

Changelog

  • 1.0.0 (2021-01-22) • Initial release