Custom segmentation / filtering of test visitors

If you want to exclude visitors by something other than logged in status, URL query etc. You can use the filter below. In this example we are excluding traffic that has the cookie ‘staff’ set top true. but you could do it by IP address, or any other criterial you like.

JavaScript filtering (recommended)

As of version 2.1.1 you can add the window.abst.isTrackingAllowed flag

<script>
// To exclude a visitor from all A/B testing (e.g., staff members)

// Add this in the head, before the Footer, where our scripts execute.

if (document.cookie.includes('staff=true')) { // put your own logic here
    window.abst = window.abst || {}; // initialize the object if not already
    window.abst.isTrackingAllowed = false; // will not let any tests run, users will see default
}
</script>

PHP

NOTE: This is server side filtering, so you may need to exclude affected pages from your cache

// Add this to your theme's functions.php file or a custom plugin

add_filter('abst_is_tracking_allowed', 'exclude_staff_from_tracking', 10, 2);

function exclude_staff_from_tracking($is_allowed, $experiment_id) {

    // If tracking is already not allowed, return false

    if (!$is_allowed) {
        return false;
    }

    // Check if the "staff" cookie exists and is set to "true"

    if (isset($_COOKIE['staff']) && $_COOKIE['staff'] === 'true') {
        return false; // Don't allow tracking for staff members
    }

    return true; // Allow tracking for all other visitors

}

Leave a Comment

You must be logged in to post a comment.