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

C++
#include <CkSecrets.h>
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkHttpResponse.h>
#include <CkJsonArray.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecrets vault;
    vault.put_Location("local_manager");
    vault.put_AutoSplit(true);
    CkJsonObject entry;
    entry.UpdateString("appName","ConstantContactV3");
    entry.UpdateString("service","OAuth2");
    entry.UpdateString("username","my-account");
    CkJsonObject tokens;
    bool loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        std::cout << "Run the desktop authorization example using this account and store." << "\r\n";
        return;
    }

    if (tokens.JsonTypeOf("access_token") != 1) {
        std::cout << "The saved authorization has no access token." << "\r\n";
        return;
    }

    CkHttp http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(tokens.stringOf("access_token"));
    CkHttpResponse resp;

    CkJsonArray ids;
    ids.AddStringAt(-1,"FIRST_SENT_ACTIVITY_ID");
    ids.AddStringAt(-1,"SECOND_SENT_ACTIVITY_ID");
    if ((ids.get_Size() < 1) || (ids.get_Size() > 10)) {
        std::cout << "Choose one to ten sent activity IDs." << "\r\n";
        return;
    }

    CkStringBuilder url;
    url.Append("https://api.cc.email/v3/reports/stats/email_campaign_activities/");
    int 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) && (resp.get_StatusCode() <= 0)) {
        std::cout << "Read statistics: HTTP request failed." << "\r\n";
        return;
    }

    if ((success != true) || ((resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300))) {
        std::cout << "Read statistics: HTTP " << resp.get_StatusCode() << " " << resp.bodyStr() << "\r\n";
        return;
    }

    CkJsonObject report;
    success = report.Load(resp.bodyStr());
    if (success != true) {
        std::cout << "Expected a JSON object in the response." << "\r\n";
        return;
    }

    i = 0;
    while ((i < report.SizeOfArray("errors"))) {
        report.put_I(i);
        std::cout << report.stringOf("errors[i].error_key") << ": " << report.stringOf("errors[i].error_message") << "\r\n";
        i = i + 1;
    }

    std::cout << "Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh" << "\r\n";
    CkJsonObject row;
    i = 0;
    while ((i < ids.get_Size())) {
        const char *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.
            std::cout << 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") << "\r\n";
        }
        else {
            std::cout << id << " | No statistics returned" << "\r\n";
        }

        i = i + 1;
    }
    }