You are here

public function Api::getStats in SendGrid Integration 8.2

Same name and namespace in other branches
  1. 8 modules/sendgrid_integration_reports/src/Api.php \Drupal\sendgrid_integration_reports\Api::getStats()

Returns stats.

Parameters

string $cid: Cache Id.

array $categories: Array of categories.

string|null $start_date: Start date.

string|null $end_date: End date.

bool $refresh: Flag is cache should be refreshed.

string $subuser: Optional subuser to report on.

Return value

array|bool Array of stats data.

File

modules/sendgrid_integration_reports/src/Api.php, line 187

Class

Api
Class SendGridReportsController.

Namespace

Drupal\sendgrid_integration_reports

Code

public function getStats($cid, array $categories = [], $start_date = NULL, $end_date = NULL, $refresh = FALSE, $subuser = '') {
  if (!$refresh && ($cache = $this->cacheFactory
    ->get($this->bin)
    ->get($cid))) {
    return $cache->data;
  }

  // Load key from variables and throw errors if not there.
  if (empty($this->apiKey)) {
    return [];
  }

  // Get config.
  $config = $this->configFactory
    ->get('sendgrid_integration_reports.settings')
    ->get();
  if ($start_date) {
    $start_date = date('Y-m-d', strtotime($start_date));
  }
  else {

    // Set start date and end date for global stats - default 30 days back.
    $start_date = empty($config['start_date']) ? date('Y-m-d', strtotime('today - 30 days')) : $config['start_date'];
  }
  if ($end_date) {
    $end_date = date('Y-m-d', strtotime($end_date));
  }
  else {

    // Set the end date which defaults to today.
    $end_date = empty($config['end_date']) ? date('Y-m-d', strtotime('today')) : $config['end_date'];
  }

  // Set aggregation of stats - default day.
  $aggregated_by = isset($config['aggregated_by']) ? $config['aggregated_by'] : 'day';
  $path = 'stats';
  $query = [
    'start_date' => $start_date,
    'end_date' => $end_date,
    'aggregated_by' => $aggregated_by,
  ];
  if ($categories) {
    $path = 'categories/stats';
    $query['categories'] = $categories;
    $query_str = http_build_query($query, NULL, '&', PHP_QUERY_RFC3986);
    $query = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $query_str);
  }

  // Lets attempt the request and catch an error if it fails.
  $stats_data = $this
    ->getResponse($path, $query, $subuser);
  if (!$stats_data) {
    return [];
  }
  $data = [];
  foreach ($stats_data as $item) {
    $data['global'][] = [
      'date' => $item->date,
      'opens' => $item->stats[0]->metrics->opens,
      'processed' => $item->stats[0]->metrics->processed,
      'requests' => $item->stats[0]->metrics->requests,
      'clicks' => $item->stats[0]->metrics->clicks,
      'delivered' => $item->stats[0]->metrics->delivered,
      'deferred' => $item->stats[0]->metrics->deferred,
      'unsubscribes' => $item->stats[0]->metrics->unsubscribes,
      'unsubscribe_drops' => $item->stats[0]->metrics->unsubscribe_drops,
      'invalid_emails' => $item->stats[0]->metrics->invalid_emails,
      'bounces' => $item->stats[0]->metrics->bounces,
      'bounce_drops' => $item->stats[0]->metrics->bounce_drops,
      'unique_clicks' => $item->stats[0]->metrics->unique_clicks,
      'blocks' => $item->stats[0]->metrics->blocks,
      'spam_report_drops' => $item->stats[0]->metrics->spam_report_drops,
      'spam_reports' => $item->stats[0]->metrics->spam_reports,
      'unique_opens' => $item->stats[0]->metrics->unique_opens,
    ];
  }

  // Save data to cache.
  $this
    ->setCache($cid, $data);
  return $data;
}