- Published at
How to A/B Test Gravity Forms
How I A/B test Gravity Forms without breaking anything
- Authors
-
-
- Name
- AJ Dichmann
- VP of Digital Strategy at Globe Runner
-
Table of Contents
If you’ve been wanting to A/B test different Gravity Forms on your WordPress site but don’t want to deal with clunky third-party tools or risk slowing things down, this method might be exactly what you need.
We’re going to walk through a simple way to test A/B multiple forms using a custom shortcode. It’s fast, lightweight, and everything runs on the server, so you avoid all the flickering and loading issues that can come with other CRO tools.
Why Test Different Gravity Forms?
You probably already know the value of A/B testing, but here’s a quick refresher.
Testing your forms can help you:
- Increase leads and conversions
- Try out different headlines, layouts, or incentives
- Understand what works best for your audience
- Test different form types (1 step vs 2 step)
Even small changes, like updating the button text or tweaking your call to action, can have a measurable impact. But if you’re not testing, you’re just guessing.
Why Most A/B Tools Aren’t Great for Forms
Most A/B testing tools like Google Optimize, Optimizely, or VWO work by using JavaScript to change content after the page loads.
That creates a few problems:
- Visitors often see the original version flash briefly before it switches
- Extra scripts can slow down the page and delay your form from loading
- Some users block tracking scripts, so your data might not be reliable
- Complex forms with conditional logic or AJAX steps often break when the DOM is changed on the fly
These tools just weren’t built with Gravity Forms in mind.
Smart Form Testing Strategy
Client-side A/B testing tools can break complex forms and create tracking issues. A server-side approach ensures faster loads, more accurate data, and fewer bugs—especially when working with Gravity Forms.
A Simpler Way That Actually Works
Instead of using JavaScript to swap out forms after the page loads, we’re going to use a PHP shortcode that picks which form to show before the page is even sent to the browser.
Here’s how it works:
- When a visitor lands on your page, a cookie assigns them to a random form version
- That version is served directly from the server
- The same version will show again when they return, for up to 30 days
No flicker. No scripts. Just a consistent, smooth experience for your visitors.
The Code You’ll Need
Add this function to your functions.php
file or your site’s custom plugin:
// A/B test shortcode for Gravity Forms using [gf_ab_test formid='1|5']
if (!function_exists('gf_ab_test_set_cookie')) {
function gf_ab_test_set_cookie() {
if (is_admin()) return; // Don't run in admin
// Only run on the front end
if (!isset($_COOKIE['gf_ab_variant']) && isset($_GET['gf_ab_test_ids'])) {
$form_ids = explode('|', sanitize_text_field($_GET['gf_ab_test_ids']));
$form_ids = array_filter(array_map('trim', $form_ids));
$variant_count = count($form_ids);
if ($variant_count > 0) {
$user_variant = mt_rand(0, $variant_count - 1);
setcookie(
'gf_ab_variant',
$user_variant,
time() + (30 * DAY_IN_SECONDS),
COOKIEPATH,
COOKIE_DOMAIN,
is_ssl(),
true
);
$_COOKIE['gf_ab_variant'] = $user_variant;
}
}
}
add_action('init', 'gf_ab_test_set_cookie');
}
if (!function_exists('gf_ab_test_shortcode')) {
function gf_ab_test_shortcode($atts) {
$atts = shortcode_atts(array(
'formid' => '',
), $atts);
$form_ids = array_filter(array_map('trim', explode('|', $atts['formid'])));
if (count($form_ids) < 1) {
return '';
}
// Pass form IDs to the cookie setter via a query var
if (!isset($_COOKIE['gf_ab_variant'])) {
// Add a query var to trigger cookie set on next page load
$url = add_query_arg('gf_ab_test_ids', implode('|', $form_ids));
// Redirect to set the cookie
wp_redirect($url);
exit;
}
$variant_count = count($form_ids);
$user_variant = isset($_COOKIE['gf_ab_variant']) ? intval($_COOKIE['gf_ab_variant']) : 0;
if ($user_variant >= $variant_count) {
$user_variant = 0;
}
$selected_id = esc_attr($form_ids[$user_variant]);
return do_shortcode("[gravityform id=\"$selected_id\" title=\"false\"]");
}
add_shortcode('gf_ab_test', 'gf_ab_test_shortcode');
}
Now use this shortcode wherever you want the test to run:
[gf_ab_test formid='1|5']
You can include as many form variations as you want. Just separate them with a pipe symbol (|).
A Few Tips to Make Your Test Even Better
If you want to track performance, it’s easy to do with Google Tag Manager and Google Analytics 4. Here’s how:
- Set up an Impression Trigger in Tag Manager to fire when the form becomes visible on the page
- Create a Form Submit Trigger to fire when someone completes the form
- Send both events to GA4 and pass the form variant as a parameter or event label
This gives you clean, reliable tracking without relying on extra scripts from a testing platform.
A few more quick tips:
- Keep your changes minimal. Just test one or two differences at a time so you know what’s making the impact
- Let the test run long enough to get useful results. The more traffic, the quicker you’ll get your answer
- This server-side method avoids all the issues that come with DOM manipulation, especially on forms with conditional logic or multiple steps
Wrap-Up
You don’t need a complicated tool or a bloated script to test Gravity Forms. This shortcode approach is fast, flexible, and doesn’t mess with your site speed or break your forms.
It’s easy to drop in and just works. No flicker. No weird behavior. No extra cost.
If you try it out, I’d love to hear what kind of results you see. Let me know which version wins for you.