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 🙏

© 2026 – Pkg Stats / Ryan Hefner

persistent-device-id

v1.0.1

Published

A library for creating a persistent device ID

Readme

Persistent Device ID

persistent-device-id is a JavaScript library that creates an identifier that is attached to a user's browser session.

Please feel welcome to create issues or submit pull requests on the project.

Table of Contents

Quickstart

Get a copy of [persistent-device-id.min.js on Github releases][releases] and instantiate your PersistentDeviceId instance on the page:

<script src="persistent-device-id.min.js"></script>
<script>
  const persistentDeviceId = new PersistentDeviceId();
</script>

If you have a build system, you can instead install persistent-device-id with npm using npm i persistent-device-id@1 and require or import PersistentDeviceId for instantiating:

import PersistentDeviceId from 'persistent-device-id';

/* or */ const PersistentDeviceId = require('persistent-device-id');
var persistentDeviceId = new PersistentDeviceId();

This will set the persistentDeviceId cookie on your domain and then allow you to call:

  • persistentDeviceId.id().then(function(id) { ... }) to get the deviceId.

Read on for more details.


npm

If you have a JavaScript build system and would prefer to include PersistentDeviceId using it, you can install persistent-device-id from npm with:

npm install persistent-device-id@1

You can then import the bundle into your project. For example, using require:

const PersistentDeviceId = require('persistent-device-id');

Or using ES6 imports:

import PersistentDeviceId from 'persistent-device-id';

The bundles published to npm are in Universal Module Definition format.

Script Integrity

If you are including the bundle directly on your page, rather than in your build system, we recommended setting the integrity attribute on the script tag to the corresponding value from the integrity file of the release. For example, if the integrity file reads:

sha384-8de9e022e2f67e2072bb114e670d2fb37cab8eaf81616bcc3951087aa473e62a8b9fcc4c780a8d8d09df55c8b63bfd7c  persistent-device-id-1.0.0-rc1.js

then your HTML becomes:

<script src="persistent-device-id-1.0.0-rc1.js" integrity="sha384-8de9e022e2f67e2072bb114e670d2fb37cab8eaf81616bcc3951087aa473e62a8b9fcc4c780a8d8d09df55c8b63bfd7c">

If the integrity file is next to the script in question, you can validate the contents using:

sed s/^sha384-// integrity | shasum -c

Browser Compatibility

Compatible on recent versions of Chrome, Safari, Firefox, and mobile.

Examples

deviceId Prefix

PersistentDeviceId prefixes the deviceId with pid- by default. You can also use a custom prefix by specifying your desired prefix or omitting it entirely:

const persistentDeviceId = new PersistentDeviceId({ prefix: '' });

or

const persistentDeviceId = new PersistentDeviceId({ prefix: 'myid-' });

Reference

Constructor

During your page load you need to instantiate your PersistentDeviceId instance:

const id = new PersistentDeviceId({
  /**
   * @prop {string} [prefix=pid-] The prefix of the generated deviceId.
   */
  // prefix: 'pid-',
  /**
   * @prop {string} [cookie=persistentDeviceId] The cookie that the deviceId is
   * persisted in.
   */
  // cookie: 'my-guid',
  /**
   * @prop {number} [cookieExpiryDays] The number of days that a device ID will live.
   * Defaults to 365 in accordance with the GDPR's ePrivacy Directive.
   */
  // cookieExpiryDays: 365,
  /**
   * @prop {string} [cookieDomain] The top-most domain that we can store
   * cookies on. If you expect your customer to navigate between multiple
   * subdomains, e.g. catalog.store.com, checkout.store.com, then set
   * cookieDomain to store.com.
   */
  // cookieDomain: 'store.com',
  /**
   * @prop {string} [cookieSameSite] The SameSite attribute to control how
   * cookies are sent in cross-site requests. If not provided, "None;Secure"
   * will be used by default for HTTPS connections. This is to support clients
   * who process card payments in iframes or WebViews.
   */
  // cookieSameSite: 'None;Secure',
});

persistentDeviceId.id()

persistentDeviceId.id() returns a Promise which resolves to the device ID string. This will eventually match the persistentDeviceId cookie.

<form action="main">
  <input type="hidden" name="device-id" id="pid-device-id" />
</form>
<script src="persistent-device-id.min.js">
  <script>
      const persistentDeviceId = new PersistentDeviceId();
      persistentDeviceId.id().then(function(deviceId) {
          document.getElementById('pid-device-id').value = deviceId;
      });
</script>

If you are using a modern bundler and transpiler you can declare:

const deviceId = await persistentDeviceId.id();