Creating 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 benchmark super easy!

If you would like to create your custom benchmark follow these steps. Here, we will develop a role change benchmark.

1. Getting Template

To start building your own benchmark download extension template ( click here) and open that in your code editor. For a detailed description of all the classes and functionality please follow the documentation link (click here).

2. Setting Up Defaults

Once you get your template update all the TODO list and personalize your extension.

3. Create a benchmark class

Once all the initialization completes. Create a file for a benchmark. You can create file In includes> steps> role-changed.

Open the file and assign a class name for the benchmark. Add namespace for the class using which file is imported using an autoloader. Make sure you entered valid namespace based on the file located inside your project because namespace help autoloader to find the location of the file.

All benchmark in Groundhogg extends Grounhoggs Benchmark class and Groundhoggs Benchmark class contains various abstract methods which need to be implemented for the custom benchmarks.

namespace GroundhoggExtension\Steps;
use Groundhogg\Steps\Benchmarks\Benchmark;

class Role_Changed extends Benchmark
{
    protected function get_complete_hooks()
    {
        // TODO: Implement get_complete_hooks() method.
    }


    protected function get_the_contact()
    {
        // TODO: Implement get_the_contact() method.
    }


    protected function can_complete_step()
    {
        // TODO: Implement can_complete_step() method.
    }


    public function get_name()
    {
        // TODO: Implement get_name() method.
    }


    public function get_type()
    {
        // TODO: Implement get_type() method.
    }


    public function get_description()
    {
        // TODO: Implement get_description() method.
    }


    public function get_icon()
    {
        // TODO: Implement get_icon() method.
    }


    public function settings( $step )
    {
        // TODO: Implement settings() method.
    }


    public function save( $step )
    {
        // TODO: Implement save() method.
    }
}<br>

4. Initializing Benchmark

Once you have a list of methods which you need to be implemented you can start customizing these methods. You can start with the methods which initialize the benchmark. 

/**
     * Get the element name
     *
     * @return string
     */
    public function get_name()
    {
        return _x( 'Role Changed', 'step_name', 'groundhogg' );
    }


    /**
     * Get the element type
     *
     * @return string
     */
    public function get_type()
    {
        return 'role_changed';
    }


    /**
     * Get the description
     *
     * @return string
     */
    public function get_description()
    {
        return _x( "Runs whenever a user's role is changed.", 'step_description', 'groundhogg' );
    }


    /**
     * Get the icon URL
     *
     * @return string
     */
    public function get_icon()
    {
        return GROUNDHOGG_EXTENSION_ASSETS_URL . '/images/role-changed.png';
    }<br>

5. Implement Settings for the Benchmark

Benchmark can have different settings and you can create various settings for your benchmark by overriding the settings method of the benchmark. Displaying new settings comes with the responsibility of storing them. You can use the save method of the benchmark class to store the values of your custom settings.these settings are getting stored in the database when the user clicks on Update button inside funnel builder 

Here, Role Changed benchmark contains one setting which is a list of roles. Admin can select and save roles based on which they want to run automation. 

    /**
     * @param $step Step
     */
    public function settings( $step )
    {
        $this->start_controls_section();


        $this->add_control( 'role', [
            'label'         => __( 'Run when this access is given:', 'groundhogg' ),
            'type'          => HTML::SELECT2,
            'default'       => [ 'subscriber' ],
            'description'   => __( 'Users with these roles will trigger this benchmark.', 'groundhogg' ),
            'multiple' => true,
            'field'         => [
                'multiple' => true,
                'data'  => Plugin::$instance->roles->get_roles_for_select(),
            ],
        ] );


        $this->end_controls_section();
    }


    /**
     * Save the step settings
     *
     * @param $step Step
     */
    public function save( $step )
    {
        $this->save_setting( 'role', array_map( 'sanitize_text_field', $this->get_posted_data( 'role', [ 'subscriber' ] ) ) );
    }

6. Performing Operation

Next step in a process to hook this code to a specific action. You can do that by implementing  get_complete_hooks. This method returns the array of hooks for which this benchmark is designed. 

The next step in the process is to locate meaningful data from the response you received from a complete hook. For that, You can use the setup method of the benchmark class to store received data and compare them for validation against the settings.

The next step is to check action can be completed. This setting used to validate the run condition and you can do that by overriding  can_complete_step method. 

Once all the setups are completed you can get the contact using email or contact id and add them to funnel using the add_the_contact method.

The final code for the benchmark after implementing all the methods it looks like follows.

namespace GroundhoggExtension\Steps;
use Groundhogg\Contact;
use function Groundhogg\create_contact_from_user;
use Groundhogg\HTML;
use Groundhogg\Plugin;
use Groundhogg\Step;

if ( ! defined( 'ABSPATH' ) ) exit;

class Role_Changed extends Benchmark
{
   /**
     * Get the element name
     *
     * @return string
     */
    public function get_name()
    {
        return _x( 'Role Changed', 'step_name', 'groundhogg' );
    }

    /**
     * Get the element type
     *
     * @return string
     */
    public function get_type()
    {
        return 'role_changed';
    }

    /**
     * Get the description
     *
     * @return string
     */
    public function get_description()
    {
        return _x( "Runs whenever a user's role is changed.", 'step_description', 'groundhogg' );
    }

    /**
     * Get the icon URL
     *
     * @return string
     */
    public function get_icon()
    {
        return GROUNDHOGG_EXTENSION_ASSETS_URL . '/images/role-changed.png';
    }
  
    /**
     * @param $step Step
     */
    public function settings( $step )
    {
        $this->start_controls_section();


        $this->add_control( 'role', [
            'label'         => __( 'Run when this access is given:', 'groundhogg' ),
            'type'          => HTML::SELECT2,
            'default'       => [ 'subscriber' ],
            'description'   => __( 'Users with these roles will trigger this benchmark.', 'groundhogg' ),
            'multiple' => true,
            'field'         => [
                'multiple' => true,
                'data'  => Plugin::$instance->roles->get_roles_for_select(),
            ],
        ] );


        $this->end_controls_section();
    }

    /**
     * Save the step settings
     *
     * @param $step Step
     */
    public function save( $step )
    {
        $this->save_setting( 'role', array_map( 'sanitize_text_field', $this->get_posted_data( 'role', [ 'subscriber' ] ) ) );
    }

    /**
     * get the hook for which the benchmark will run
     *
     * @return string[]
     */
    protected function get_complete_hooks()
    {
        return [ 'set_user_role' => 3, 'add_user_role' => 2 ];
    }

    /**
     * @param $userId int the ID of a user.
     * @param $cur_role string the new role of the user
     * @param $old_roles array list of previous user roles.
     */
    public function setup( $userId, $cur_role, $old_roles=array() )
    {
        $this->add_data( 'user_id', $userId );
        $this->add_data( 'role', $cur_role );
    }

    /**
     * Get the contact from the data set.
     *
     * @return Contact
     */
    protected function get_the_contact()
    {
        return create_contact_from_user( $this->get_data( 'user_id' ) );
    }

    /**
     * Based on the current step and contact,
     *
     * @return bool
     */
    protected function can_complete_step()
    {
        $role = $this->get_setting( 'role' );
        $step_roles = is_array( $role )? $role : [ $role ];
        $added_role = $this->get_data( 'role' );
        return in_array( $added_role, $step_roles );
    }
}

7. Create Object Of Benchmark

After completing all the steps above your benchmark is ready for the operation but, you will not able to find this benchmark in your settings yet. To view This benchmark in a funnel builder you need to create an object of a benchmark.

To do that, open the plugin file and add the following code. It registers a new benchmark and now you will be able to see the working benchmark in your funnel builder.

FILE: your_extension_folder/includes/plugin.php
   /**
     * Register Benchmark and Action.
     * @param \Groundhogg\Steps\Manager $manager
     */
    public function register_funnel_steps($manager)
    {
        $manager->add_step( new Role_Changed() );
    }
Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us