Support home / Script /

How to exclude certain pages from being tracked?

In some cases, you may not ever want to collect analytics data about certain pages on your website. A great example of this are preview pages, which most CMS' (like Ghost and WordPress) create. Sure, you could exclude yourself from being tracked, but below is a way to exclude pages instead of people.

Step 1: Disable automatic tracking in Fathom

The first step is to add data-auto="false" to your Fathom script tag. This stops Fathom from automatically tracking the page onload. Your script should look something like this:

<script data-auto="false" src="https://cdn.usefathom.com/script.js" data-site="YOUR-SITE-CODE"></script>

Step 2: Add in the logic

Here is where we check the pathname. We'll do a few examples here to show you what you can do.

Example 1: Blocking a single page

In this example, we want to block a page called /secret-page. Here's how we do it:

<script>
if (window.location.pathname !== '/secret-page') {
fathom.trackPageview();
}
</script>

Example 2: Blocking multiple pages

We want to block the following pages from being tracked on our website:

  • /thin-white-duke
  • /ashes-to-ashes
  • /joe-the-lion
  • /rebel-rebel

Instead of doing lots of IF statements, we're going to use an array:

<script>
var blockedPages = [
'/thin-white-duke',
'/ashes-to-ashes',
'/joe-the-lion',
'/rebel-rebel'
];
 
if (blockedPages.indexOf(window.location.pathname) < 0) {
window.fathom.trackPageview();
}
</script>

Example 3: Blocking directories or path patterns

We now want to block all pages that are in the following folders. In the list below I've used * to represent wildcards. So if you see /admin/*, it means I want to block every single page in the admin directory/folder from being tracked by Fathom.

  • /admin/*
  • /private/*

Now before we get to the code, let's be hyper clear about something. We are blocking the sub-pages and the directory itself. So in the following example, we're looking to block /admin/, /admin/page-1, /admin/page-2, etc. and then the same with /private/, /private/page-1 and /private/page-2.

In this example, we're using startsWith(), which isn't compatible with Internet Explorer. If you need to support Internet Explorer, you'll have to roll something different.

<script>
var blockedDirectories = [
'/admin/',
'/private/'
];
 
blockedDirectories.forEach(function(directory) {
if (window.location.pathname.startsWith(directory)) {
window.blockFathomPageview = true;
}
});
 
if (typeof window.blockFathomPageview == 'undefined') {
window.fathom.trackPageview();
}
</script>

By using startsWith() instead of indexOf(), we don't run into unexpected situations, like us accidentally blocking /blog/private when attempting to block /private.

One final note is that the above code won't block /admin (without the trailing slash). If you want to block /admin too, you'd need to add that to your blockedDirectories array.

Debugging

If you ever find that you're trying to block certain pages, but they're still being tracked, make sure you double check the URL of the page. If you're trying to block https://yourwebsite.com/another-page-to-block but your code has /another-page-to-block/, it won't work. Why? Because of the slashes.

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.