Black Friday Week20% OFF on Nuxt UI Pro until Dec 2nd

fathom-analytics
nuxt-fathom

Fathom analytics integration for Nuxt

Nuxt Fathom module

Nuxt Fathom

npm versionnpm downloadsLicenseNuxt

Nuxt module to integrate Fathom Analytics.

Features

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

PropertyTypeDescriptionDefault
manualbooleanIf you want to manually control page view tracking.false
autobooleanWhen false, skips automatically tracking page views on script load.true
honorDNTbooleanWhen true, honors the DNT header in the visitor's browser.false
canonicalbooleanWhen 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