LoginSignup
0

More than 5 years have passed since last update.

ecwid-mailchimpでListにSubscribersを追加する時にGroupsも指定する方法

Last updated at Posted at 2013-06-17

事前にGroupsを以下のように作っておく。

List Groups for Example   MailChimp.png

Javaコードの中では、GROUPINGSというフィールドで上記Groupsを扱える。

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ecwid.mailchimp.MailChimpClient;
import com.ecwid.mailchimp.MailChimpException;
import com.ecwid.mailchimp.MailChimpObject;
import com.ecwid.mailchimp.method.list.ListSubscribeMethod;

public class Example {

    @SuppressWarnings("serial")
    public static class MergeVars extends MailChimpObject {
        @Field
        public String EMAIL, FNAME, LNAME;

        @Field
        public List<Map<String, Object>> GROUPINGS = new ArrayList<>();

        public MergeVars() {
        }

        public MergeVars(String email, String fname, String lname, String groups) {
            this.EMAIL = email;
            this.FNAME = fname;
            this.LNAME = lname;
            Map<String, Object> group = new HashMap<>();
            group.put("name", "Exmple Groups");
            group.put("groups", groups);
            GROUPINGS.add(group);
        }
    }

    public static void main(String[] args) throws IOException,
            MailChimpException {
        MailChimpClient mailChimpClient = new MailChimpClient();

        ListSubscribeMethod listSubscribeMethod = new ListSubscribeMethod();
        // ここにAPI Keyを入れる。
        listSubscribeMethod.apikey = "****";
        // ここにリストIDを入れる。
        listSubscribeMethod.id = "****";
        // メールアドレスは実在するものを使うこと。このまま使うとエラーになる。
        listSubscribeMethod.email_address = "ryu22e@example.com";
        listSubscribeMethod.double_optin = false;
        listSubscribeMethod.update_existing = true;
        listSubscribeMethod.merge_vars = new MergeVars("ryu22e@example.com",
                "Ryuji", "TSUTSUI", "group1");
        mailChimpClient.execute(listSubscribeMethod);
    }

}

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0