Skip to content

valgeirb/nuxt-fathom

Repository files navigation

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

Installation

Running the following command will:

  • Install the module as a dependency using your package manager.
  • Add it to your package.json file.
  • Update your nuxt.config file.
npx nuxi@latest module add fathom-analytics

Basic Usage

Provide your Fathom site ID in your nuxt.config file.

// `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