Fathom Analytics Fathom Analytics

Advanced options for our tracking/embed code

You can see our basic installation instructions here (this works great for most users).

CMS / Frontend Frameworks

We have instructions for the following tools in the "Install Site" area.

Changing when our script is loaded

By default, our script is deferred, meaning our script loads once your HTML has finished loading. That means our script doesn’t affect your site load time or speed in any way (fast sites == better search engine rankings).

You can also swap out defer with async or just load it without either, but we don’t advise this. Instead, this is how our code should be most commonly used:

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" defer></script>

Honoring Do Not Track (DNT)

By default we track every visitor to your website, regardless of them having DNT turned on or not. That’s because Fathom is privacy-focused analytics, so nothing personal or identifiable is ever “tracked”.

That said, if you would like to honor DNT then update your code to include honor-dnt="true":

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" honor-dnt="true" defer></script>

Disable automatic tracking

By default, we track a page view every time a visitor to your website loads a page with our script on it. If you don’t want that functionality, you can turn it off by using auto="false":

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" auto="false" defer></script>

Ignore canonicals

If there’s a canonical URL in place, then by default we use it instead of the current URL. This is what most customers want, and it’s why it’s the default. If you want to use the current URL, even if there’s canonical (ignoring the canonical), then use canonical="false":

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" canonical="false" defer></script>

Excluded or included domains

By default, you can use our tracking code on any domain, it doesn’t matter if you use it on multiple sites or domains.

You can, however, exclude one or several domains, so our tracker will track things on every domain, except the ones excluded by adding excluded-domains="example.com" and use a comma to separate multiple domains (no spaces in between the domains, just commas). This is useful to exclude stats being tracked on your local development environment (if you have one).

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" excluded-domains="example1.com,localhost" defer></script>

If you want to go in the opposite direction and only track stats on a specific domain, you can use included-domains="example.com", using a comma to separate multiple domains (no spaces in between the domains, just commas). For example, if you have 2 development environments and a staging environment, you might want to track stats on your live site, example.com, but not on example.local and not on staging.example.com.

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" included-domains="example.com" defer></script>

Single page applications

If there’s not a Fathom plugin for your specific javascript system, you can use our generic SPA mode. Most applications use HTML5 History API, so the following code will typically work as-is. This code checks if the History API is available, and if it’s not, it falls back to listening to hash changes:

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" spa="auto" defer></script>

Note: With this code, it will still track the initial load. See “Disable automatic tracking” above if you don’t want this.

If you want to explicitly say “We are using only HTML5 History API” or “We only use hash-based routing”, you can use one of the following snippets:

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" spa="history" defer></script>

Or:

<script src="https://cdn.usefathom.com/script.js" site="ABCDEFG" spa="hash" defer></script>

It’s unlikely that you’ll be using hash-based routing but some people do (we do in our dashboard).

Client Side API functions

There are functions that can be called once the Fathom script has been loaded.

Track pageview

The typical use case for using this would be for someone handling routing manually themselves. Perhaps they have their own SPA / AJAX page loading, or they want more control over the data they send to Fathom.

fathom.trackPageview();

In addition to this, you can also go more complex and set your own parameters (when using this method, the canonical will not be used, as this becomes the source of truth):

fathom.trackPageview({
    url: 'https://pjrvs.com/about',
    referrer: 'https://referrer.com/paul-jarvis-co1'
});

You do not have to send both of these properties. If you send just the referrer, the URL will be established using the default method (canonical or current URL).

Track a Goal

You can track a goal that you have already set-up in the Fathom dashboard.

// Goal Code (string), The code you are given when you set-up the goal
// Cents (integer), below is $1.00
fathom.trackGoal('goalCode', 100);
← Return to support