Sample code for 30+ languages & platforms
Lazarus Pascal 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 Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.JsonArray,
  Chilkat.HttpResponse,
  Chilkat.StringBuilder,
  Chilkat.JsonObject,
  Chilkat.Secrets,
  Chilkat.Http;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  vault: TSecrets;
  entry: TJsonObject;
  tokens: TJsonObject;
  loaded: Boolean;
  http: THttp;
  resp: THttpResponse;
  ids: TJsonArray;
  url: TStringBuilder;
  i: Integer;
  report: TJsonObject;
  row: TJsonObject;
  id: string;

begin
  success := False;

  //  Assumes Chilkat is unlocked. See Global Unlock Sample.
  vault := TSecrets.Create;
  vault.Location := 'local_manager';
  vault.AutoSplit := True;
  entry := TJsonObject.Create;
  entry.UpdateString('appName','ConstantContactV3');
  entry.UpdateString('service','OAuth2');
  entry.UpdateString('username','my-account');
  tokens := TJsonObject.Create;
  loaded := vault.GetSecretJson(entry,tokens);
  if (loaded <> True) then
    begin
      WriteLn('Run the desktop authorization example using this account and store.');
      Exit;
    end;
  if (tokens.JsonTypeOf('access_token') <> 1) then
    begin
      WriteLn('The saved authorization has no access token.');
      Exit;
    end;
  http := THttp.Create;
  http.RequireSslCertVerify := True;
  http.AuthToken := tokens.StringOf('access_token');
  resp := THttpResponse.Create;

  ids := TJsonArray.Create;
  ids.AddStringAt(-1,'FIRST_SENT_ACTIVITY_ID');
  ids.AddStringAt(-1,'SECOND_SENT_ACTIVITY_ID');
  if ((ids.Size < 1) or (ids.Size > 10)) then
    begin
      WriteLn('Choose one to ten sent activity IDs.');
      Exit;
    end;
  url := TStringBuilder.Create;
  url.Append('https://api.cc.email/v3/reports/stats/email_campaign_activities/');
  i := 0;
  while (i < ids.Size) do
    begin
      if (i > 0) then
        begin
          url.Append(',');
        end;
      url.Append(ids.StringAt(i));
      i := i + 1;
    end;

  success := http.HttpNoBody('GET',url.GetAsString(),resp);
  if ((success <> True) and (resp.StatusCode <= 0)) then
    begin
      WriteLn('Read statistics: HTTP request failed.');
      Exit;
    end;
  if ((success <> True) or ((resp.StatusCode < 200) or (resp.StatusCode >= 300))) then
    begin
      WriteLn('Read statistics: HTTP ' + resp.StatusCode + ' ' + resp.BodyStr);
      Exit;
    end;
  report := TJsonObject.Create;
  success := report.Load(resp.BodyStr);
  if (success <> True) then
    begin
      WriteLn('Expected a JSON object in the response.');
      Exit;
    end;
  i := 0;
  while (i < report.SizeOfArray('errors')) do
    begin
      report.I := i;
      WriteLn(report.StringOf('errors[i].error_key') + ': ' + report.StringOf('errors[i].error_message'));
      i := i + 1;
    end;

  WriteLn('Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh');
  row := TJsonObject.Create;
  i := 0;
  while (i < ids.Size) do
    begin
      id := ids.StringAt(i);
      success := report.FindRecord2('results','campaign_activity_id',id,True,row);
      if (success = True) then
        begin
          //  StringOf preserves a reported zero and leaves an unavailable individual metric blank.
          WriteLn(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'));
        end
      else
        begin
          WriteLn(id + ' | No statistics returned');
        end;
      i := i + 1;
    end;



  vault.Free;
  entry.Free;
  tokens.Free;
  http.Free;
  resp.Free;
  ids.Free;
  url.Free;
  report.Free;
  row.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.