7

Using MailChimp API V3.0 to create a campaign.

I want to create a campaign that sends to users with a specific interest. It looks like this is possible in the docs, but I've tried every permutation I can think of. I can create the campaign fine as long as I leave out the segment_ops member. Does anyone have an example of PHP code that will do it?

It seems that interests are handled strangely since you don't include the interest-category when setting a users interests via the API. I'm not sure how this affects campaign creation.

ekad
  • 14,436
  • 26
  • 44
  • 46
Bob Ray
  • 1,105
  • 10
  • 20
  • Seems like a question for your MailChimp representative – Landon Kuhn Mar 04 '16 at 00:43
  • 1
    It's a free MailChimp account with very low volume, so no support. I've found a number of other helpful answers about the MC API here, so hopefully someone who has done this will post a working example. – Bob Ray Mar 04 '16 at 03:30

3 Answers3

20

I've gotten this to work, API definition can be found here https://us1.api.mailchimp.com/schema/3.0/Segments/Merge/InterestSegment.json

Interests have to be grouped under interest categories (called 'Groups' in some parts of the UI).

Here is the JSON for the segment_opts member of the recipients array:

 "segment_opts": {
        "match": "any",
        "conditions": [{
            "condition_type": "Interests",
            "field": "interests-31f7aec0ec",
            "op": "interestcontains",
            "value": ["a9014571b8", "5e824ac953"]
        }]
 }

Here is the PHP array version with comments. The 'match' member refers to the the rules in the array of 'conditions'. The segment can match any, all, or none of the conditions. This example has only one condition, but others can be added as additional arrays in the 'conditions' array:

$segment_opts = array(
'match' => 'any', // or 'all' or 'none'
'conditions' => array (
    array(
        'condition_type' => 'Interests', // note capital I
        'field' => 'interests-31f7aec0ec', // ID of interest category
                                           // This ID is tricky: it is 
                                           // the string "interests-" + 
                                           // the ID of interest category
                                           // that you get from MailChimp 
                                           // API (31f7aec0ec)
        'op' => 'interestcontains', // or interestcontainsall, interestcontainsnone
        'value' => array (
            'a9014571b8',  // ID of interest in that category
            '5e824ac953' // ID of another interest in that category
        )
    )

  )
);
firfin
  • 315
  • 2
  • 8
Bob Ray
  • 1,105
  • 10
  • 20
  • so interests-31f7aec0ec is the id of the category. So does this condition match users who have an interest which is in that category? Could you elaborate on the meaning of interestcontains? --- ultimately, I just want to send an email to all users that have interest with interest_id "X" (I don't really care about categories) – maxfowler May 06 '16 at 21:30
  • interestcontains just tells MG to select users where the user has an interest with one of the IDs contained in the 'value' array – Bob Ray Jul 02 '16 at 18:24
  • <3 thank you! Opaque documentation on this one to be sure. – Ben Holmquist Aug 14 '18 at 14:44
  • 1
    how can i find the category id? – Mikha Matta Oct 15 '18 at 19:52
1

You may also send to a Saved segment. The gotcha on this is that the segment_id has to be int. I was saving this value in a db as varchar, and it would not work unless cast to int.

(i am using use \DrewM\MailChimp\MailChimp;)

$segment_id =  (int) $methodThatGetsMySegmentID;

$campaign = $MailChimp->post("campaigns", [
    'type' => 'regular',
    'recipients' => array(
        'list_id' => 'abc123yourListID',
        'segment_opts' => array(
            'saved_segment_id' => $segment_id,
        ),
    ),
    'settings' => array(
        'subject_line' => 'A New Article was Posted',
        'from_name' => 'From Name',
        'reply_to' => 'info@example.com',
        'title' => 'New Article Notification'
    )
]);
MightyQ
  • 61
  • 5
1

Just wanted to add this because someone asked in the comments of the accepted answer how to get the interest category id and interest id, that you can get those with separate get requests on the API.

To get the interest category id, use:

GET https://[dc].api.mailchimp.com/3.0/lists/[listid]/interest-categories/

[dc] is the district code at the end of your API Key, and [listid] is your audience's list id.

To get the interest id, use:

GET https://[dc].api.mailchimp.com/3.0/lists/[listid]/interest-categories/[interestcategoryid]/interests

[dc] is the district code at the end of your API Key, [listid] is your audience's list id, [interestcategoryid] is the value you got in the previous query above.

I would've posted this in comments on the accepted answet, but my reputation score is just below the threshold to post in comments.

TMax
  • 59
  • 2
  • 10
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34314744) – Wongjn May 06 '23 at 20:00