→ 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):

<script>

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-XXXXXXXX-X', 'auto'); // enter your account ID
  ga('send', 'pageview');

</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) {
            ga('send', 'pageview', window.location.pathname + window.location.search);
        }
    });
})(jQuery);
</script>

That’s it! Both snippets can go into your theme’s header.php (or into a custom javascript file).