Advanced embed code settings
These are the advanced options we offer with our embed script. Our standard script installation (pasting a single line of code into your site) works great for most customers.
Change when the 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" data-site="ABCDEFG" defer></script>
Honouring Do Not Track (DNT)
By default, we collect data on 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.” Plus, DNT is deprecated and no longer recommended by any browser.
That said, if you would like to honor DNT then update your code to include data-honor-dnt="true"
:
<script src="https://cdn.usefathom.com/script.js" data-site="ABCDEFG" data-honor-dnt="true" defer></script>
Disable automatic tracking
By default, we collect 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 data-auto="false"
:
<script src="https://cdn.usefathom.com/script.js" data-site="ABCDEFG" data-auto="false" defer></script>
Ignore canonicals
If there’s a canonical URL in place, 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 data-canonical="false"
:
<script src="https://cdn.usefathom.com/script.js" data-site="ABCDEFG" data-canonical="false" defer></script>
Exclude domains
To use included domains, see our site firewall settings. To exclude specific domains, but allow data to be collected from any other domain using Fathom, use data-excluded-domains="example.com"
and use a comma to separate multiple domains (no spaces in between the domains, just commas).
<script src="https://cdn.usefathom.com/script.js" data-site="ABCDEFG" data-excluded-domains="example1.com,localhost" defer></script>
Note: your www. has to match your site's usage of www.. For example, if your site loads as www.example.com and has data-included-domains=example.com, it will not collect data on any visits as they do not match.
Single-page applications
If there’s no Fathom integration 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" data-site="ABCDEFG" data-spa="auto" defer></script>
Note: this code will still collect data about 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" data-site="ABCDEFG" data-spa="history" defer></script>
Or:
<script src="https://cdn.usefathom.com/script.js" data-site="ABCDEFG" data-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
Some functions 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 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://yoursite.com/about', referrer: 'https://referrer.com/yoursite-link'});
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 an event
You can track an event that you have already set up in the Fathom dashboard.
// Event Code (string), The code you are given when you set-up the event// Cents (integer), below is $1.00fathom.trackGoal('eventCode', 100);
If you still have questions or require help with anything, please reach out to us and we'll happily get things sorted out for you.