A tiny utility for working with anonymous ids
on the server and in the browser.
Getting Started
Installation
Install with npm:
npm install --save anonymous-id
Or with yarn:
yarn add anonymous-id
Examples
import anonymousId from 'anonymous-id'
anonymousId()
anonymousId('exampleId')
Usage
Get & Set
import { get, set } from 'anonymous-id'
set({ key: 'my_anon_id' })
get({ key: 'my_anon_id' })
Options
These are the base options that can be passed in to both get and set functions:
anonymousId({
debug: true,
preset: String,
cookie: String,
localStorage: Object
})
For setting anonymous ids, you can specify an id
field which will pass the options to setAnonymousId()
. If you don't set the id
option, then getAnonymousId()
will be called with the options:
const id = await promptEmailFromUser()
anonymousId({ id })
And for getting anonymous ids, you can pass control if the preset tries to persist data:
console.log(document.cookie)
const { cookie } = anonymousId({ persist: false })
console.log(document.cookie)
console.log(cookie)
Contributing
All contributions are super welcome! anonymous-id
is MIT-licensed.