Advertisement
npittet

Compare Campaign Results on CM

Oct 1st, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.46 KB | None | 0 0
  1. <?php
  2. /*
  3. *
  4. *   Compare Campaign Results on CM
  5. *   Created date: 1.10.2014
  6. *   Modified date: 2.10.2014
  7. *   Created by: N.Pittet
  8. *
  9. */
  10.  
  11. // Parameters - please configure it
  12. $debug = "off"; // values: on|off, default is off
  13. $dateformat = "fr"; // values: fr|en, default is en
  14. $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'
  15. $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'
  16. $apikey = ""; // Your account API key
  17. $clientid = ""; // Your clientID
  18. // EOF Parameters
  19.  
  20. // Access & authentification to CM's API
  21. require_once '../csrest_clients.php';
  22. require_once '../csrest_campaigns.php';
  23. $auth = array('api_key' => $apikey);
  24. $wrap1 = new CS_REST_Clients($clientid,$auth);
  25.  
  26. // Check if a date range is set, is correct and get the limits
  27. if($start<>"") $start = convertDateFRtoUK($start,$dateformat);
  28. else $start = "2000-01-01";
  29. if($end<>"") $end = convertDateFRtoUK($end,$dateformat);
  30. else $end = "2020-12-31";
  31. if (!dateControl($start)) { echo "Invalid start date or format date: ".$start; die(); }
  32. if (!dateControl($end)) { echo "Invalid end date or format date: ".$end; die(); }
  33.  
  34. // Get result
  35. $currentcampaign = 0;
  36. $totalcampaign = 0;
  37. $campaigns = $wrap1->get_campaigns();
  38. for($i=0;$i<sizeof($campaigns->response);$i++) {
  39.     if(($campaigns->response[$i]->SentDate>=$start)&&($campaigns->response[$i]->SentDate<=$end)) {
  40.         $date[$currentcampaign] = $campaigns->response[$i]->SentDate;
  41.         $subject[$currentcampaign] = $campaigns->response[$i]->Subject;
  42.         $name[$currentcampaign] = $campaigns->response[$i]->Name;
  43.         $campagneid[$currentcampaign] = $campaigns->response[$i]->CampaignID;
  44.         $wrap2 = new CS_REST_Campaigns($campagneid[$currentcampaign], $auth); // campagneID
  45.         $summary = $wrap2->get_summary();
  46.         $recipients[$currentcampaign] = $summary->response->Recipients;
  47.         $bounced[$currentcampaign] = $summary->response->Bounced;
  48.         $receiver[$currentcampaign] = ($summary->response->Recipients-$summary->response->Bounced);
  49.         $totalopened[$currentcampaign] = $summary->response->TotalOpened;
  50.         $clicks[$currentcampaign] = $summary->response->Clicks;
  51.         $unsubscribed[$currentcampaign] = $summary->response->Unsubscribed;
  52.         $uniqueopened[$currentcampaign] = $summary->response->UniqueOpened;
  53.         $spamcomplaints[$currentcampaign] = $summary->response->SpamComplaints;
  54.         $currentcampaign++;
  55.         $totalcampaign++;
  56.         // Display results detailled if debug is on
  57.         if($debug=="on") {
  58.             echo "<pre>";
  59.             echo "i=>".$i."<br />";
  60.             var_dump($campaigns->response[$i]);
  61.             var_dump($summary->response);
  62.             echo "---------------------------------------";
  63.             echo "</pre>";
  64.         }
  65.     }
  66. }
  67.  
  68. // Sum results    
  69. $sumrecipients = array_sum($recipients);
  70. $sumbounced = array_sum($bounced);
  71. $sumreceiver = array_sum($receiver);
  72. $sumtotalopened = array_sum($totalopened);
  73. $sumclicks = array_sum($clicks);
  74. $sumunsubscribed = array_sum($unsubscribed);
  75. $sumuniqueopened = array_sum($uniqueopened);
  76. $sumspamcomplaints = array_sum($spamcomplaints);
  77. $total = ($i-1);
  78. $totaldays = countDays($date[$totalcampaign-1],$date[0]);
  79.  
  80. // Display results
  81. if($totalcampaign==0) {
  82.     echo "<h2>No campaign found</h2>";
  83.     echo "<p>Start date: ".$start."</p>";
  84.     echo "<p>End date: ".$end."</p>";
  85. }
  86. else {
  87.     echo "<h2>Campaigns overview</h2>";
  88.     echo "<p>Number total of campaigns: ".$i."</p>";
  89.     echo "<p>Number of campaigns analyzed: ".$totalcampaign."</p>";
  90.     echo "<p>First campaign: ".dateFormat($date[$totalcampaign-1])."</p>";
  91.     echo "<p>Last campaign: ".dateFormat($date[0])."</p>";
  92.     echo "<p>Number of days: ".$totaldays."</p>";
  93.     echo "<p>Average campaigns/month: ".round(($totalcampaign)/($totaldays/30),2)."</p>";
  94.     echo "---------------------------------------<br />";
  95.     echo "<h2>Recipients overview</h2>";
  96.     echo "<p>Number of recipients at the beginning: ".$recipients[$totalcampaign-1]."</p>";
  97.     echo "<p>Number of recipients at the end: ".$recipients[1]."</p>";
  98.     echo "<p>Number of recipients's evolution: ".($recipients[1]-$recipients[$totalcampaign-1])."</p>";
  99.     echo "---------------------------------------<br />";
  100.     echo "<h2>Performance overview</h2>";
  101.     echo "<p>Total number of recipients: ".$sumrecipients."</p>";
  102.     echo "<p>Total number of receiver: ".$sumreceiver."</p>";
  103.     echo "<p>Total number of bounces: ".$sumbounced."</p>";
  104.     echo "<p>Total number of unsubscribed: ".$sumunsubscribed."</p>";
  105.     echo "<p>Total number of spam report: ".$sumspamcomplaints."</p>";
  106.     // difference between # sent messages and # bounces in %
  107.     echo "<p>Average deliverability rate: ".round(($sumreceiver/$sumrecipients)*100,2)." % </p>";
  108.     // based on # effective delivered messages. Please note that CM calculating it incorrectly by a sum of percents
  109.     echo "<p>Average open rate: ".round(($sumuniqueopened/$sumreceiver)*100,2)." % </p>";
  110.     // based on # clics by # effective delivered messages. Please note that CM indicate the reactivity rate (see below)
  111.     echo "<p>Average CTR: ".round(($sumclicks/$sumreceiver)*100,2)." % </p>";
  112.     // based on # clics by # opened messages.Please note that CM calculating it incorrectly by a sum of percents
  113.     echo "<p>Average reactivity rate: ".round(($sumclicks/$sumuniqueopened)*100,2)." % </p>";
  114.     echo "---------------------------------------<br />";
  115. }
  116.  
  117. // Functions
  118. function dateFormat($date) { // input date & hour in english format like '2014-12-01 17:00:00'
  119.     if($dateformat=="fr") {
  120.         $array = explode("-", $date);
  121.         $year = $array[2];
  122.         $month = $array[1];
  123.         $day = $array[0];
  124.         return $year."-".$month."-".$day;
  125.     }
  126.     else return strftime('%Y-%m-%d',strtotime($date));
  127. }
  128. function convertDateFRtoUK ($date,$dateformat) { // convert date from FR format to english format
  129.     if($dateformat=="fr") {
  130.         $array = explode("-", $date);
  131.         $year = $array[2];
  132.         $month = $array[1];
  133.         $day = $array[0];
  134.         $date = $year."-".$month."-".$day;
  135.     }
  136.     return $date;
  137. }
  138. function dateControl($date) { // input date in english format like '2014-12-01'
  139.     $array = explode("-", $date);
  140.     $year = $array[0];
  141.     $month = ltrim($array[1],'0');
  142.     $day = ltrim($array[2],'0');
  143.     return checkdate($month, $day, $year);
  144. }
  145. function countDays($datestart,$dateend) { // input date in english format like '2014-12-01'
  146.     $seconds= 60*60*24;
  147.     $tsdatestart = strtotime($datestart);
  148.     $tsdateend = strtotime($dateend);
  149.     $diff = $tsdateend - $tsdatestart;
  150.     return round($diff/$seconds);
  151. }
  152. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement