Google Analytics for Facet Pages

With Google Analytics, it’s very easy to track how users are interacting with facets.

First, make sure your site is using the new GA embed (notice the ga() function):

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXX-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXX-1');
</script>

Next, we use the ga() function within the facetwp-loaded event to trigger a pageview.

<script>
(function($) {
    $(document).on('facetwp-loaded', function() {
        if (FWP.loaded) {
            gtag('config', 'UA-XXXXXXXX-1', {'page_path': window.location.pathname + window.location.search});
        }
    });
})(jQuery);
</script>

Both snippets can go into your theme’s header.php (or into a custom javascript file). Just make sure to replace UA-XXXXXXXX-1 with your actual Analytics identifier ID!