Skip to content

Commit dde5f8a

Browse files
Add Tags for new members, fix route
1 parent 6a5f7a9 commit dde5f8a

4 files changed

Lines changed: 48 additions & 3 deletions

File tree

app/Http/Controllers/RestApi/RestController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function handleGet(string $secret)
4545
}
4646

4747
/**
48-
* Add a new contact to Mailchimp using CRM data
48+
* Add a new contact to Mailchimp from the website (using CRM-mapping)
4949
*
5050
* @param Request $request
5151
* @param string $secret

app/Http/MailChimpClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,30 @@ public function putSubscriber(array $mcData, string $email = null, string $id =
293293

294294
return $put;
295295
}
296+
297+
/**
298+
* Add or update tags for a subscriber
299+
*
300+
* @param string $subscriberId The MailChimp subscriber ID
301+
* @param array $tags Array of tag names (strings) or tag objects with 'name' and 'status'
302+
*
303+
* @throws MailchimpClientException
304+
*/
305+
public function addTagsToSubscriber(string $subscriberId, array $tags)
306+
{
307+
if (empty($tags)) {
308+
return;
309+
}
310+
311+
$formattedTags = [];
312+
foreach ($tags as $tag) {
313+
if (is_string($tag)) {
314+
$formattedTags[] = (object)['name' => $tag, 'status' => 'active'];
315+
}
316+
}
317+
318+
$this->postSubscriberTags($subscriberId, ['tags' => $formattedTags]);
319+
}
296320

297321
/**
298322
* Update tags of subscriber

app/Synchronizer/Config.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,24 @@ public function getMailchimpListId(): string
175175
if (empty($this->mailchimp['listId'])) {
176176
throw new ConfigException("Missing mailchimp list id.");
177177
}
178-
178+
179179
return $this->mailchimp['listId'];
180180
}
181-
181+
182+
/**
183+
* Return the tag that should be added to new subscribers
184+
*
185+
* @return string
186+
*/
187+
public function getNewTag(): string
188+
{
189+
if (empty($this->mailchimp['newtag'])) {
190+
return 'new';
191+
}
192+
193+
return $this->mailchimp['newtag'];
194+
}
195+
182196
/**
183197
* The mailchimp merge field key that corresponds to the crm's id
184198
*

app/Synchronizer/WebsiteToMailchimpSynchronizer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public function syncSingle(array $websiteData)
101101
// Add the subscriber to Mailchimp
102102
$response = $this->mailchimpClient->putSubscriber($mailchimpData);
103103

104+
// Add tags to new subscriber
105+
$tags = [$this->config->getNewTag()];
106+
if (isset($websiteData['notesCountry'])) {
107+
$tags[] = $websiteData['notesCountry'];
108+
}
109+
$this->mailchimpClient->addTagsToSubscriber($response['id'], $tags);
110+
104111
return $response;
105112
}
106113

0 commit comments

Comments
 (0)