CkPython Requires Chilkat v11.0.0+
CkPython
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 CkPython Downloads
import sys
import chilkat
success = False
# Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = chilkat.CkSecrets()
vault.put_Location("local_manager")
vault.put_AutoSplit(True)
entry = chilkat.CkJsonObject()
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
tokens = chilkat.CkJsonObject()
loaded = vault.GetSecretJson(entry,tokens)
if (loaded != True):
print("Run the desktop authorization example using this account and store.")
sys.exit()
if (tokens.JsonTypeOf("access_token") != 1):
print("The saved authorization has no access token.")
sys.exit()
http = chilkat.CkHttp()
http.put_RequireSslCertVerify(True)
http.put_AuthToken(tokens.stringOf("access_token"))
resp = chilkat.CkHttpResponse()
ids = chilkat.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.")
sys.exit()
url = chilkat.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.")
sys.exit()
if ((success != True) or ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))):
print("Read statistics: HTTP " + str(resp.get_StatusCode()) + " " + resp.bodyStr())
sys.exit()
report = chilkat.CkJsonObject()
success = report.Load(resp.bodyStr())
if (success != True):
print("Expected a JSON object in the response.")
sys.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"))
i = i + 1
print("Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh")
row = chilkat.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"))
else:
print(id + " | No statistics returned")
i = i + 1