Setup Fathom with Turbolinks

The partial app/views/application/_fathom.erb:

<script defer>
  (function(f, a, t, h, o, m){
  a[h]=a[h]||function(){
  (a[h].q=a[h].q||[]).push(arguments)
  };
  o=f.createElement('script'),
  m=f.getElementsByTagName('script')[0];
  o.async=1; o.src=t; o.id='fathom-script';
  m.parentNode.insertBefore(o,m)
  })(document, window, 'https://cdn.usefathom.com/tracker.js', 'fathom');
  fathom('set', 'siteId', '<%= ENV["FATHOM_ID"]');
  fathom('set', 'spa', 'pushstate');
</script>

fathom('set', 'spa', 'pushstate'); is from Fathom’s documentation.

with Fathom ID set in environment: ENV["FATHOM_ID"].

Applies in layout app/views/layouts/application.html.slim:

= render "application/fathom"
= javascript_include_tag "application", "data-turbolinks-track": "reload", "defer" => "defer"

In application.js:

+ //= require fathom
  //= require_tree .

fathom.js:

document.addEventListener('turbolinks:load', function(event) {
  if (window.location.hostname !== 'localhost') {
    fathom('trackPageview');
  }
})

Hope this helps! Modified from this comment.