[希望通过api将tags添加到我的邮件列表成员中。但是我看不到documentation中的标签传递位置。有人可以指出如何通过api更新与成员关联的标签的示例吗?
标签替换了静态细分。因此,用于创建标记以及从成员中添加和删除标记的端点与以前用于管理细分的端点相同。这是端点上的文档,可用于通过API来管理标签,其中包括请求和响应主体参数以及示例请求和响应:
http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/
为了向成员添加标签,您需要在'static_segment'数组参数中包括其电子邮件地址。
我希望有帮助。
显然,Mailchimp的“标签”是“细分”。
我编写了几个函数,使我可以通过电子邮件地址按名称(而不是ID)向成员(即订户)添加标签。
/**
*
* @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');
}
这依赖于https://github.com/spatie/laravel-newsletter库。
P.S。非常感谢@Jelan,他的回答使我走上了正轨!
这是添加标签的官方方法:https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/它可以工作,除了在我的测试中,即使添加了标签,响应消息也为空。
这里是Google Apps脚本中的示例代码:
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);