/** * Gets all data needed for this page */ private function getData() { // get parameters $id = $this->getParameter('mailing_id', 'int'); // does the item exist if (!BackendMailmotorModel::existsMailing($id)) { $this->redirect(BackendModel::createURLForAction('index') . '&error=mailing-does-not-exist'); } // fetch the mailing $this->mailing = BackendMailmotorModel::getMailing($id); // fetch the bounces $this->bounces = BackendMailmotorCMHelper::getBounces($this->mailing['id']); // does the item exist if (empty($this->bounces)) { $this->redirect(BackendModel::createURLForAction('statistics') . '&id=' . $this->mailing['id'] . '&error=no-bounces'); } }
/** * Returns the statistics for a given mailing * * @param int $id The id of the mailing. * @param bool[optional] $fetchClicks If the click-count should be included. * @param bool[optional] $fetchOpens If the open-count should be included. * @return array */ public static function getStatistics($id, $fetchClicks = false, $fetchOpens = false) { // check if the mailing exists if (!BackendMailmotorModel::existsMailing($id)) { throw new SpoonException('No mailing found for id ' . $id); } // fetch cmID $cmId = self::getCampaignMonitorID('campaign', $id); // fetch the CM ID if ($cmId) { // fetch the statistics $stats = self::getCM()->getCampaignSummary($cmId); // stop here if no recipients were found if ($stats['recipients'] == 0) { return false; } // reset the bounces to match the real ones $bounces = BackendMailmotorCMHelper::getBounces($id); // re-calculate base stats to match CM's $stats['bounces'] = count($bounces); $stats['recipients'] = $stats['recipients']; $stats['recipients_total'] = $stats['recipients']; $stats['unopens'] = $stats['recipients'] - $stats['unique_opens'] - $stats['bounces']; $stats['clicks_total'] = 0; // add percentages to these stats $stats['bounces_percentage'] = $stats['recipients'] == 0 ? 0 : floor($stats['bounces'] / $stats['recipients_total'] * 100) . '%'; $stats['recipients_percentage'] = $stats['recipients'] == 0 ? 0 : ceil($stats['recipients'] / $stats['recipients_total'] * 100) . '%'; $stats['unique_opens_percentage'] = $stats['recipients'] == 0 ? 0 : ceil($stats['unique_opens'] / $stats['recipients'] * 100) . '%'; $stats['unopens_percentage'] = $stats['recipients'] == 0 ? 0 : floor($stats['unopens'] / $stats['recipients'] * 100) . '%'; $stats['clicks_percentage'] = $stats['recipients'] == 0 ? 0 : ceil($stats['clicks'] / $stats['recipients'] * 100) . '%'; // fetch clicks or not? if ($fetchClicks) { // get detailed click reports $subscriberClicks = self::getCM()->getCampaignClicks($cmId); // links have been clicked if (!empty($subscriberClicks)) { // declare array $stats['clicked_links'] = array(); $stats['clicked_links_by'] = $subscriberClicks; // filter out the clicked links foreach ($subscriberClicks as $link => $clickers) { // count the clickers $clickerCount = count($clickers); $stats['clicked_links'][] = array('link' => $link, 'clicks' => $clickerCount); $stats['clicks_total'] += $clickerCount; } /* // re-loop so we can fix the keys foreach($stats['clicked_links'] as $link) { // store the link data $stats['clicked_links'][] = array('link' => urlencode($link['link'])); // unset the record with the link as key unset($stats['clicked_links'][$link['link']]); } // re-loop so we can fix the keys foreach($stats['clicked_links_by'] as $link => $clicks) { // loop the clicks foreach($clicks as $click) { // store the link data $stats['clicked_links_by'][$link][] = array('email' => $click['email']); // unset the record with the link as key unset($stats['clicked_links_by'][$link][$click['email']]); } } */ } } // fetch opened stats or not? if ($fetchOpens) { // fetch opens $stats['opens'] = self::getMailingOpens($cmId); } // return the results return $stats; } // at this point, return false return false; }