How to add "Tags" to mailchimp subscriber via the api

Apparently Mailchimp "tags" are "segments".

I coded a couple functions that allow me to add tags by name (rather than by ID) to a member (i.e. subscriber) by email address.

/**
 * 
 * @param string $emailAddress
 * @param array $tags
 * @return void
 */
public function addTagsToContact($emailAddress, $tags) {
    $list_id = $this->getDefaultListId();
    foreach ($tags as $tag) {
        $this->addMemberToSegment($emailAddress, $list_id, $tag);
    }
}

/**
 * Add a tag to a subscriber (tags replaced segments https://stackoverflow.com/a/52315577/470749)
 * 
 * @param string $emailAddress
 * @param string $list_id
 * @param string $segment_name
 * @return array
 */
public function addMemberToSegment($emailAddress, $list_id, $segment_name) {
    $api = Newsletter::getApi();
    $segmentsByName = $this->getSegments($list_id);
    $segment_id = $segmentsByName[$segment_name]['id'];
    $response = $api->post("lists/$list_id/segments/$segment_id", [
        'members_to_add' => [$emailAddress]
    ]); //https://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#create-post_lists_list_id_segments_segment_id
    return $response;
}

/**
 * 
 * @param string $list_id
 * @return array
 */
public function getSegments($list_id) {//https://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#%20
    $segmentsByName = [];
    $api = Newsletter::getApi();
    $count = 50; //default is 10
    $offset = 0;
    do {
        $url = "lists/$list_id/segments/?" . http_build_query(['count' => $count, 'offset' => $offset]);
        Log::debug($url);
        $response = $api->get($url);
        $total_items = $response['total_items'];
        foreach ($response['segments'] as $segment) {
            $segmentsByName[$segment['name']] = $segment;
        }
        $offset += $count;
    } while (count($segmentsByName) < $total_items);
    //Log::debug(json_encode($segmentsByName));
    return $segmentsByName;
}

/**
 * 
 * @return string
 */
public function getDefaultListId() {
    return config('newsletter.lists.subscribers.id');
}

This relies on the https://github.com/spatie/laravel-newsletter library.

P.S. Thanks so much to @Jelan, whose answer got me on the right track!


If you want to create a member AND add a tag while doing so you may specify the tag attribute the following way:

$data = array(
              'apikey'        => $api_key,
              'email_address' => $email,
              'status'     => $status,
              'tags'  => array('your-tag-name'),
              'merge_fields'  => array(
                    'FNAME' => $fname,
                    'LNAME' => $lname
                  )
            );

Even though MC API some places will tell you to fill out both a name and a status, it helped me to define tags as an array but ONLY pasting in the name of the tag.

Seefan's answer in this thread helped me out and I figured i wanted to help a person who spend days (like me) to figure out how the "tags" is specified: add tags to mailchimp subscriber created via api php


This is the official way to add tags: https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/ It works, except that in my testing, the response message is empty, even though the tag is added.

Here's sample code in Google Apps Script:

payload = '{\
  "tags": [\
    {\
     "name":"' + tagName + '",\
     "status":"' + tagStatus + '"\
    }\
   ]\
}'
;

params = {
  "method": "POST",
  "headers":MC_headers,
  "payload": payload,
  "muteHttpExceptions": true
};
url = MC_url + 'lists/' + MC_IDs.listId + '/members/' + sub_hash + '/tags';
response = UrlFetchApp.fetch(url, params);

Tags replaced static segments. So, the endpoints used to create tags and add and remove tags from members are the same endpoints that were previously used to manage segments. Here is the documentation on the endpoints to use to manage your tags via the API that includes the request and response body parameters as well as example requests and responses:

http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/

In order to add tags to your members, you need to include their email addresses in the 'static_segment' array parameter.

I hope that helps.