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. 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 documentation link (click here).

2. Setting Up Defaults

Once 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.

Implement the abstract method.

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.
    }
}

4. Initialize Benchmark

once you have all the methods implement benchmark methods. add the following code to do that.


   /**
     * 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';
    }

5. Implement Settings for the Benchmark

Role Changed benchmark contains one setting which is a list of roles. Admin can select and save role based on which they want to run automation. These settings are created by implementing settings and they are stored in a database using the Save method.


 /**
     * @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.

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 looks like follow.

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 into funnel builder you need to create an object of a benchmark.

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

   /**
     * Register Benchmark and Action.
     * @param \Groundhogg\Steps\Manager $manager
     */
    public function register_funnel_steps($manager)
    {
        $manager->add_step( new Role_Changed() );
    }
Was this article helpful to you? Yes No

How can we help?