MailChimp API PHP - Add to Interest Group
Asked Answered
C

8

16

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?

Carranza answered 6/1, 2012 at 17:8 Comment(0)
C
8

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.

Corpse answered 8/2, 2017 at 15:53 Comment(2)
I'm marking this as accepted since the API has moved on to v3. If you need the solution for v2, scroll down to Trevor Gehman's answer. Thanks for posting this, CrimboCarranza
No problem! There is still no official documentation other than an API guide. So you are left to guess what does whatCorpse
M
21

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
));
Motorist answered 12/9, 2013 at 18:28 Comment(4)
Marking this one as correct so other people landing here see it. Thanks for posting the APIv2 version :)Carranza
Sounds like you're missing a comma in your array elements. I don't see any errors in the above code. Post yours and I can take a look.Motorist
Thanks @TrevorGehman, somehow I missed the whole dynamic of groups being inside groupings, this clarified it and got my subs working.Exhibitor
As the API has moved on to v3, I've unmarked this as correct, for the same reason as I originally accepted it :-) Thanks from everyone you've helped between then and nowCarranza
T
8

I could not get the other answers on this page to work. Here's the merge vars that I had to use:

$merge_vars = array(
    'GROUPINGS' => array(
        0 => array(
            'id' => "101", //You have to find the number via the API
            'groups' => "Interest Name 1, Interest Name 2",
        )
    )
);
Terryn answered 2/11, 2012 at 21:55 Comment(4)
Yes, this works. I had to find out what was meant by 'id' - it's the interest group LIST id. I'll post my code below.Mccann
Same here. The important thing is the array key within groupings. I spent about an hour on this until I found your answer. Sadly, the API didn't throw an exception otherwise.Tit
Note that while you can pass 'name' or 'id' of the grouping list, you must only use the name (just as in the example) for 'groups'. It's unfortunate you cannot pass an ID there instead. :/Dunlap
This is also the answer that worked for me. For me it was specifying the key "0" for the array inside 'GROUPINGS'Terryn
C
8

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.

Corpse answered 8/2, 2017 at 15:53 Comment(2)
I'm marking this as accepted since the API has moved on to v3. If you need the solution for v2, scroll down to Trevor Gehman's answer. Thanks for posting this, CrimboCarranza
No problem! There is still no official documentation other than an API guide. So you are left to guess what does whatCorpse
S
2

Use GROUPINGS merge var:

Set Interest Groups by Grouping. Each element in this array should be an array containing the "groups" parameter which contains a comma delimited list of Interest Groups to add. Commas in Interest Group names should be escaped with a backslash. ie, "," => "\," and either an "id" or "name" parameter to specify the Grouping - get from listInterestGroupings()

Stowe answered 6/1, 2012 at 17:24 Comment(3)
Which method is that available in?Carranza
Maybe this one apidocs.mailchimp.com/api/1.3/listupdatemember.func.php ? ;)Stowe
Sorry, did not checked API for almost a year =)Stowe
M
2

Here's the code I got to work

require_once 'MCAPI.class.php';
require_once 'config.inc.php'; //contains apikey

// use this once to find out id of interest group List
//$retval = $api->listInterestGroupings($listId);
//echo '<pre>';
//print_r($retval);
//echo '</pre>';
//die();

$emailAddress = '[email protected]';
//You have to find the number via the API (id of interest group list)
$interestGroupListId = FILLMEIN;

$api = new MCAPI($apikey);

// Create an array of Interest Groups you want to add the subscriber to.
$mergeVars = array(
    'GROUPINGS' => array(
        0 => array(
            'id' => $interestGroupListId, 
            'groups' => "FILL IN GROUP NAMES",
        )
    )
);

// Then use listUpdateMember to add them
$retval = $api->listUpdateMember($listId, $emailAddress, $mergeVars);

if ($api->errorCode){
    echo "Unable to update member info!\n";
    echo "\tCode=".$api->errorCode."\n";
    echo "\tMsg=".$api->errorMessage."\n";
} else {    
    echo "Returned: ".$retval."\n";
}
Mccann answered 18/6, 2013 at 12:36 Comment(0)
K
2

This is a variation of Justins answer but having tried all the above this is the only one I could get to work with DrewM's MailChimp wrapper

$merge_vars = array(
    'GROUPINGS' => array(
        0 => array(
            'id' => '[GROUP_LIST_ID]', // need grouping ID
            'name' => '[OR_GROUP_LIST_NAME]', // or name instead
            'groups' => array( '[GROUP_NAME_1]', '[GROUP_NAME_2]' )
        )
    ),
);

$mc = new MailChimp('[YOUR_MAILCHIMP_API]');
$mc->call(
        'lists/subscribe', array(
            'id'                => '[YOUR_LIST_ID]', // list ID
            'email'             => array( 'email' => '[email protected]'),
            'merge_vars'        => $merge_vars,
            'double_optin'      => true,
            'update_existing'   => true,
            'replace_interests' => false, // do you want to add or replace?
            'send_welcome'      => false,
        )
    );

If you are unsure of your groups lists ID and wish to use it you can call:

$current_groupings = $mc->call( 'lists/interest-groupings', array(
                'id' => '[GROUP_LIST_ID]',
            ) );
var_dump($current_groupings);
Kassel answered 30/1, 2015 at 12:45 Comment(0)
P
0

Also please note optional but very important last parameter of listUpdateMember, by default replace_interests it set to true so it will overwrite any subscription the user you are updating could have had in the past. If you want to add new ones not touching previous ones just pass the new group name you wanna add and set replace_interests to false.

$api = new MailChimp('API_KEY');
$pArray = array('GROUPINGS'=>array(
                        array('name'=>'GROUP CATEGORY', 'groups'=>'group1,group2'),
                  )
    );
if (is_array($pArray)) {
        $api->listUpdateMember($pListId, $pEmail, $pArray, '', false);
}
Prebend answered 6/12, 2013 at 12:3 Comment(0)
D
0

Example using DrewM's MailChimp wrapper and shorthand syntax for arrays (PHP 5.4+) add to groups:

$merge_vars = [
    'GROUPINGS' => array(
        0 => [
            'id' => 12345, // need grouping ID
            'name' => 'Interests', // or name instead
            'groups' => [ 'cars', 'trucks' ]
        ]
    ]
];

$mc = new MailChimp('API_KEY');
$mc->call(
        'lists/subscribe', [
            'id'                => '9876', // list ID
            'email'             => array[ 'email' => '[email protected]' ],
            'merge_vars'        => $merge_vars,
            'double_optin'      => true,
            'update_existing'   => true,
            'replace_interests' => false, // do you want to add or replace?
            'send_welcome'      => false,
        ]
    );

You need grouping 'name' OR 'id', both are not needed. To get grouping ID use: lists/interest-groupings

Dunlap answered 28/4, 2014 at 18:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.