Skip to content

Instantly share code, notes, and snippets.

@ryanshoover
Created August 5, 2019 19:53
Show Gist options
  • Save ryanshoover/f3eb4b5f2d59df4ace5a133f62a628bd to your computer and use it in GitHub Desktop.
Save ryanshoover/f3eb4b5f2d59df4ace5a133f62a628bd to your computer and use it in GitHub Desktop.
Server-side A/B testing with NGINX & Varnish (WP Engine)
add_header Set-Cookie "my_test_group=$test_group;Domain=.mydomain.com;Path=/;Max-Age=604800";
#############################
# Define our A/B Test group #
#############################
set $test_group a;
if ( $time_iso8601 ~ '[13579]\+00:00$' ) {
set $test_group b;
}
if ( $cookie_wpe_test_group != '' ) {
set $test_group $cookie_my_test_group;
}
####################
# Redirect Group B #
####################
set $variant '';
# Redirect Group B
if ( $test_group = b ) {
set $variant 'new';
}
# Don't redirect if we're hitting a WP resource file.
if ( $request_uri ~* ^/(wp-admin|wp-content|wp-includes)/? ) {
set $variant '';
}
# Don't redirect if we're already in the experiment page.
if ( $request_uri ~* ^/new/? ) {
set $variant '';
}
# Redirect to language path if we have one
if ( $variant != '' ) {
rewrite .* "/$variant/$request_uri" permanent;
}
@ryanshoover
Copy link
Author

This setup in WP Engine's NGINX rules will send 1/2 of all visitors from mydomain.com/path/to/post/ to mydomain.com/new/path/to/post/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment