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

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
local loaded = vault:get_secret_json(entry, tokens)
if not loaded then
    print("Run the desktop authorization example using this account and store.")
    return
end

if tokens:json_type_of("access_token") ~= 1 then
    print("The saved authorization has no access token.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.auth_token = tokens:string_of("access_token")
local resp = chilkat.HttpResponse.new()

local ids = chilkat.JsonArray.new()
ids:add_string_at(-1, "FIRST_SENT_ACTIVITY_ID")
ids:add_string_at(-1, "SECOND_SENT_ACTIVITY_ID")
if (ids.size < 1) or (ids.size > 10) then
    print("Choose one to ten sent activity IDs.")
    return
end

local url = chilkat.StringBuilder.new()
url:append("https://api.cc.email/v3/reports/stats/email_campaign_activities/")
local i = 0
while i < ids.size do
    if i > 0 then
        url:append(",")
    end

    url:append(ids:string_at(i))
    i = i + 1
end

local success = http:http_no_body("GET", url:get_as_string(), resp)
if (not success) and (resp.status_code <= 0) then
    print("Read statistics: HTTP request failed.")
    return
end

if (not success) or ((resp.status_code < 200) or (resp.status_code >= 300)) then
    print("Read statistics: HTTP " .. resp.status_code .. " " .. resp.body_str)
    return
end

local report = chilkat.JsonObject.new()
success = report:load(resp.body_str)
if not success then
    print("Expected a JSON object in the response.")
    return
end

i = 0
while i < report:size_of_array("errors") do
    report.i = i
    print(report:string_of("errors[i].error_key") .. ": " .. report:string_of("errors[i].error_message"))
    i = i + 1
end

print("Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh")
local row = chilkat.JsonObject.new()
i = 0
while i < ids.size do
    local id = ids:string_at(i)
    success = report:find_record2("results", "campaign_activity_id", id, true, row)
    if success then
        -- StringOf preserves a reported zero and leaves an unavailable individual metric blank.
        print(id .. " | " .. row:string_of("stats.em_sends") .. " | " .. row:string_of("stats.em_opens") .. " | " .. row:string_of("stats.em_clicks") .. " | " .. row:string_of("stats.em_bounces") .. " | " .. row:string_of("last_refresh_time"))
    else
        print(id .. " | No statistics returned")
    end

    i = i + 1
end