CkPhp Requires Chilkat v11.0.0+
CkPhp
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 CkPhp Downloads
<?php
include("chilkat.php");
$success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = new CkSecrets();
$vault->put_Location('local_manager');
$vault->put_AutoSplit(true);
$entry = new CkJsonObject();
$entry->UpdateString('appName','ConstantContactV3');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','my-account');
$tokens = new CkJsonObject();
$loaded = $vault->GetSecretJson($entry,$tokens);
if ($loaded != true) {
print 'Run the desktop authorization example using this account and store.' . "\n";
exit;
}
if ($tokens->JsonTypeOf('access_token') != 1) {
print 'The saved authorization has no access token.' . "\n";
exit;
}
$http = new CkHttp();
$http->put_RequireSslCertVerify(true);
$http->put_AuthToken($tokens->stringOf('access_token'));
$resp = new CkHttpResponse();
$ids = new CkJsonArray();
$ids->AddStringAt(-1,'FIRST_SENT_ACTIVITY_ID');
$ids->AddStringAt(-1,'SECOND_SENT_ACTIVITY_ID');
if (($ids->get_Size() < 1) or ($ids->get_Size() > 10)) {
print 'Choose one to ten sent activity IDs.' . "\n";
exit;
}
$url = new CkStringBuilder();
$url->Append('https://api.cc.email/v3/reports/stats/email_campaign_activities/');
$i = 0;
while (($i < $ids->get_Size())) {
if ($i > 0) {
$url->Append(',');
}
$url->Append($ids->stringAt($i));
$i = $i + 1;
}
$success = $http->HttpNoBody('GET',$url->getAsString(),$resp);
if (($success != true) and ($resp->get_StatusCode() <= 0)) {
print 'Read statistics: HTTP request failed.' . "\n";
exit;
}
if (($success != true) or (($resp->get_StatusCode() < 200) or ($resp->get_StatusCode() >= 300))) {
print 'Read statistics: HTTP ' . $resp->get_StatusCode() . ' ' . $resp->bodyStr() . "\n";
exit;
}
$report = new CkJsonObject();
$success = $report->Load($resp->bodyStr());
if ($success != true) {
print 'Expected a JSON object in the response.' . "\n";
exit;
}
$i = 0;
while (($i < $report->SizeOfArray('errors'))) {
$report->put_I($i);
print $report->stringOf('errors[i].error_key') . ': ' . $report->stringOf('errors[i].error_message') . "\n";
$i = $i + 1;
}
print 'Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh' . "\n";
$row = new CkJsonObject();
$i = 0;
while (($i < $ids->get_Size())) {
$id = $ids->stringAt($i);
$success = $report->FindRecord2('results','campaign_activity_id',$id,true,$row);
if ($success == true) {
// StringOf preserves a reported zero and leaves an unavailable individual metric blank.
print $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";
}
else {
print $id . ' | No statistics returned' . "\n";
}
$i = $i + 1;
}
?>