Gravity Wiz

Magically enhanced tutorials, snippets and plugins for Gravity Forms!

  • Gravity Perks
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    • Documentation
    • Support
    • Account

Simple Split Testing with Gravity Forms

Which version of your form performs the best? Split Testing for Gravity Forms will help you find out.

Last updated November 3, 2014 | Written by David Smith 20 Comments

  • March 5th, 2014: Fixed issue where validation errors would sometimes result in another form being loaded. Byproduct of this fix: multi-page forms are now supported.

Matt Medeiros wrote an awesome article about A/B Testing with Gravity Forms (also known as split testing). Matt shared a small function which accepts two Gravity Form IDs. The provided form IDs are randomly alternated so each form is displayed about half of the time. A handy little function!

Feeling a bit inspired, I thought it might be easier for user’s to implement if this were available as part of the [gravityform] shortcode. I also figured it couldn’t hurt to add support for more than two forms.

View DemoShow CodeDownload Code
<?php
/**
* Gravity Wiz // Split Testing for Gravity Forms
*
* Allows you to test the effectiveness of two or more Gravity Forms by using a single shortcode (or function call) to
* randomly alternating which form is displayed. Effectiveness can be measured by the "Conversion" column available
* by default on the Gravity Forms' "Forms" list view.
*
* Based on https://gist.github.com/fatmedia/8289103 by @realFATmedia via @mattreport
*
* @version 1.0
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link https://gravitywiz.com/simple-split-testing-gravity-forms/
* @copyright 2013 Gravity Wiz
*/
class GW_Split_Testing {
protected static $instance = null;
public static function get_instance() {
if( null == self::$instance )
self::$instance = new self;
return self::$instance;
}
private function __construct() {
add_filter( 'gform_shortcode_split_test', array( $this, 'do_split_test_shortcode' ), 10, 2 );
}
public function do_split_test_shortcode( $output, $atts ) {
// get our "random" form ID from the provided form IDs
$form_ids = array_map( 'trim', explode( ',', $atts['ids'] ) );
return $this->get_split_test_form( $form_ids, $atts );
}
public function get_split_test_form( $form_ids = array(), $atts = array() ) {
if( empty( $form_ids ) )
return;
if( rgpost( 'gform_submit' ) && in_array( rgpost( 'gform_submit' ), $form_ids ) ) {
$form_id = rgpost( 'gform_submit' );
} else {
$index = mt_rand( 0, count( $form_ids ) - 1 );
$form_id = $form_ids[$index];
}
// modify attributes to create form-generating shortcode
$atts['action'] = 'form';
$atts['id'] = $form_id;
// generate [gravityform] form shortcode
$shortcode_bits = array();
foreach( $atts as $key => $value ) {
if( is_array( $value ) )
$value = implode( ',', $value );
if( $value === true )
$value = 'true';
if( $value === false )
$value = 'false';
$shortcode_bits[] = "{$key}=\"$value\"";
}
$shortcode = '[gravityform ' . implode( ' ', $shortcode_bits ) . ' /]';
// get the form markup by processing the generated shortcode
$form_markup = do_shortcode( $shortcode );
return $form_markup;
}
}
function gw_split_testing() {
return GW_Split_Testing::get_instance();
}
gw_split_testing();
view raw gw-gravity-forms-split-testing.php hosted with ❤ by GitHub

How do I get started?

  1. Copy and paste the snippet into your theme’s functions.php file.
  2. Configure the shortcode via the instructions below.

The Shortcode

[gravityform action="split_test" ids="1,2" /]

Shortcode Parameters

  • action
    Use “split_test” to activate the split test functionality.
  • ids
    A comma-delimited list of form IDs which should be displayed.

Function Call

If you need to implement this in a template file or other advanced usage, you can make use of the function call.

if( function_exists( 'gw_split_testing' ) ) {
echo gw_split_testing()->get_split_test_form( $form_ids, $attributes );
}

Function Parameters

  • $form_ids (array) An array of form IDs that should be randomly alternated for display.
  • $attributes (array) An array of settings used to configure form properties. Settings include:
    • title: (bool) Whether to display the form title. Defaults to true.
    • description: (bool) Whether to display the form description. Defaults to true.
    • ajax: (bool) Whether to enable AJAX for the displayed form. Defaults to false.
    • tabindex: (int) The starting tab index for displayed form.

Basic Usage

In this example, we pass form IDs “1” and “2” to the split testing function.

if( function_exists( 'gw_split_testing' ) ) {
echo gw_split_testing()->get_split_test_form( array( 1, 2, ) );
}

Advanced Usage

In this example, we pase form IDs “1” and “2” and set the title, description and ajax options.

if( function_exists( 'gw_split_testing' ) ) {
echo gw_split_testing()->get_split_test_form(
array( 1, 2, ),
array(
'title' => 'true',
'description' => 'false',
'ajax' => 'true'
)
);
}

How do I measure the effectiveness of the forms?

Gravity Forms has built-in conversion tracking for all your forms. You can see how many views each form has and how many entries. In the far right column, you can see the “Conversion” percentage. The higher the percentage the more effective the form is at getting users to fill it out.

split-testingExample of Gravity Forms Forms List View Conversion Column

Summary

For more details on the benefits of split testing, make sure you check out Matt’s original article.

Have suggestions to improve this functionality? Let me know in the comments!

Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 29+ essential add-ons for Gravity Forms with support you can count on.

  • View All Perks
  • Buy Gravity Perks

Filed Under: Snippets

Comments

  1. Shmuel says

    November 7, 2018 at 8:17 pm

    Is there a way to split test individual questions within one form instead of creating multiple separate forms?

    Reply
    • David Smith says

      November 8, 2018 at 7:13 am

      We don’t have a solution for this one, Shmuel.

  2. Alex says

    March 22, 2017 at 7:50 pm

    Thank you so much for this. This saved me so much time today!

    Reply
    • David Smith says

      March 22, 2017 at 10:23 pm

      Glad to help, Alex! Thanks for the shoutout on Twitter. :)

  3. Mason Lawlor says

    April 1, 2016 at 1:32 am

    Hey David! Love this script and got it working on one of my sites earlier this week. I’m trying to think of how to extend it’s usability.

    I would love the ability to generate dynamic content based on which form is showing.

    For example:

    Let’s say I have a toll-free number on my page and I’d like to link up 2 #s to test which one converts best when a particular form is shown. Maybe my long form has a better call-in conversion rate.

    Or say I have a heading above the form that says something different based on which form is showing. (i.e. “Enter your phone number below” vs. “Enter your email and phone number below”.

    Would love to know if this is even feasible. Thanks! Mason

    Reply
    • David Smith says

      April 1, 2016 at 9:03 am

      Hi Mason, I took a look through the code to see if this is something that could be easily implemented but it just isn’t set up very well for anything besides split testing the Gravity Forms themselves. You might need a more robust split testing solution for your goals.

  4. zak loh says

    July 19, 2014 at 5:38 pm

    Hi, I cannot get this to work. Am I supposed to paste the code shown at

    https://gist.githubusercontent.com/spivurno/8292521/raw/gw-gravity-forms-split-testing.php

    and then paste the following code below the above:

    Basic Function Usage

    if( function_exists( ‘gw_split_testing’ ) ) { echo gw_split_testing()->get_split_test_form( array( 1, 2, ) ); }

    I’m guessing (noobie here) that if I paste this at the bottom of my functions file that it shoudl be before

    <?php
    

    }

    Would so appreciate your help.

    Thanks

    Zak

    Reply
  5. John Kirker says

    June 19, 2014 at 9:09 pm

    Hi David,

    I’m digging this. I’ve got it working if I use a shortcode, however if I use the advanced PHP function I’m not getting any output.

    I’ve done an echo ‘hello’; after the IF condition and that part is passing – however I’m not getting any love beyond that.

    No errors – nothing strange with debug turned on.

    Form at top: http://windoronline.com/win-dor-locations

    Happy to pay for your time for some help.

    I’ll email the pastie link as I don’t want to expose it publicly.

    Reply
    • David Smith says

      June 22, 2014 at 2:10 pm

      Per our email exchange, I’ve updated the function usage examples to indicate that you need to echo the results of the function out. Thanks for confirming that this worked for you, John.

  6. Damien says

    March 28, 2014 at 6:59 pm

    Thanks for this tutorial. Maybe you could offer it as an extension for GF somehow :)

    I’m with WPE too — it would be good if they made it possible to exclude URLs from caching in the dashboard somehow rather than having to email support.

    Reply
    • David Smith says

      March 29, 2014 at 7:46 am

      Thanks for the feedback. I definitely wouldn’t mind offering this as a perk. And HUGE second on being able to exclude URLs from caching in the dashboard… I hate to have to contact them every time, but at least they are quick to respond.

  7. Alex says

    February 21, 2014 at 12:08 pm

    I’m noticing a problem using your snippet with multi-page, non-ajax enabled forms. When I submit one of those forms, the snippet code seems to run for some reason instead of actually submitting, so that final submission button is taking me back to page 1 of whatever form wins the random roll. It’s odd, but seems to not be a problem when ajax is enabled.

    Reply
    • David Smith says

      March 5, 2014 at 10:40 pm

      Hi Alex, I’ve updated this snippet to resolve this issue. See the March 5th update at the top of the post for more details.

  8. Adam W. Warner says

    January 9, 2014 at 9:46 am

    Hi David, just came here from Matt’s article. Great stuff:)

    Tried viewing your demo and refreshing (a lot) and never saw Form A, only B.

    Tried in several browsers.

    Can you confirm your demo is working as expected?

    Reply
    • David Smith says

      January 9, 2014 at 9:51 am

      Hey Adam, I realized last night that my host’s caching was preventing this demo from working. I’ve got a support ticket in with them and anticipate the demo will be working correctly very soon. I’ll post a follow up once I’ve confirmed. Thanks for checking up!

    • David Smith says

      January 9, 2014 at 2:10 pm

      I’m with WP Engine as well. They’ve removed the demo page from the cache and it’s working correctly for me. Looks like you will need to have any page you’re using this on excluded from WPE’s caching. Assuming it wouldn’t be a problem to include it in the cache again after your A/B testing is complete.

      Also, will send you an email shortly. :)

    • Adam W. Warner says

      January 9, 2014 at 10:58 am

      Good to know David, who’s your host?

      I ask because we’re using WPE and have had to have several urls excluded from their caching (for eCommerce) so I’m wondering if the caching will be an ongoing issues with this script that will affect the actual A/B intent. Know what I mean?

    • Adam W. Warner says

      January 9, 2014 at 11:41 am

      Hey David, can you shoot me an email? I’d like to discuss some business with you.

  9. Nate says

    January 7, 2014 at 3:47 pm

    This is awesome! I’m excited to test this on a new site we just created using Gravity Forms as the ordering process. Thanks!

    Reply
    • David Smith says

      January 7, 2014 at 6:30 pm

      Glad to hear it, Nate! Let me know how it works for you and any suggestions you have to improve it.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Categories

  • How To (29)
  • News (20)
  • Plugins (5)
  • Releases (5)
  • Resource (1)
  • Snippets (61)
  • Tip (1)
  • Tutorials (43)
  • Updates (62)

Recent Posts

  • Gravity Wiz Weekly #62
  • Gravity Forms Populate Anything, Public Beta Available!
  • Gravity Wiz Weekly #61
  • Gravity Wiz Weekly #60
  • Gravity Wiz Weekly #59

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Copyright © 2019 · Powered by WordPress · Gravity Wiz LLC · Log out

  • Support
  • Affiliates
  • About
  • Sitemap
  • Gravity Perks
    ▼
    • Gravity Perks
    • Tutorials & Snippets
    • About
  • Support
    ▼
    • Documentation
    • Support
    • Account