Coder Social home page Coder Social logo

kieperb / anonymous-user-id Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lynnntropy/anonymous-user-id

0.0 0.0 0.0 1.72 MB

A JavaScript library that allows you to anonymously identify unique users on your website without requiring them to store (and consent to) a tracking cookie.

License: MIT License

TypeScript 100.00%

anonymous-user-id's Introduction

anonymous-user-id

CI Test Coverage Maintainability npm node-current GitHub

anonymous-user-id is a JavaScript library that allows you to anonymously identify unique users on your website without requiring them to store (and consent to) a tracking cookie. Instead, we generate a unique ID for each user based on information that can be pulled out of a regular HTTP request, mainly the source IP address and User-Agent header.

The method we use to do this is heavily inspired by Plausible Analytics, with BLAKE2s as the hash function.

Supported Algorithms

  • hash(salt + domain + ip + user_agent) - This is the same algorithm used by Plausible Analytics. It relies on a salt you need to rotate at least once a day, preventing you (or anyone else) from tracking the actions of a single user for longer than the lifespan of a single salt.

  • hash(hash(secret + date) + domain + ip + user_agent) - This is a modified (and less secure) variant of the original algorithm, meant for apps that can't reliably keep state (such as serverless functions), preventing them from storing a salt. Instead, we generate the salt from a long-lived secret (which you can set as e.g. an environment variable) and the current date.

Getting Started

Prerequisites

  • Node >= 10 (if using in Node)

Installing

npm:

npm i anonymous-user-id

yarn:

yarn add anonymous-user-id

Usage

For each algorithm this package supports, it exports a function you can use to generate an ID with it.

  • getAnonymousUserId(salt: string, request: RequestDetails)

    • implements hash(salt + domain + ip + user_agent)
  • getAnonymousUserIdWithSecret(secret: string, request: RequestDetails)

    • implements hash(hash(secret + date) + domain + ip + user_agent)

Example

import {
  getAnonymousUserId,
  getAnonymousUserIdWithSecret,
} from 'anonymous-user-id';

const requestDetails = {
  domain: 'test.test',
  ip: '1.1.1.1',
  userAgent: 'test/1.0',
};

const id1 = getAnonymousUserId('salt', requestDetails);
const id2 = getAnonymousUserIdWithSecret('secret', requestDetails);

Contributing

If you have Docker and Docker Compose installed, you can run docker-compose up to immediately get a working development environment for this package, with Jest running the tests in watch mode.

You can also use yarn link to use your local version of the package in a different project.

Authors

License

This project is licensed under the MIT License - see the LICENSE file for details.

anonymous-user-id's People

Contributors

lynnntropy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.