public function MailchimpCampaignController::stats in Mailchimp 8
Same name and namespace in other branches
- 2.x modules/mailchimp_campaign/src/Controller/MailchimpCampaignController.php \Drupal\mailchimp_campaign\Controller\MailchimpCampaignController::stats()
View a Mailchimp campaign stats.
Parameters
\Drupal\mailchimp_campaign\Entity\MailchimpCampaign $mailchimp_campaign: The Mailchimp campaign to view stats for.
Return value
array Renderable array of page content.
1 string reference to 'MailchimpCampaignController::stats'
- mailchimp_campaign.routing.yml in modules/
mailchimp_campaign/ mailchimp_campaign.routing.yml - modules/mailchimp_campaign/mailchimp_campaign.routing.yml
File
- modules/
mailchimp_campaign/ src/ Controller/ MailchimpCampaignController.php, line 262
Class
- MailchimpCampaignController
- Mailchimp Campaign controller.
Namespace
Drupal\mailchimp_campaign\ControllerCode
public function stats(MailchimpCampaign $mailchimp_campaign) {
$content = [];
/* @var \Mailchimp\MailchimpReports $mc_reports */
$mc_reports = mailchimp_get_api_object('MailchimpReports');
try {
if (!$mc_reports) {
throw new MailchimpAPIException('Cannot get campaign stats without Mailchimp API. Check API key has been entered.');
}
$response = $mc_reports
->getCampaignSummary($mailchimp_campaign
->getMcCampaignId());
} catch (\Exception $e) {
$this->messenger
->addError($e
->getMessage());
$this->logger
->error('An error occurred getting report data from Mailchimp: {message}', [
'message' => $e
->getMessage(),
]);
}
if (!empty($response)) {
// Attach stats JS.
$content['#attached']['library'][] = 'mailchimp_campaign/google-jsapi';
$content['#attached']['library'][] = 'mailchimp_campaign/campaign-stats';
// Time series chart data.
$content['#attached']['drupalSettings']['mailchimp_campaign'] = [
'stats' => [],
];
foreach ($response->timeseries as $series) {
$content['#attached']['drupalSettings']['mailchimp_campaign']['stats'][] = [
'timestamp' => $series->timestamp,
'emails_sent' => isset($series->emails_sent) ? $series->emails_sent : 0,
'unique_opens' => $series->unique_opens,
'recipients_click' => isset($series->recipients_click) ? $series->recipients_click : 0,
];
}
$content['charts'] = [
'#prefix' => '<h2>' . $this
->t('Hourly stats for the first 24 hours of the campaign') . '</h2>',
'#markup' => '<div id="mailchimp-campaign-chart"></div>',
];
$content['metrics_table'] = [
'#type' => 'table',
'#header' => [
$this
->t('Key'),
$this
->t('Value'),
],
'#empty' => '',
'#prefix' => '<h2>' . $this
->t('Other campaign metrics') . '</h2>',
];
$stat_groups = [
'bounces',
'forwards',
'opens',
'clicks',
'facebook_likes',
'list_stats',
];
foreach ($stat_groups as $group) {
$content['metrics_table'][] = [
'key' => [
'#markup' => '<strong>' . ucfirst(str_replace('_', ' ', $group)) . '</strong>',
],
'value' => [
'#markup' => '',
],
];
foreach ($response->{$group} as $key => $value) {
if ($key == "last_open" && !empty($value)) {
$value = $this->dateFormatter
->format(strtotime($value), 'custom', 'F j, Y - g:ia');
}
$content['metrics_table'][] = [
'key' => [
'#markup' => $key,
],
'value' => [
'#markup' => $value,
],
];
}
}
}
else {
$content['unavailable'] = [
'#markup' => $this
->t('The campaign stats are unavailable at this time.'),
];
}
return $content;
}