Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.0.0+

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 Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
loaded: Boolean;
http: HCkHttp;
resp: HCkHttpResponse;
ids: HCkJsonArray;
url: HCkStringBuilder;
i: Integer;
report: HCkJsonObject;
row: HCkJsonObject;
id: PWideChar;

begin
success := False;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
vault := CkSecrets_Create();
CkSecrets_putLocation(vault,'local_manager');
CkSecrets_putAutoSplit(vault,True);
entry := CkJsonObject_Create();
CkJsonObject_UpdateString(entry,'appName','ConstantContactV3');
CkJsonObject_UpdateString(entry,'service','OAuth2');
CkJsonObject_UpdateString(entry,'username','my-account');
tokens := CkJsonObject_Create();
loaded := CkSecrets_GetSecretJson(vault,entry,tokens);
if (loaded <> True) then
  begin
    Memo1.Lines.Add('Run the desktop authorization example using this account and store.');
    Exit;
  end;
if (CkJsonObject_JsonTypeOf(tokens,'access_token') <> 1) then
  begin
    Memo1.Lines.Add('The saved authorization has no access token.');
    Exit;
  end;
http := CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,True);
CkHttp_putAuthToken(http,CkJsonObject__stringOf(tokens,'access_token'));
resp := CkHttpResponse_Create();

ids := CkJsonArray_Create();
CkJsonArray_AddStringAt(ids,-1,'FIRST_SENT_ACTIVITY_ID');
CkJsonArray_AddStringAt(ids,-1,'SECOND_SENT_ACTIVITY_ID');
if ((CkJsonArray_getSize(ids) < 1) or (CkJsonArray_getSize(ids) > 10)) then
  begin
    Memo1.Lines.Add('Choose one to ten sent activity IDs.');
    Exit;
  end;
url := CkStringBuilder_Create();
CkStringBuilder_Append(url,'https://api.cc.email/v3/reports/stats/email_campaign_activities/');
i := 0;
while (i < CkJsonArray_getSize(ids)) do
  begin
    if (i > 0) then
      begin
        CkStringBuilder_Append(url,',');
      end;
    CkStringBuilder_Append(url,CkJsonArray__stringAt(ids,i));
    i := i + 1;
  end;

success := CkHttp_HttpNoBody(http,'GET',CkStringBuilder__getAsString(url),resp);
if ((success <> True) and (CkHttpResponse_getStatusCode(resp) <= 0)) then
  begin
    Memo1.Lines.Add('Read statistics: HTTP request failed.');
    Exit;
  end;
if ((success <> True) or ((CkHttpResponse_getStatusCode(resp) < 200) or (CkHttpResponse_getStatusCode(resp) >= 300))) then
  begin
    Memo1.Lines.Add('Read statistics: HTTP ' + IntToStr(CkHttpResponse_getStatusCode(resp)) + ' ' + CkHttpResponse__bodyStr(resp));
    Exit;
  end;
report := CkJsonObject_Create();
success := CkJsonObject_Load(report,CkHttpResponse__bodyStr(resp));
if (success <> True) then
  begin
    Memo1.Lines.Add('Expected a JSON object in the response.');
    Exit;
  end;
i := 0;
while (i < CkJsonObject_SizeOfArray(report,'errors')) do
  begin
    CkJsonObject_putI(report,i);
    Memo1.Lines.Add(CkJsonObject__stringOf(report,'errors[i].error_key') + ': ' + CkJsonObject__stringOf(report,'errors[i].error_message'));
    i := i + 1;
  end;

Memo1.Lines.Add('Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh');
row := CkJsonObject_Create();
i := 0;
while (i < CkJsonArray_getSize(ids)) do
  begin
    id := CkJsonArray__stringAt(ids,i);
    success := CkJsonObject_FindRecord2(report,'results','campaign_activity_id',id,True,row);
    if (success = True) then
      begin
        //  StringOf preserves a reported zero and leaves an unavailable individual metric blank.
        Memo1.Lines.Add(id + ' | ' + CkJsonObject__stringOf(row,'stats.em_sends') + ' | ' + CkJsonObject__stringOf(row,'stats.em_opens') + ' | ' + CkJsonObject__stringOf(row,'stats.em_clicks')
             + ' | ' + CkJsonObject__stringOf(row,'stats.em_bounces') + ' | ' + CkJsonObject__stringOf(row,'last_refresh_time'));
      end
    else
      begin
        Memo1.Lines.Add(id + ' | No statistics returned');
      end;
    i := i + 1;
  end;

CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonArray_Dispose(ids);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(report);
CkJsonObject_Dispose(row);