1. Home
  2. Docs
  3. Developer Docs
  4. Custom Funnel Elements
  5. Custom Benchmark

Custom Benchmark

Groundhogg provides an extensive set of Benchmarks to listen for special website events that launch automated workflows in the funnel builder. But sometimes you need to do something a little special. So we’ve made creating your own benchmark super easy!

If you would like to create your own benchmark follow these steps..

1. The Funnel Step Class

To start building your own benchmark we will define a class call My_Benchmark. This class will extend the WPGH_Funnel_Step class.

The WPGH_Funnel_Step class does a LOT of the heavy lifting for you as far as hooking in to the rest of Groundhogg and WordPress, extending it will make our job a LOT easier.

class My_Benchmark extends WPGH_Funnel_Step
{
    //todo 
}

2. Setting Up Your Benchmark

In the __construct method you need to define which type of step it is, the unique step identifier, pretty step name and a url to an icon to use in the funnel builder benchmarks panel.

public function __construct()
{
    # Give your benchmark a custom identifier. NOTE: Your identifier must be less than 20 characters in length.
    $this->type     = 'custom_benchmark';

    # You need to define that this is in fact a benchmark.
    $this->group    = 'benchmark';

    # This is the name of the benchmark as seen from the funnel builder panel.
    $this->name     = __( 'My Benchmark' );

    # Define a url to the image you'd like to use as an Icon. Square images are reccomended.
    $this->icon     = 'url_to_my_icon.png' ;

    # you MUST call the parent __construct method as well.
    parent::__construct();
}

At this point, if you where to add the benchmark to your project, you’d see the icon show up in the benchmarks panel.

3. Completing A Benchmark

One important aspect of Benchmarks is the concept that they are “Completed” whenever an action happens in WordPress. Thus we need to register an action in the __construct method to listen for something that would complete our benchmark.

add_action( 'some_wordpress_or_plugin_action' , array( $this, 'complete' ) );

Above code make sure when certain actions  happen the ‘complete’ method is called and “completes” the benchmark in the funnel.

So far we have…

class My_Benchmark extends WPGH_Funnel_Step
{
    //todo 

    public function __construct()
    {
        # Give your benchmark a custom identifier. NOTE: Your identifier must be less than 20 characters in length.
        $this->type     = 'custom_benchmark';

        # You need to define that this is in fact a benchmark.
        $this->group    = 'benchmark';

        # This is the name of the benchmark as seen from the funnel builder panel.
        $this->name     = __( 'My Benchmark' );

        # Define a url to the image you'd like to use as an Icon. Square images are reccomended.
        $this->icon     = 'url_to_my_icon.png' ;

        # you MUST call the parent __construct method as well.
        parent::__construct();

        # Register an action to listen for events that will complete the benchmark.
        add_action( 'some_wordpress_or_plugin_action' , array( $this, 'complete' ) );
    }

}

4. Add Your Settings

A benchmark would be pretty useless unless it had some settings for you to define what happens.

To register your own settings is fairly simple. Just add the settings method to your class.

The settings method accepts 1 parameter which is a WPGH_Step object. This particular class has a LOT of helper methods that will allow you to

define your settings.

/**
 * @param $step WPGH_Step the step object with helper methods.
 */
public function settings( $step )
{

     $my_setting = $step->get_meta( 'my_setting' );
     if ( ! $my_setting )
         $my_setting = 'something else';

     ?>
     <table class="form-table">
     <tr>
         <th><?php _e( 'My Setting' ); ?></th>
         <td><?php
              $args = array(
                  # use the $step->prefix helper method to uniquely identify settings in the funnel builder.
                  'name' => $step->prefix( 'my_setting' ),
                  'value' => $my_setting,
              );

              # use the WPGH()->html helper class to create a setting quickly.
              echo WPGH()->html->input( $args ); ?>
         </td>
     </tr>
     </table>
     <?php
}

5. Save Your Settings

Now that you’ve displayed your settings you should save them so that they can be used in the complete method.

/**
 * @param $step WPGH_Step the step object with helper methods.
 */
public function save ( $step )
{
    # our settings were displayed using the $step->prefix() method, so we access them in the $_POST using the same method.

    if ( isset( $_POST[ $step->prefix( 'my_setting' ) ] ) ){
        $step->update_meta( 'my_setting', sanitize_text_field( $_POST[ $step->prefix( 'my_setting' ) ] ) );
    }
}

6. Implement the Complete Method

Now that we have our settings, we can implement the complete method which will “complete” the benchmark.

Most benchmarks in CORE have similar complete methods. Do the following…

/**
 * @param $some_args mixed whatever this hook passes?
 */
public function complete( $some_args )
{

    # We need to get the current contact which we can do via the following.
    $contact = WPGH()->tracking->get_contact();

    # Exit out if there is no contact to use.
    if ( ! $contact ){
        return;
    }    

    # Get all the benchmarks of this type.
    $steps = WPGH()->steps->get_steps( array( 'step_type' => $this->type, 'step_group' => $this->group ) );

    # Exit out if there are not benchmarks of this type
    if ( empty( $steps ) )
        return;

    # iterate through the benchmarks
    foreach ( $steps as $step ){

        # Transform step to WPGH_Step object to use helper methods.
        $step = new WPGH_Step( $step->ID );

        # Get your setting
        $my_setting = $step->get_meta( 'my_setting' );

        # perform some sort of check on the args
        $can_complete = $my_setting === $some_args;

        if ( $step->can_complete( $contact ) && $can_complete ) ){

            # this will add the complete event to the queue for processing.
            $step->enqueue( $contact );

        }
    }
}

7. Putting It All Together

class My_Benchmark extends WPGH_Funnel_Step
{
    //todo 

    public function __construct()
    {
        # Give your benchmark a custom identifier. NOTE: Your identifier must be less than 20 characters in length.
        $this->type     = 'custom_benchmark';

        # You need to define that this is in fact a benchmark.
        $this->group    = 'benchmark';

        # This is the name of the benchmark as seen from the funnel builder panel.
        $this->name     = __( 'My Benchmark' );

        # Define a url to the image you'd like to use as an Icon. Square images are reccomended.
        $this->icon     = 'url_to_my_icon.png' ;

        # you MUST call the parent __construct method as well.
        parent::__construct();

        # Register an action to listen for events that will complete the benchmark.
        add_action( 'some_wordpress_or_plugin_action' , array( $this, 'complete' ) );
    }

    /**
     * @param $step WPGH_Step the step object with helper methods.
     */
    public function settings( $step )
    {

         $my_setting = $step->get_meta( 'my_setting' );
         if ( ! $my_setting )
             $my_setting = 'something else';

         ?>
         <table class="form-table">
         <tr>
             <th><?php _e( 'My Setting' ); ?></th>
             <td><?php
                  $args = array(
                      # use the $step->prefix helper method to uniquely identify settings in the funnel builder.
                      'name' => $step->prefix( 'my_setting' ),
                      'value' => $my_setting,
                  );

                  # use the WPGH()->html helper class to create a setting quickly.
                  echo WPGH()->html->input( $args ); ?>
             </td>
         </tr>
         </table>
         <?php
     }

    /**
     * @param $step WPGH_Step the step object with helper methods.
     */
    public function save ( $step )
    {
        # our settings were displayed using the $step->prefix() method, so we access them in the $_POST using the same method.

        if ( isset( $_POST[ $step->prefix( 'my_setting' ) ] ) ){
            $step->update_meta( 'my_setting', sanitize_text_field( $_POST[ $step->prefix( 'my_setting' ) ] ) );
        }
    }

    /**
     * @param $some_args mixed whatever this hook passes?
     */
    public function complete( $some_args )
    {

        # We need to get the current contact which we can do via the following.
        $contact = WPGH()->tracking->get_contact();

        # Exit out if there is no contact to use.
        if ( ! $contact ){
            return;
        }    

        # Get all the benchmarks of this type.
        $steps = WPGH()->steps->get_steps( array( 'step_type' => $this->type, 'step_group' => $this->group ) );

        # Exit out if there are not benchmarks of this type
        if ( empty( $steps ) )
            return;

        # iterate through the benchmarks
        foreach ( $steps as $step ){

            # Transform step to WPGH_Step object to use helper methods.
            $step = new WPGH_Step( $step->ID );

            # Get your setting
            $my_setting = $step->get_meta( 'my_setting' );

            # perform some sort of check on the args
            $can_complete = $my_setting === $some_args;

            if ( $step->can_complete( $contact ) && $can_complete ) ){

                # this will add the complete event to the queue for processing.
                $step->enqueue( $contact );

            }
        }
    }
}

// somewhere else in your project...
$my_benchmark = new My_Benchmark();

Implement all the methods with your own custom code and you are ready to launch your own new custom benchmark for Groundhogg.

Was this article helpful to you? Yes No

How can we help?