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 🙏

© 2024 – Pkg Stats / Ryan Hefner

plausible-tracker-node

v0.1.5

Published

A backend tracker to interact with Plausible Analytics for nodejs

Downloads

23

Readme

plausible-tracker-node

A backend tracker to interact with Plausible Analytics for nodejs

Features

  • Minimum features from the official script, but as an NPM module
  • Track goals and custom events
  • Provide manual values that will be bound to the event
  • Full typescript support

Installation

To install, simply run:

npm install plausible-tracker-node

yarn add plausible-tracker-node

Usage

To begin tracking events, you must initialize the tracker:

import {Tracker} from 'plausible-tracker-node';

const tracker = Tracker({
  domain: 'my-app.com',
});

new Tracker(options) accepts some options that you may want to provide:

| Option | Type | Description | Default | | -------------- | -------- | ----------------------------------------------------------------- | ------------------------ | | domain | string | Your site's domain, as declared by you in Plausible's settings | url.hostname | | hashMode | bool | Enables tracking based on URL hash changes. | false | | trackLocalhost | bool | Enables tracking on localhost | false | | apiHost | string | Plausible's API host to use. Change this if you are self-hosting. | 'https://plausible.io' |

The object returned from Plausible() contains the functions that you'll use to track your events. These functions are:

  • trackPageview(options?: TrackingOptions): Tracks a single page view.
  • trackEvent(eventName: string, options?: TrackingOptions): Tracks custom events and goals

For the complete documentation on these functions and their parameters, check out the reference documentation.

Tracking page views

To track a page view, use the trackPageview function provided

import {Tracker} from 'plausible-tracker-node';

const tracker = new Tracker();

// Track a page view
tracker.trackPageview();

You may also override the values you provided when initializing the tracker by passing a similar object as the first parameter.

This object takes the same options as the initialization one, plus the following:

| Option | Type | Description | Default | | --------------- | ------------------ | ------------------------------------------- | ------------------- | | url | string | Current page's URL. | location.href | | referrer | string or null | Referrer's address | document.referrer | | deviceWidth | number | User's device width for device tracking. | window.innerWidth | | clientUserAgent | string | Client User Agent for unique user counting. | | | clientIp | `string` | Client IP address for unique user counting. | | | props | object | Properties to be bound to the event. | undefined |

import {Tracker} from 'plausible-tracker-node';

const tracker = new Tracker({
  // Track localhost by default
  trackLocalhost: true,
});

// Override it on this call and also set a custom url
tracker.trackPageview({
  trackLocalhost: false,
  url: 'https://my-app.com/my-url',
});

The second parameter is an object with some options similar to the ones provided by the official Plausible script.

import {Tracker} from 'plausible-tracker-node';

const tracker = new Tracker();

// And wait for sending the event
await tracker.trackPageviewAndWait({});

Tracking custom events and goals

To track goals, all you need to do is call trackEvent and give it the name of the goal/event as the first parameter:

import {Tracker} from 'plausible-tracker-node';

const tracker = new Tracker();

// Tracks the 'signup' goal
tracker.trackEvent('signup');

Custom props can be provided with props parameter:

// Tracks the 'download' goal and provides a 'method' property.
tracker.trackEvent('download', {props: {method: 'HTTP'}});

As with trackPageview, you may also provide override values.

import Plausible from 'plausible-tracker'

const {trackEvent} = Plausible({
  trackLocalhost: false,
})

// Tracks the 'signup' goal with a callback, props and a different referrer.
trackEvent('signup', {
  trackLocalhost: true
  props: {
    variation: 'button A'
  }
});

Reference documentation

For the full method and type documentation, check out the reference documentation.