PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
long li_Loaded
oleobject loo_Http
oleobject loo_Resp
oleobject loo_Ids
oleobject loo_Url
long i
oleobject loo_Report
oleobject loo_Row
string ls_Id
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
destroy loo_Vault
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")
loo_Entry.UpdateString("appName","ConstantContactV3")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","my-account")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")
li_Loaded = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Loaded <> 1 then
Write-Debug "Run the desktop authorization example using this account and store."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
if loo_Tokens.JsonTypeOf("access_token") <> 1 then
Write-Debug "The saved authorization has no access token."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
return
end if
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
loo_Http.RequireSslCertVerify = 1
loo_Http.AuthToken = loo_Tokens.StringOf("access_token")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Ids = create oleobject
li_rc = loo_Ids.ConnectToNewObject("Chilkat.JsonArray")
loo_Ids.AddStringAt(-1,"FIRST_SENT_ACTIVITY_ID")
loo_Ids.AddStringAt(-1,"SECOND_SENT_ACTIVITY_ID")
if (loo_Ids.Size < 1) OR (loo_Ids.Size > 10) then
Write-Debug "Choose one to ten sent activity IDs."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Ids
return
end if
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Url.Append("https://api.cc.email/v3/reports/stats/email_campaign_activities/")
i = 0
do while (i < loo_Ids.Size)
if i > 0 then
loo_Url.Append(",")
end if
loo_Url.Append(loo_Ids.StringAt(i))
i = i + 1
loop
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
if (li_Success <> 1) AND (loo_Resp.StatusCode <= 0) then
Write-Debug "Read statistics: HTTP request failed."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Ids
destroy loo_Url
return
end if
if (li_Success <> 1) OR ((loo_Resp.StatusCode < 200) OR (loo_Resp.StatusCode >= 300)) then
Write-Debug "Read statistics: HTTP " + string(loo_Resp.StatusCode) + " " + loo_Resp.BodyStr
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Ids
destroy loo_Url
return
end if
loo_Report = create oleobject
li_rc = loo_Report.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Report.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
Write-Debug "Expected a JSON object in the response."
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Ids
destroy loo_Url
destroy loo_Report
return
end if
i = 0
do while (i < loo_Report.SizeOfArray("errors"))
loo_Report.I = i
Write-Debug loo_Report.StringOf("errors[i].error_key") + ": " + loo_Report.StringOf("errors[i].error_message")
i = i + 1
loop
Write-Debug "Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh"
loo_Row = create oleobject
li_rc = loo_Row.ConnectToNewObject("Chilkat.JsonObject")
i = 0
do while (i < loo_Ids.Size)
ls_Id = loo_Ids.StringAt(i)
li_Success = loo_Report.FindRecord2("results","campaign_activity_id",ls_Id,1,loo_Row)
if li_Success = 1 then
// StringOf preserves a reported zero and leaves an unavailable individual metric blank.
Write-Debug ls_Id + " | " + loo_Row.StringOf("stats.em_sends") + " | " + loo_Row.StringOf("stats.em_opens") + " | " + loo_Row.StringOf("stats.em_clicks") + " | " + loo_Row.StringOf("stats.em_bounces") + " | " + loo_Row.StringOf("last_refresh_time")
else
Write-Debug ls_Id + " | No statistics returned"
end if
i = i + 1
loop
destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Resp
destroy loo_Ids
destroy loo_Url
destroy loo_Report
destroy loo_Row