Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- *
- * Compare Campaign Results on CM
- * Created date: 1.10.2014
- * Modified date: 2.10.2014
- * Created by: N.Pittet
- *
- */
- // Parameters - please configure it
- $debug = "off"; // values: on|off, default is off
- $dateformat = "fr"; // values: fr|en, default is en
- $start = "01-03-2014"; // date in your $dateformat eg '2014-12-01' in en or '01-12-2014' in fr, optional, limits the analysis to a date range, default is "" => '2000-01-01'
- $end = "30-03-2014"; // date in your $dateformat eg '2014-12-01' in en or '01-12-2014' in fr, optional, limits the analysis to a date range, default is "" => '2020-12-31'
- $apikey = ""; // Your account API key
- $clientid = ""; // Your clientID
- // EOF Parameters
- // Access & authentification to CM's API
- require_once '../csrest_clients.php';
- require_once '../csrest_campaigns.php';
- $auth = array('api_key' => $apikey);
- $wrap1 = new CS_REST_Clients($clientid,$auth);
- // Check if a date range is set, is correct and get the limits
- if($start<>"") $start = convertDateFRtoUK($start,$dateformat);
- else $start = "2000-01-01";
- if($end<>"") $end = convertDateFRtoUK($end,$dateformat);
- else $end = "2020-12-31";
- if (!dateControl($start)) { echo "Invalid start date or format date: ".$start; die(); }
- if (!dateControl($end)) { echo "Invalid end date or format date: ".$end; die(); }
- // Get result
- $currentcampaign = 0;
- $totalcampaign = 0;
- $campaigns = $wrap1->get_campaigns();
- for($i=0;$i<sizeof($campaigns->response);$i++) {
- if(($campaigns->response[$i]->SentDate>=$start)&&($campaigns->response[$i]->SentDate<=$end)) {
- $date[$currentcampaign] = $campaigns->response[$i]->SentDate;
- $subject[$currentcampaign] = $campaigns->response[$i]->Subject;
- $name[$currentcampaign] = $campaigns->response[$i]->Name;
- $campagneid[$currentcampaign] = $campaigns->response[$i]->CampaignID;
- $wrap2 = new CS_REST_Campaigns($campagneid[$currentcampaign], $auth); // campagneID
- $summary = $wrap2->get_summary();
- $recipients[$currentcampaign] = $summary->response->Recipients;
- $bounced[$currentcampaign] = $summary->response->Bounced;
- $receiver[$currentcampaign] = ($summary->response->Recipients-$summary->response->Bounced);
- $totalopened[$currentcampaign] = $summary->response->TotalOpened;
- $clicks[$currentcampaign] = $summary->response->Clicks;
- $unsubscribed[$currentcampaign] = $summary->response->Unsubscribed;
- $uniqueopened[$currentcampaign] = $summary->response->UniqueOpened;
- $spamcomplaints[$currentcampaign] = $summary->response->SpamComplaints;
- $currentcampaign++;
- $totalcampaign++;
- // Display results detailled if debug is on
- if($debug=="on") {
- echo "<pre>";
- echo "i=>".$i."<br />";
- var_dump($campaigns->response[$i]);
- var_dump($summary->response);
- echo "---------------------------------------";
- echo "</pre>";
- }
- }
- }
- // Sum results
- $sumrecipients = array_sum($recipients);
- $sumbounced = array_sum($bounced);
- $sumreceiver = array_sum($receiver);
- $sumtotalopened = array_sum($totalopened);
- $sumclicks = array_sum($clicks);
- $sumunsubscribed = array_sum($unsubscribed);
- $sumuniqueopened = array_sum($uniqueopened);
- $sumspamcomplaints = array_sum($spamcomplaints);
- $total = ($i-1);
- $totaldays = countDays($date[$totalcampaign-1],$date[0]);
- // Display results
- if($totalcampaign==0) {
- echo "<h2>No campaign found</h2>";
- echo "<p>Start date: ".$start."</p>";
- echo "<p>End date: ".$end."</p>";
- }
- else {
- echo "<h2>Campaigns overview</h2>";
- echo "<p>Number total of campaigns: ".$i."</p>";
- echo "<p>Number of campaigns analyzed: ".$totalcampaign."</p>";
- echo "<p>First campaign: ".dateFormat($date[$totalcampaign-1])."</p>";
- echo "<p>Last campaign: ".dateFormat($date[0])."</p>";
- echo "<p>Number of days: ".$totaldays."</p>";
- echo "<p>Average campaigns/month: ".round(($totalcampaign)/($totaldays/30),2)."</p>";
- echo "---------------------------------------<br />";
- echo "<h2>Recipients overview</h2>";
- echo "<p>Number of recipients at the beginning: ".$recipients[$totalcampaign-1]."</p>";
- echo "<p>Number of recipients at the end: ".$recipients[1]."</p>";
- echo "<p>Number of recipients's evolution: ".($recipients[1]-$recipients[$totalcampaign-1])."</p>";
- echo "---------------------------------------<br />";
- echo "<h2>Performance overview</h2>";
- echo "<p>Total number of recipients: ".$sumrecipients."</p>";
- echo "<p>Total number of receiver: ".$sumreceiver."</p>";
- echo "<p>Total number of bounces: ".$sumbounced."</p>";
- echo "<p>Total number of unsubscribed: ".$sumunsubscribed."</p>";
- echo "<p>Total number of spam report: ".$sumspamcomplaints."</p>";
- // difference between # sent messages and # bounces in %
- echo "<p>Average deliverability rate: ".round(($sumreceiver/$sumrecipients)*100,2)." % </p>";
- // based on # effective delivered messages. Please note that CM calculating it incorrectly by a sum of percents
- echo "<p>Average open rate: ".round(($sumuniqueopened/$sumreceiver)*100,2)." % </p>";
- // based on # clics by # effective delivered messages. Please note that CM indicate the reactivity rate (see below)
- echo "<p>Average CTR: ".round(($sumclicks/$sumreceiver)*100,2)." % </p>";
- // based on # clics by # opened messages.Please note that CM calculating it incorrectly by a sum of percents
- echo "<p>Average reactivity rate: ".round(($sumclicks/$sumuniqueopened)*100,2)." % </p>";
- echo "---------------------------------------<br />";
- }
- // Functions
- function dateFormat($date) { // input date & hour in english format like '2014-12-01 17:00:00'
- if($dateformat=="fr") {
- $array = explode("-", $date);
- $year = $array[2];
- $month = $array[1];
- $day = $array[0];
- return $year."-".$month."-".$day;
- }
- else return strftime('%Y-%m-%d',strtotime($date));
- }
- function convertDateFRtoUK ($date,$dateformat) { // convert date from FR format to english format
- if($dateformat=="fr") {
- $array = explode("-", $date);
- $year = $array[2];
- $month = $array[1];
- $day = $array[0];
- $date = $year."-".$month."-".$day;
- }
- return $date;
- }
- function dateControl($date) { // input date in english format like '2014-12-01'
- $array = explode("-", $date);
- $year = $array[0];
- $month = ltrim($array[1],'0');
- $day = ltrim($array[2],'0');
- return checkdate($month, $day, $year);
- }
- function countDays($datestart,$dateend) { // input date in english format like '2014-12-01'
- $seconds= 60*60*24;
- $tsdatestart = strtotime($datestart);
- $tsdateend = strtotime($dateend);
- $diff = $tsdateend - $tsdatestart;
- return round($diff/$seconds);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement