PHP Requires Chilkat v11.0.0+
PHP
Constant Contact V3: Compare results for sent email activities
See more Constant Contact Examples
Compare one to ten sent primary_email or resend activities with status DONE. Supply activity IDs from your sent newsletters. Campaign IDs identify a different resource. This report requires campaign_data and permission to view campaign metrics.
Each row shows sends, unique opens, unique clicks, bounces and the last metrics refresh. The code matches results by activity ID, keeps the input order, and prints any partial-success errors alongside the available results. A missing result is reported separately from a measured zero.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat PHP Downloads
<?php
use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonArray;
use Chilkat2\JsonObject;
use Chilkat2\Secrets;
use Chilkat2\StringBuilder;
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = new Secrets();
$vault->Location = 'local_manager';
$vault->AutoSplit = true;
$entry = new JsonObject();
$entry->UpdateString('appName', 'ConstantContactV3');
$entry->UpdateString('service', 'OAuth2');
$entry->UpdateString('username', 'my-account');
$tokens = new JsonObject();
try {
$vault->GetSecretJson($entry, $tokens);
} catch (\Chilkat2\Exception $e) {
echo "Run the desktop authorization example using this account and store.\n";
exit;
}
if ($tokens->JsonTypeOf('access_token') != 1) {
echo "The saved authorization has no access token.\n";
exit;
}
$http = new Http();
$http->RequireSslCertVerify = true;
$http->AuthToken = $tokens->StringOf('access_token');
$resp = new HttpResponse();
$ids = new JsonArray();
$ids->AddStringAt(-1, 'FIRST_SENT_ACTIVITY_ID');
$ids->AddStringAt(-1, 'SECOND_SENT_ACTIVITY_ID');
if (($ids->Size < 1) || ($ids->Size > 10)) {
echo "Choose one to ten sent activity IDs.\n";
exit;
}
$url = new StringBuilder();
$url->Append('https://api.cc.email/v3/reports/stats/email_campaign_activities/');
$i = 0;
while ($i < $ids->Size) {
if ($i > 0) {
$url->Append(',');
}
$url->Append($ids->StringAt($i));
$i = $i + 1;
}
try {
$http->HttpNoBody('GET', $url->GetAsString(), $resp);
$success = true;
} catch (\Chilkat2\Exception $e) {
$success = false;
}
if ((!$success) && ($resp->StatusCode <= 0)) {
echo "Read statistics: HTTP request failed.\n";
exit;
}
if ((!$success) || (($resp->StatusCode < 200) || ($resp->StatusCode >= 300))) {
echo 'Read statistics: HTTP ', $resp->StatusCode, ' ', $resp->BodyStr, "\n";
exit;
}
$report = new JsonObject();
try {
$report->Load($resp->BodyStr);
} catch (\Chilkat2\Exception $e) {
echo "Expected a JSON object in the response.\n";
exit;
}
$i = 0;
while ($i < $report->SizeOfArray('errors')) {
$report->I = $i;
echo $report->StringOf('errors[i].error_key'), ': ', $report->StringOf('errors[i].error_message'), "\n";
$i = $i + 1;
}
echo "Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh\n";
$row = new JsonObject();
$i = 0;
while ($i < $ids->Size) {
$id = $ids->StringAt($i);
try {
$report->FindRecord2('results', 'campaign_activity_id', $id, true, $row);
// StringOf preserves a reported zero and leaves an unavailable individual metric blank.
echo $id, ' | ', $row->StringOf('stats.em_sends'), ' | ', $row->StringOf('stats.em_opens'), ' | ', $row->StringOf('stats.em_clicks'), ' | ', $row->StringOf('stats.em_bounces'), ' | ', $row->StringOf('last_refresh_time'), "\n";
} catch (\Chilkat2\Exception $e) {
echo $id, " | No statistics returned\n";
}
$i = $i + 1;
}