Output the amount of facet selections

<?php
// Outputs the number of currently selected facet options
// Optionally exclude your pager and/or sort facets (or any other facet)
// You need to correct for double counting facet types that have 2 values for 1 selection (e.g. Slider, Proximity, Date/Number Range etc.)

add_action( 'wp_head', function() {
  ?>
    <script>
      document.addEventListener('facetwp-loaded', function() {

        var count = 0;
        var exclusions = ['mypagerfacetname', 'mysortfacetname']; // don't count these facet selections
        var doubles = ['mysliderfacet','myproximityfacet','mydaterangefacet']; // double counting facets
          
        Object.entries(FWP.facets).forEach(([name, val]) => {
          if (exclusions.indexOf(name) < 0 && val.length > 0) {
            if (doubles.indexOf(name) > -1) {
              count += val.length/2;
            } else {
              count += val.length;
            }
          }
        });

        console.log(count);
        
      });
    </script>
  <?php
}, 100 );