nuxt-fathom
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

Nuxt Fathom module

Nuxt Fathom

npm version npm downloads License Nuxt

Nuxt module to integrate Fathom Analytics.

Features

  • ⚡ Automatic or manual tracking
  • 📯 Automatically imported composables
  • 🏷️ Fully typed Fathom API
  • 🦾 SSR-ready
  • 📂 .env file support

Setup

# pnpm
pnpm add -D nuxt-fathom

# npm
npm i -D nuxt-fathom

# yarn
yarn add -D nuxt-fathom

Basic Usage

Add nuxt-fathom to the modules section of your Nuxt configuration and provide your Fathom site ID.

// `nuxt.config.ts`
export default defineNuxtConfig({
  modules: ["nuxt-fathom"],

  fathom: {
    siteId: "FATHOM_SITE_ID",
  },
});

Tip: you can also use an .env file instead of a fathom key.

Done! Fathom Analytics will now run in your application's client.

Configuration

All supported module options can be configured using the fathom key in your Nuxt configuration:

export default defineNuxtConfig({
  modules: ['nuxt-fathom'],

  fathom: {
    // The Fathom Analytics site ID to use for tracking
    siteId: string,
    // Additional configuration
    config: {
      manual?: boolean
      auto?: boolean
      honorDNT?: boolean
      canonical?: boolean
      spa?: 'auto' | 'history' | 'hash'
    }
  }
})

Runtime Config

Instead of hard-coding your Fathom Analytics site ID in your Nuxt configuration, you can set your desired option in your project's .env file, leveraging automatically replaced public runtime config values by matching environment variables at runtime.

# Overwrites the `fathom.siteId` module option
NUXT_PUBLIC_FATHOM_SITE_ID=YOUR_SITE_ID

With this setup, you can omit the fathom key in your Nuxt configuration if you only intend to set the site ID.

Module Options

Property Type Description Default
manual boolean If you want to manually control page view tracking. false
auto boolean When false, skips automatically tracking page views on script load. true
honorDNT boolean When true, honors the DNT header in the visitor's browser. false
canonical boolean When false, ignores the canonical tag if present. true
spa 'auto', 'history', 'hash' Accepts one of the following values: auto, history, or hash (see advanced docs). undefined

Composables

As with other composables in the Nuxt 3 ecosystem, they are auto-imported and can be used in your application's components.

useFathom

The SSR-safe useFathom composable provides access to:

  • The blockTrackingForMe method.
  • The enableTrackingForMe method.
  • The isTrackingEnabled method.
  • The setSite method.
  • The trackEvent method.
  • The trackPageview method.

It can be used as follows:

// Each method is destructurable from the composable
const {
  blockTrackingForMe,
  enableTrackingForMe,
  isTrackingEnabled,
  setSite,
  trackEvent, // The method most likely to be used.
  trackPageview,
} = useFathom();

[!NOTE] Since the fathom instance is available in the client only, any useFathom method calls executed on the server will have no effect.

Development

# Install dependencies
pnpm install

# Generate type stubs
pnpm run dev:prepare

# Develop with the playground
pnpm run dev

# Build the playground
pnpm run dev:build

# Run ESLint
pnpm run lint

# Run Vitest
pnpm run test
pnpm run test:watch

# Release new version
pnpm run release

Credits

Readme

Keywords

none

Package Sidebar

Install

npm i nuxt-fathom

Weekly Downloads

121

Version

0.0.2

License

MIT

Unpacked Size

12 kB

Total Files

14

Last publish

Collaborators

  • valgeirb