0.1.5 • Published 2 years ago

plausible-tracker-node v0.1.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

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:

OptionTypeDescriptionDefault
domainstringYour site's domain, as declared by you in Plausible's settingsurl.hostname
hashModeboolEnables tracking based on URL hash changes.false
trackLocalhostboolEnables tracking on localhostfalse
apiHoststringPlausible'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:

OptionTypeDescriptionDefault
urlstringCurrent page's URL.location.href
referrerstring or nullReferrer's addressdocument.referrer
deviceWidthnumberUser's device width for device tracking.window.innerWidth
clientUserAgentstringClient User Agent for unique user counting.
clientIpstringClient IP address for unique user counting.
propsobjectProperties 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.