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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonArray;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        Secrets vault = new Secrets();
        vault.setLocation("local_manager");
        vault.setAutoSplit(true);
        JsonObject entry = new JsonObject();
        entry.updateString("appName", "ConstantContactV3");
        entry.updateString("service", "OAuth2");
        entry.updateString("username", "my-account");
        JsonObject tokens = new JsonObject();
        try {
            vault.getSecretJson(entry, tokens);
        } catch (ChilkatException e) {
            System.out.println("Run the desktop authorization example using this account and store.");
            return;
        }

        if (tokens.jsonTypeOf("access_token") != 1) {
            System.out.println("The saved authorization has no access token.");
            return;
        }

        Http http = new Http();
        http.setRequireSslCertVerify(true);
        http.setAuthToken(tokens.stringOf("access_token"));
        HttpResponse resp = new HttpResponse();

        JsonArray ids = new JsonArray();
        ids.addStringAt(-1, "FIRST_SENT_ACTIVITY_ID");
        ids.addStringAt(-1, "SECOND_SENT_ACTIVITY_ID");
        if ((ids.getSize() < 1) || (ids.getSize() > 10)) {
            System.out.println("Choose one to ten sent activity IDs.");
            return;
        }

        StringBuilder url = new StringBuilder();
        url.append("https://api.cc.email/v3/reports/stats/email_campaign_activities/");
        int i = 0;
        while (i < ids.getSize()) {
            if (i > 0) {
                url.append(",");
            }

            url.append(ids.stringAt(i));
            i = i + 1;
        }

        try {
            http.httpNoBody("GET", url.getAsString(), resp);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if ((!success) && (resp.getStatusCode() <= 0)) {
            System.out.println("Read statistics: HTTP request failed.");
            return;
        }

        if ((!success) || ((resp.getStatusCode() < 200) || (resp.getStatusCode() >= 300))) {
            System.out.println("Read statistics: HTTP " + resp.getStatusCode() + " " + resp.getBodyStr());
            return;
        }

        JsonObject report = new JsonObject();
        try {
            report.load(resp.getBodyStr());
        } catch (ChilkatException e) {
            System.out.println("Expected a JSON object in the response.");
            return;
        }

        i = 0;
        while (i < report.sizeOfArray("errors")) {
            report.setI(i);
            System.out.println(report.stringOf("errors[i].error_key") + ": " + report.stringOf("errors[i].error_message"));
            i = i + 1;
        }

        System.out.println("Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh");
        JsonObject row = new JsonObject();
        i = 0;
        while (i < ids.getSize()) {
            String id = ids.stringAt(i);
            try {
                report.findRecord2("results", "campaign_activity_id", id, true, row);
                // StringOf preserves a reported zero and leaves an unavailable individual metric blank.
                System.out.println(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"));
            } catch (ChilkatException e) {
                System.out.println(id + " | No statistics returned");
            }
            i = i + 1;
        }
    }
}