phpmailchimpsegmentmailchimp-api-v3.0

Create campaign with dynamic segment using MailChimp API V3.0


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.


Solution

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