facetwp conditionally change templates

<?php
/** conditionally change templates on ajax refresh, only works for use with facetwp
 ** layout builder templates
 **/
add_action( 'wp_footer', function() {
	?>
    <script>
        (function($) {
            $(document).on('facetwp-refresh', function() {
                if ( 'undefined' !== typeof FWP.facets['categories'] && FWP.facets['categories'].length > 0 ) {
                    FWP.template = 'example_2';
                } else {
                    FWP.template = 'example';
                }
            });
        })(jQuery);
    </script>
	<?php
}, 100 );