Lance
Lance

Reputation: 161

Mailchimp add tags to existing member in a list with php and drupal 8

I would like to add a tag to a member in a mailchimp list in Drupal 8 after adding the member to the list. I use PHP.

Based upon the info I found at: https://mailchimp.com/developer/guides/organize-contacts-with-tags/#label-a-contact-with-a-tag

Below code is working:

$response_tags = $mailchimp->lists->getListMemberTags($list_id, $subscriber_hash);
\Drupal::logger('mailchimp_get_tags')->notice('<pre><code>' . print_r($response_tags, TRUE) . '</code></pre>');


stdClass Object
(
    [tags] => Array
        (
            [0] => stdClass Object
                (
                    [id] => ****
                    [name] => 1_year
                    [date_added] => 2020-09-30T14:09:29+00:00
                )

        )

    [total_items] => 1
)

But below code is giving 400 bad request error ...

$mailchimp->lists->updateListMemberTags($list_id, $subscriber_hash, [
   "tags" => [
     "name" => "my_new_tag",
     "status" => "active"
   ]
]);

Error:

GuzzleHttp\Exception\ClientException: Client error: POST https://***.api.mailchimp.com/3.0/lists/***/members/***/tags resulted in a 400 Bad Request response: {"type":" http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/ ","title":"Invalid Resource","stat (truncated...) in GuzzleHttp\Exception\RequestException::create() (line 113 of /home/d8/drupal-project/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php).

=> https://mailchimp.com/developer/guides/marketing-api-conventions/#error-glossary

The list_id is correct, the hash is correct but adding a tag to an existing member in the mailchimp list is not working.

Upvotes: 3

Views: 1612

Answers (1)

Lance
Lance

Reputation: 161

The documentation is not very clear. "name" and "status" must be an array. That makes sense ... but it is not well documented.

$mailchimp->lists->updateListMemberTags($list_id, $subscriber_hash, [
   "tags" => [
     ["name" => "my_new_tag", "status" => "active"]
   ]
]);

Upvotes: 3

Related Questions