Ruby Requires Chilkat v11.0.0+
Ruby
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 Ruby Downloads
require 'chilkat'
success = false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = Chilkat::CkSecrets.new()
vault.put_Location("local_manager")
vault.put_AutoSplit(true)
entry = Chilkat::CkJsonObject.new()
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
tokens = Chilkat::CkJsonObject.new()
loaded = vault.GetSecretJson(entry,tokens)
if (loaded != true)
print "Run the desktop authorization example using this account and store." + "\n";
exit
end
if (tokens.JsonTypeOf("access_token") != 1)
print "The saved authorization has no access token." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
http.put_AuthToken(tokens.stringOf("access_token"))
resp = Chilkat::CkHttpResponse.new()
ids = Chilkat::CkJsonArray.new()
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
end
url = Chilkat::CkStringBuilder.new()
url.Append("https://api.cc.email/v3/reports/stats/email_campaign_activities/")
i = 0
while (i < ids.get_Size())
if (i > 0)
url.Append(",")
end
url.Append(ids.stringAt(i))
i = i + 1
end
success = http.HttpNoBody("GET",url.getAsString(),resp)
if ((success != true) and (resp.get_StatusCode() <= 0))
print "Read statistics: HTTP request failed." + "\n";
exit
end
if ((success != true) or ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300)))
print "Read statistics: HTTP " + resp.get_StatusCode().to_s() + " " + resp.bodyStr() + "\n";
exit
end
report = Chilkat::CkJsonObject.new()
success = report.Load(resp.bodyStr())
if (success != true)
print "Expected a JSON object in the response." + "\n";
exit
end
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
end
print "Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh" + "\n";
row = Chilkat::CkJsonObject.new()
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";
end
i = i + 1
end