Options
All
  • Public
  • Public/Protected
  • All
Menu

plausible-tracker

Index

Type aliases

Cleanup

Cleanup: () => void

Cleans up all event listeners attached.

Type declaration

    • (): void
    • Returns void

EnableAutoOutboundTracking

EnableAutoOutboundTracking: (targetNode?: Node & ParentNode, observerInit?: MutationObserverInit) => Cleanup

Tracks all outbound link clicks automatically

Call this if you don't want to manually manage these links.

It works using a MutationObserver to automagically detect link nodes throughout your application and bind click events to them.

Optionally takes the same parameters as MutationObserver.observe.

Example

import Plausible from 'plausible-tracker'

const { enableAutoOutboundTracking } = Plausible()

// This tracks all the existing and future outbound links on your page.
enableAutoOutboundTracking()

The returned value is a callback that removes the added event listeners and disconnects the observer

import Plausible from 'plausible-tracker'

const { enableAutoOutboundTracking } = Plausible()

const cleanup = enableAutoOutboundTracking()

// Remove event listeners and disconnect the observer
cleanup()

Type declaration

    • (targetNode?: Node & ParentNode, observerInit?: MutationObserverInit): Cleanup
    • Parameters

      • Optional targetNode: Node & ParentNode
      • Optional observerInit: MutationObserverInit

      Returns Cleanup

EnableAutoPageviews

EnableAutoPageviews: () => Cleanup

Tracks the current page and all further pages automatically.

Call this if you don't want to manually manage pageview tracking.

Example

import Plausible from 'plausible-tracker'

const { enableAutoPageviews } = Plausible()

// This tracks the current page view and all future ones as well
enableAutoPageviews()

The returned value is a callback that removes the added event listeners and restores history.pushState

import Plausible from 'plausible-tracker'

const { enableAutoPageviews } = Plausible()

const cleanup = enableAutoPageviews()

// Remove event listeners and restore `history.pushState`
cleanup()

Type declaration

EventOptions

EventOptions: { callback?: undefined | (() => void); props?: undefined | {} }

Type declaration

  • Optional Readonly callback?: undefined | (() => void)

    Callback called when the event is successfully sent.

  • Optional Readonly props?: undefined | {}

    Properties to be bound to the event.

PlausibleEventData

PlausibleEventData: { deviceWidth?: Window["innerWidth"]; referrer?: Document["referrer"] | null; url?: Location["href"] }

Data passed to Plausible as events.

Type declaration

  • Optional Readonly deviceWidth?: Window["innerWidth"]

    The current device's width. Defaults to window.innerWidth

  • Optional Readonly referrer?: Document["referrer"] | null

    The referrer to bind the event to. Defaults to document.referrer

  • Optional Readonly url?: Location["href"]

    The URL to bind the event to. Defaults to location.href.

PlausibleInitOptions

PlausibleInitOptions: { apiHost?: undefined | string; domain?: Location["hostname"]; hashMode?: undefined | false | true; trackLocalhost?: undefined | false | true }

Options used when initializing the tracker.

Type declaration

  • Optional Readonly apiHost?: undefined | string

    The API host where the events will be sent. Defaults to 'https://plausible.io'

  • Optional Readonly domain?: Location["hostname"]

    The domain to bind the event to. Defaults to location.hostname

  • Optional Readonly hashMode?: undefined | false | true

    If true, pageviews will be tracked when the URL hash changes. Enable this if you are using a frontend that uses hash-based routing.

  • Optional Readonly trackLocalhost?: undefined | false | true

    Set to true if you want events to be tracked when running the site locally.

PlausibleOptions

Options used when tracking Plausible events.

TrackEvent

TrackEvent: (eventName: string, options?: EventOptions, eventData?: PlausibleOptions) => void

Tracks a custom event.

Use it to track your defined goals by providing the goal's name as eventName.

Example

import Plausible from 'plausible-tracker'

const { trackEvent } = Plausible()

// Tracks the 'signup' goal
trackEvent('signup')

// Tracks the 'Download' goal passing a 'method' property.
trackEvent('Download', { props: { method: 'HTTP' } })
param

Name of the event to track

param

Event options.

param

Optional event data to send. Defaults to the current page's data merged with the default options provided earlier.

Type declaration

TrackPageview

TrackPageview: (eventData?: PlausibleOptions, options?: EventOptions) => void

Manually tracks a page view.

Example

import Plausible from 'plausible-tracker'

const { trackPageview } = Plausible()

// Track a page view
trackPageview()
param

Optional event data to send. Defaults to the current page's data merged with the default options provided earlier.

param

Event options.

Type declaration

Functions

Plausible

  • Initializes the tracker with your default values.

    Example (es module)

    import Plausible from 'plausible-tracker'
    
    const { enableAutoPageviews, trackEvent } = Plausible({
      domain: 'my-app-domain.com',
      hashMode: true
    })
    
    enableAutoPageviews()
    
    function onUserRegister() {
      trackEvent('register')
    }
    

    Example (commonjs)

    var Plausible = require('plausible-tracker');
    
    var { enableAutoPageviews, trackEvent } = Plausible({
      domain: 'my-app-domain.com',
      hashMode: true
    })
    
    enableAutoPageviews()
    
    function onUserRegister() {
      trackEvent('register')
    }
    

    Parameters

    • Optional defaults: PlausibleInitOptions

      Default event parameters that will be applied to all requests.

    Returns { enableAutoOutboundTracking: EnableAutoOutboundTracking; enableAutoPageviews: EnableAutoPageviews; trackEvent: TrackEvent; trackPageview: TrackPageview }

Generated using TypeDoc