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

@plainapps/analytics-js

v1.0.2

Published

Plain Web Analytics vanilla JavaScript library - privacy-focused, cookie-free analytics with global API

Downloads

252

Readme

@plainapps/analytics-js

Vanilla JavaScript library for Plain Web Analytics - privacy-focused, cookie-free web analytics.

Features

  • 🔒 Privacy-first: Cookie-free tracking using IP hashing
  • 🚀 Lightweight: < 2KB gzipped
  • 🌐 Universal: Works with any website, framework, or CMS
  • 📦 Multiple formats: ESM, CommonJS, and UMD (for CDN)
  • 💪 TypeScript: Full type definitions included
  • 🔧 Auto-init: Automatically initializes from script data attributes
  • 🎯 Simple API: window.plainAnalytics global for easy access

Installation

CDN (Recommended for most sites)

Add this script tag before </body>:

<script defer 
    data-site="YOUR_SITE_KEY"
    src="https://cdn.jsdelivr.net/npm/@plainapps/analytics-js"></script>

That's it! Pageviews are automatically tracked.

npm/yarn/pnpm

npm install @plainapps/analytics-js
# or
yarn add @plainapps/analytics-js
# or
pnpm add @plainapps/analytics-js
import { init, event, goal } from '@plainapps/analytics-js';

init({ siteId: 'YOUR_SITE_KEY' });

// Track custom events
event('signup', { plan: 'pro' });

// Track goal conversions
goal('purchase', { revenue: 9900 }); // in cents

Quick Start

Basic Usage (CDN)

<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <h1>Welcome!</h1>
    <button onclick="plainAnalytics.event('button_click')">Click Me</button>
    
    <!-- Plain Web Analytics - add before </body> -->
    <script defer 
        data-site="pwa_site_abc123"
        src="https://cdn.jsdelivr.net/npm/@plainapps/analytics-js"></script>
</body>
</html>

Track Custom Events

// Track a button click
plainAnalytics.event('button_click', {
    button: 'signup',
    location: 'header'
});

// Track a form submission
plainAnalytics.event('form_submit', {
    form: 'contact',
    source: 'footer'
});

// Track a file download
plainAnalytics.event('download', {
    file: 'whitepaper.pdf',
    category: 'resources'
});

Track Goal Conversions

// Track a goal with revenue
plainAnalytics.goal('purchase', {
    revenue: 9900,  // in cents ($99.00)
    properties: {
        plan: 'pro',
        period: 'annual'
    }
});

// Track a goal without revenue
plainAnalytics.goal('free_trial');

Manual Pageviews (for SPAs)

// Track current page
plainAnalytics.pageview();

// Track a specific URL
plainAnalytics.pageview('/dashboard/settings');

Configuration Options

Data Attributes (CDN)

| Attribute | Description | Default | |-----------|-------------|---------| | data-site | Your site key (required) | - | | data-api | Custom API endpoint | https://api.plainwebanalytics.com/api/events | | data-auto-pageview | Auto-track pageview on load | true | | data-hash-mode | Track hash changes as pageviews | false | | data-debug | Enable debug logging | false |

Example with all options:

<script defer 
    data-site="pwa_site_abc123"
    data-api="https://your-api.com/events"
    data-auto-pageview="true"
    data-hash-mode="false"
    data-debug="false"
    src="https://cdn.jsdelivr.net/npm/@plainapps/analytics-js"></script>

Programmatic Configuration (npm)

import { init } from '@plainapps/analytics-js';

init({
    siteId: 'pwa_site_abc123',    // Required
    apiEndpoint: 'custom-url',    // Optional
    autoPageview: true,            // Optional (default: true)
    hashMode: false,               // Optional (default: false)
    debug: false                   // Optional (default: false)
});

API Reference

Global API (window.plainAnalytics)

When using the CDN, the following methods are available on window.plainAnalytics:

init(config)

Initialize analytics (usually not needed with CDN as it auto-initializes).

plainAnalytics.init({
    siteId: 'pwa_site_abc123',
    debug: true
});

pageview(url?)

Track a pageview. If no URL is provided, uses the current page URL.

plainAnalytics.pageview();
plainAnalytics.pageview('/custom/path');

event(name, properties?)

Track a custom event with optional properties.

plainAnalytics.event('click', { button: 'signup' });

goal(goalId, options?)

Track a goal conversion with optional revenue and properties.

plainAnalytics.goal('purchase', { 
    revenue: 9900,
    properties: { plan: 'pro' }
});

setEnabled(enabled)

Enable or disable tracking (e.g., for user opt-out).

// Disable tracking
plainAnalytics.setEnabled(false);

// Re-enable tracking
plainAnalytics.setEnabled(true);

isInitialized()

Check if analytics has been initialized.

if (plainAnalytics.isInitialized()) {
    console.log('Analytics is ready');
}

getConfig()

Get the current configuration.

const config = plainAnalytics.getConfig();
console.log(config.siteId);

Backwards Compatibility

For backwards compatibility with the simple tracking script, window.pwa is also available as an alias:

// These are equivalent
plainAnalytics.event('signup');
pwa.event('signup');

Integration Examples

WordPress

See examples/wordpress.html for detailed WordPress integration including:

  • Theme functions.php integration
  • Header/footer plugins
  • WooCommerce tracking
  • Contact form tracking

Static Site Generators

See examples/static-sites.html for integration with:

  • Hugo
  • Jekyll
  • Eleventy (11ty)
  • Gatsby
  • Astro
  • VitePress
  • Docusaurus
  • MkDocs

Single Page Applications (SPAs)

For SPAs, disable auto-pageview and track manually:

<script defer 
    data-site="pwa_site_abc123"
    data-auto-pageview="false"
    src="https://cdn.jsdelivr.net/npm/@plainapps/analytics-js"></script>

Then track pageviews on route changes:

// React Router example
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

function App() {
    const location = useLocation();
    
    useEffect(() => {
        plainAnalytics.pageview(location.pathname);
    }, [location]);
    
    return <Routes>...</Routes>;
}

User Opt-Out

Respect user privacy preferences:

// Check if user has opted out
const hasOptedOut = localStorage.getItem('analytics_optout') === 'true';
plainAnalytics.setEnabled(!hasOptedOut);

// Provide opt-out button
document.getElementById('optout-btn').addEventListener('click', () => {
    localStorage.setItem('analytics_optout', 'true');
    plainAnalytics.setEnabled(false);
    alert('Analytics disabled');
});

TypeScript

Full TypeScript support is included:

import type { AnalyticsConfig, EventProperties } from '@plainapps/analytics-js';
import { init, event, goal } from '@plainapps/analytics-js';

const config: AnalyticsConfig = {
    siteId: 'pwa_site_abc123',
    debug: process.env.NODE_ENV === 'development'
};

init(config);

const props: EventProperties = {
    button: 'signup',
    plan: 'pro'
};

event('click', props);

Multiple Instances

For tracking to multiple sites:

import { createAnalytics } from '@plainapps/analytics-js';

const site1 = createAnalytics();
site1.init({ siteId: 'pwa_site_site1' });

const site2 = createAnalytics();
site2.init({ siteId: 'pwa_site_site2' });

// Track to both
site1.event('pageview');
site2.event('pageview');

Framework-Specific Libraries

For deeper integration with popular frameworks, use our dedicated packages:

Browser Support

Works in all modern browsers:

  • Chrome (last 2 versions)
  • Firefox (last 2 versions)
  • Safari (last 2 versions)
  • Edge (last 2 versions)

Uses navigator.sendBeacon() when available, falls back to XHR.

Privacy

Plain Web Analytics is privacy-focused:

  • No cookies used
  • No personal data collected
  • GDPR/CCPA/PECR compliant
  • Visitor IDs are hashed (IP + UA + Site + Date)
  • No cross-site tracking

License

MIT

Links