Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MailChimp API PHP - Add to Interest Group

I'm currently using the MailChimp API for PHP, version 1.3.1 (http://apidocs.mailchimp.com/api/downloads/#php)

I've set up a list in MailChimp, and would like to dynamically add:

  1. Subscribers to the list (done: $objMailChimp->listBatchSubscribe($strMailingListID, ...))
  2. Interest Groupings (done: $objMailChimp->listInterestGroupingAdd($strMailingListID, ...))
  3. Interest Groups into those Groupings (done: $objMailChimp->listInterestGroupAdd($strMailingListID, ...))
  4. Subscribers assigned to relevant Groups (not done)

The API (http://apidocs.mailchimp.com/api/1.3/#listrelated) is somewhat unclear on how to add a subscriber to an interest group - does anyone here have any ideas?

like image 212
Joe Avatar asked Jan 06 '12 17:01

Joe


2 Answers

As of version 2.0 of MailChimp's API, this should work:

$merge_vars = array(
    'GROUPINGS' => array(
        array(
            'name' => "GROUP CATEGORY #1", // You can use either 'name' or 'id' to identify the group
            'groups' => array("GROUP NAME","GROUP NAME")
        ),
        array(
            'name' => "GROUP CATEGORY #2",
            'groups' => array("GROUP NAME")
        )
    )
);

Source: http://apidocs.mailchimp.com/api/2.0/lists/subscribe.php

Using a barebones PHP wrapper (https://github.com/drewm/mailchimp-api/) you can then send this to MailChimp via either the lists/subscribe or lists/batch-subscribe:

$MailChimp = new MailChimp('API_KEY');
$result = $MailChimp->call('lists/subscribe', array(
      'id'                => 'LIST ID',
      'email'             => array('email'=>'[email protected]'),
      'merge_vars'        => $merge_vars
));
like image 62
Trevor Gehman Avatar answered Sep 19 '22 13:09

Trevor Gehman


For MailChimp API v3

As of v3, 'groupings' has changed to 'interests'.

You have to find out the ID of the group (interest) that you are wanting to add to. Unfortunately this cannot be found anywhere on the MailChimp dashboard.

The easiest way to find out the 'interest' ID (rather than creating a script) is to go to the MailChimp playground and then, after entering in your API key, route to...

lists > the list in question > interest-categories (in the sub-resources dropdown)

then...

interests (in the sub-resources dropdown) for interest category

then...

Click through to the interest and refer to the 'id' field, ignoring the other ID fields

OR

lists > the list in question > members (in the sub-resources dropdown)

then...

load (in the actions dropdown) for any member

or

Create Members (button)

The page will load the member's details. Scroll down until you see the 'interests' array/object. There you will see the IDs. Notice they can be set to true or false.

You will have to figure out which ID relates to what 'group'/'interest' by going about the previous method or making the call, and then looking at the member's details via your MailChimp dashboard.


So when it comes to actually making the POST call ('member' create), you would want something on the lines of...

{
  "email_address":"[email protected]",
  "status":"subscribed",
  "interests": {
    "b8a9d7cbf6": true,
    "5998e44916": false
  },
# ADDITIONAL FIELDS, IF REQUIRED...
  "merge_fields":{
    "FNAME": "foo bar",
    "LNAME": "foo bar",
    "MERGE3": "foo bar",
    "MERGE4": "foo bar"
  }
}

A PUT call ('member' edit) example...

{
  "interests": {
    "b8a9d7cbf6": false,
    "5998e44916": true
  }
}

It seems that you must declare every 'interest', and state whether it is true or false.

like image 36
Crimbo Avatar answered Sep 19 '22 13:09

Crimbo