Unique Visitor Counter

DOCUMENTATION · V0.4.0

Unique Visitor Counter documentation

Install and configure a small JavaScript package for showing a lifetime unique browser count on your website. Use the ready-made React component or call the SDK directly from JavaScript and other frameworks.

Quick start

Install with your preferred package manager:

npm install unique-visitor-counter
yarn add unique-visitor-counter
pnpm add unique-visitor-counter
bun add unique-visitor-counter

Create a site ID from that same project folder:

npx uvc init

Or use yarn uvc init, pnpm exec uvc init, or bunx uvc init. The command prints your site ID and saves it in .uvc.json. Use that same ID on every page of the same website. Keep it available to your app wherever you use the counter.

React

Import the component from the React entry point and place it where you want the number to appear:

import { VisitorCount } from 'unique-visitor-counter/react';

export function Footer() {
  return <VisitorCount siteId="your-site-id" />;
}

Replace your-site-id with the ID from npx uvc init. The component renders only the number after it loads.

Loading and saved counts

On a visitor’s first visit, the component shows Loading... while it fetches the count. It saves the last count in local storage. On later visits it shows that saved number immediately while it refreshes in the background. If a refresh fails, it keeps showing the saved number.

Customize the first-load and error messages with loadingText and errorText:

<VisitorCount
  siteId="your-site-id"
  loadingText="Getting the count..."
  errorText="Count is unavailable"
/>

JavaScript and other frameworks

Use createCounter() in browser JavaScript, Vue, Angular, or Svelte:

import { createCounter } from 'unique-visitor-counter';

const counter = createCounter({ siteId: 'your-site-id' });
const count = await counter.getCount();

document.querySelector('#visitor-count').textContent =
  String(count);

Call getCount() when your page or component loads. The returned value is a number. Add your own label, formatting, and loading state as needed.

How counting works

  1. The SDK creates a random visitor ID in the browser and saves it in local storage.
  2. It sends that ID and your site ID to the counter service.
  3. The service records that browser once for that site and returns the site’s total.

Calling the SDK again from the same browser and site refreshes the total without counting that browser again. A different browser, a different device, or cleared local storage gets a new ID and can count as a new visitor. This is a browser-based estimate of unique visitors, not a count of verified people.

The total is lifetime and is shared by everyone using the same site ID. Use a different site ID for a different website.

Privacy

The SDK does not use cookies, IP addresses, or browser fingerprinting to identify visitors. It creates a random ID and keeps it in local storage so repeat visits from the same browser are recognized.

  • The browser stores the random visitor ID and the last displayed count in local storage.
  • The service receives the site ID and random visitor ID, and stores a record for the unique site and visitor pair with a first-seen timestamp.
  • The package does not ask for a visitor’s name, email, or account.

Clearing local storage or switching browsers can make the same person appear as a new visitor. Requests still travel through the API host, whose infrastructure may keep ordinary operational logs under its own policies. If you need a different retention policy or hosting setup, review the service configuration before using the counter.

Command line

From the website project folder, print the current count for its saved site ID:

npx uvc count

Or query a specific site ID:

npx uvc your-site-id count

The CLI reads the count without registering a browser visitor.

Troubleshooting

The count does not appear

Check that your site ID is copied correctly and that the page can reach the counter API. React shows your configured errorText when an initial request fails; the direct JavaScript API rejects the promise, so handle errors in your app.

The count increased after I cleared browser data

The random browser ID lives in local storage. Clearing site data removes that ID, so the next visit is treated as a new browser.

Want to check a site’s count?

Run npx uvc count in the project that contains its .uvc.json file.