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

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkSecrets vault = new CkSecrets();
    vault.put_Location("local_manager");
    vault.put_AutoSplit(true);
    CkJsonObject entry = new CkJsonObject();
    entry.UpdateString("appName","ConstantContactV3");
    entry.UpdateString("service","OAuth2");
    entry.UpdateString("username","my-account");
    CkJsonObject tokens = new CkJsonObject();
    boolean loaded = vault.GetSecretJson(entry,tokens);
    if (loaded != true) {
        Log.i(TAG, "Run the desktop authorization example using this account and store.");
        return;
        }

    if (tokens.JsonTypeOf("access_token") != 1) {
        Log.i(TAG, "The saved authorization has no access token.");
        return;
        }

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

    CkJsonArray ids = new 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)) {
        Log.i(TAG, "Choose one to ten sent activity IDs.");
        return;
        }

    CkStringBuilder url = new CkStringBuilder();
    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) and (resp.get_StatusCode() <= 0)) {
        Log.i(TAG, "Read statistics: HTTP request failed.");
        return;
        }

    if ((success != true) or ((resp.get_StatusCode() < 200) or (resp.get_StatusCode() >= 300))) {
        Log.i(TAG, "Read statistics: HTTP " + String.valueOf(resp.get_StatusCode()) + " " + resp.bodyStr());
        return;
        }

    CkJsonObject report = new CkJsonObject();
    success = report.Load(resp.bodyStr());
    if (success != true) {
        Log.i(TAG, "Expected a JSON object in the response.");
        return;
        }

    i = 0;
    while ((i < report.SizeOfArray("errors"))) {
        report.put_I(i);
        Log.i(TAG, report.stringOf("errors[i].error_key") + ": " + report.stringOf("errors[i].error_message"));
        i = i + 1;
        }

    Log.i(TAG, "Activity ID | Sends | Unique opens | Unique clicks | Bounces | Last refresh");
    CkJsonObject row = new CkJsonObject();
    i = 0;
    while ((i < ids.get_Size())) {
        String 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.
            Log.i(TAG, 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 {
            Log.i(TAG, id + " | No statistics returned");
            }

        i = i + 1;
        }


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}