Adding custom reports

Groundhogg provides functionality to add reports in the Groundhogg dashboard. Using the Groundhogg dashboard you can add tables, line chart, donut chart in the dashboard. To add your custom chart you need to create custom tab inside the dashboard first. We also have a help document on how to add a custom chart.

Once a tab is successfully added its time to add reports itself. for that, you need to register a list of reports which you would like to add in your custom tab of the dashboard. You can do that by using groundhogg/reports/setup_default_reports/after action.

class My_Reports {

   protected $start;
   protected $end;

   public function __construct() {
      add_action( 'groundhogg/admin/init', [ $this, 'init' ] );
      add_action( 'groundhogg/reports/setup_default_reports/after', [ $this, 'register_new_reports' ] );
   }



   public function init() {
      Groundhogg::$instance->admin->reporting->add_custom_report_tab( [
         'name'     => __( 'My Tab', 'groundhogg' ),
         'slug'     => 'my_tab',
         'reports'  => [
            'chart_my_report', //Name of the Report which you want to load            
         ],
         'callback' => [ $this, 'view' ] //Contains HTML 
      ] );
   }

   /**
    * Html Code
    */ 
   public function view() {
	?>
	<div class="groundhogg-report">
    		<h2 class="title"><?php _e( 'My Chart', 'groundhogg-pipeline' ); ?></h2>
    		<div class="big-chart-wrap">
        		<canvas id="chart_my_report"></canvas>
    		</div>
	</div>

	<?php
   }

   /**
    * Add the new reports
    *
    * @param $reports \Groundhogg\Reports
    */
   public function register_new_reports( $reports ) {
      $this->start = $reports->start; //stores start time recived form the Groundhogg datepicker so we can use that in custom reports.
      $this->end   = $reports->end;
      $new_reports = [
         [
            'id'       => 'chart_my_report',
            'callback' => [ $this, 'chart_my_report' ]
         ],
      ];
      foreach ( $new_reports as $new_report ) {
         $reports->add( $new_report['id'], $new_report['callback'] );
      }
   }

   

   /**
    * Register local function 
    */ 
   
   public function chart_my_report() {
      $report = new chart_my_report( $this->start, $this->end );
      return $report->get_data();
   }

}

Here we can see that various reports are defined with the corresponding get data method. You need to implement a custom class which extends the class from Groundhogg and returns data to display into the canvas. class to display opt-in status of contact look like follow.

use Groundhogg\Preferences;


class Chart_My_Report extends Base_Doughnut_Chart_Report {

   protected function get_chart_data() {

      $values = wp_list_pluck( $this->get_new_contacts_in_time_period(), 'optin_status' );
      $counts = array_count_values( $values );

      $data  = [];
      $label = [];
      $color = [];

      // normalize data
      foreach ( $counts as $key => $datum ) {
         $normalized = $this->normalize_datum( $key, $datum );
         $label []   = $normalized ['label'];
         $data[]     = $normalized ['data'];
         $color[]    = $normalized ['color'];
      }

      return [
         'label' => $label,
         'data'  => $data,
         'color' => $color
      ];

   }

   /**
    * Normalize a datum
    *
    * @param $item_key
    * @param $item_data
    *
    * @return array
    */
   protected function normalize_datum( $item_key, $item_data ) {
      switch ( $item_key ) {
         default:
         case Preferences::UNCONFIRMED:
            $label = __( 'Unconfirmed', 'groundhogg' );
            break;
         case Preferences::CONFIRMED:
            $label = __( 'Confirmed', 'groundhogg' );
            break;
         case Preferences::HARD_BOUNCE:
            $label = __( 'Bounced', 'groundhogg' );
            break;
         case Preferences::SPAM:
            $label = __( 'Spam', 'groundhogg' );
            break;
         case Preferences::UNSUBSCRIBED:
            $label = __( 'Unsubscribed', 'groundhogg' );
            break;
      }

      return [
         'label' => $label,
         'data'  => $item_data,
//       'url'  => admin_url( 'admin.php?page=gh_contacts&optin_status=' . $item_key ),
         'color' => $this->get_random_color()
      ];
   }
}<br>
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