Create campaign with dynamic segment using MailChimp API V3.0

南楼画角 提交于 2019-11-29 11:19:24
Bob Ray

I've gotten this to work, though there's no way you'd get it from the current docs, which don't list an 'Interests' condition_type or the possible 'op' values for it.

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
        )
    )

  )
);

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'
    )
]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!