Skip to content

A Fathom Analytics library to help with client-side routing

License

Notifications You must be signed in to change notification settings

bobheadlabs/fathom-client

 
 

Repository files navigation

Fathom Client CircleCI

A Fathom Analytics library for environments with client-side routing.

Extracted from the StaticKit website.

Installation

Run the following to install in your project:

npm install fathom-client

Motivation

The standard installation flow for Fathom is to drop their snippet on your page, which will automatically load the library and track a pageview. This approach works great for server-rendered sites with full page refreshes, but gets tricky when:

  • Routing happens on the client-side (e.g. an SPA)
  • The DOM is abstracted away (e.g. Next.js)

This library provides an interface you can use to orchestrate Fathom calls at various points in your page lifecycle:

import * as Fathom from 'fathom-client';

// Upon initial page load...
Fathom.load();
Fathom.setSiteId('XXXXXXXX');
Fathom.trackPageview();

// In the route changed event handler...
const onRouteChangeComplete = () => {
  Fathom.trackPageview();
};

// In an event handler where a goal is achieved...
const onSignUp = () => {
  Fathom.trackGoal('Sign Up', 100);
};

Usage

Next.js

Create an _app.js file in your pages directory, like this:

import React, { useEffect } from 'react';
import Router from 'next/router';
import * as Fathom from 'fathom-client';

// Record a pageview when route changes
Router.events.on('routeChangeComplete', () => {
  Fathom.trackPageview();
});

function App({ Component, pageProps }) {
  // Initialize Fathom when the app loads
  useEffect(() => {
    if (process.env.NODE_ENV === 'production') {
      Fathom.load();
      Fathom.setSiteId('ZFEWBXJZ');
      Fathom.trackPageview();
    }
  }, []);

  return <Component {...pageProps} />;
}

export default App;

Releasing

Run the following to publish a new version:

npm run release

About

A Fathom Analytics library to help with client-side routing

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 62.9%
  • TypeScript 37.1%