Create campaign with dynamic segment using MailChimp API V3.0

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 06:49:14

问题


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.


回答1:


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

  )
);



回答2:


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'
    )
]);


来源:https://stackoverflow.com/questions/35785278/create-campaign-with-dynamic-segment-using-mailchimp-api-v3-0

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