Table of Contents

Managing audiences

Audiences (Mailchimp's term for subscriber lists) are the containers your members and campaigns belong to. TTMSFNCCloudMailChimpMarketing keeps them in its Lists collection and exposes asynchronous operations to read, create, update, and delete them. Each audience carries a required contact address and a set of campaign defaults. This chapter covers listing and creating audiences and editing or removing them.

Listing and creating audiences

Call GetLists(ACount, AIncludeTotalContacts) to load audiences; the result arrives in OnGetLists (and the Lists collection is populated). Create one by building a TTMSFNCCloudMailChimpSubscriberList, filling its ListName, PermissionReminder, the Contact address sub-object, and the CampaignDefaults (from-name, from-email, subject, language), then calling AddList. Mailchimp requires a real physical Contact address for compliance, so populate it.

procedure TForm1.MailChimpGetLists(Sender: TObject;
  ALists: TTMSFNCCloudMailChimpSubscriberLists);
var
  List: TTMSFNCCloudMailChimpSubscriberList;
begin
  ListsListBox.Clear;
  for List in ALists do
    ListsListBox.Items.AddObject(List.ListName, List);
end;

procedure TForm1.CreateAudience;
var
  L: TTMSFNCCloudMailChimpSubscriberList;
begin
  L := TTMSFNCCloudMailChimpSubscriberList.Create;
  L.ListName := 'Newsletter subscribers';
  L.PermissionReminder := 'You signed up on our website.';
  // A required physical contact address for compliance.
  L.Contact.Company := 'Contoso';
  L.Contact.Address1 := '1 Main St';
  L.Contact.City := 'Brussels';
  L.Contact.Zip := '1000';
  L.Contact.Country := 'BE';
  // Defaults applied to campaigns created for this audience.
  L.CampaignDefaults.FromName := 'Contoso News';
  L.CampaignDefaults.FromEmail := 'news@contoso.com';
  L.CampaignDefaults.Subject := 'Contoso update';
  L.CampaignDefaults.Language := 'en';
  FMailChimp.AddList(L); // result arrives in OnAddList
end;

procedure TForm1.RemoveSelectedAudience;
begin
  if ListsListBox.ItemIndex >= 0 then
    FMailChimp.DeleteList(
      TTMSFNCCloudMailChimpSubscriberList(ListsListBox.Items.Objects[ListsListBox.ItemIndex]));
end;

Updating and deleting audiences

To edit an audience, change the fields on the item from the Lists collection (which keeps its Id) and call UpdateList; the change is reported through OnUpdateList. Remove one with DeleteList, confirmed by OnDeleteList. Refresh your UI in those events rather than immediately after the call, and re-GetLists to reflect a deletion.

Pitfalls

  • Provide a complete Contact address. Mailchimp rejects an audience without the required physical address fields.
  • Edit the collection item for updates. Modify the existing TTMSFNCCloudMailChimpSubscriberList (which has the server Id) and pass it to UpdateList; a freshly built instance has no identity to update.
  • Watch OnError. A malformed audience surfaces there, not in OnAddList.

See also