How can I update subscriber tags using mailchimp3 for Python?

431 Views Asked by At

I am trying to update the tags for members of a MailChimp list using the mailchimp3 package/API on a regular basis using purchase data from an external source. The tags need to be updated weekly based on recent purchases by customers. Sample data is below:

original_contacts = pd.DataFrame{'First Name': {0: 'Michael',
  1: 'James',
  2: 'Josephine',
  3: 'Art',
  4: 'Lenna'},
 'Last Name': {0: 'Ox',
  1: 'Butt',
  2: 'Darakjy',
  3: 'Venere',
  4: 'Paprocki'},
 'Email': {0: '[email protected]',
  1: '[email protected]',
  2: '[email protected]',
  3: '[email protected]',
  4: '[email protected]'},
 'Account Name': {0: 'Lenixi, Co.',
  1: 'Lenixi, Co.',
  2: 'Lenixi, Co.',
  3: 'Fouray, Co.',
  4: 'Fouray, Co.'},
 'Purchase': {0: 'No Purchase',
  1: 'No Purchase',
  2: 'No Purchase',
  3: 'Purchase',
  4: 'No Purchase'}}

I have successfully been able to create new members and update the merge fields of existing members using this script that I modified from this post:

list_id = '123456'
operations = []

for index, row in original_contacts.iterrows():
    databody_item = {'email_address': row['Email'],
                     'status': 'subscribed',
                     'merge_fields': {
                         'FNAME': row['First Name'],
                         'LNAME': row['Last Name'],
                         'COMPANY': row['Account Name'],
                     },
                     'tags': [row['Purchase']]
                    }
    operations.append(databody_item)

def upload_list(list_id, subscribers_data):
    data = {'operations': create_subscriptions_data(list_id, subscribers_data)}
    client.batches.create(data)

def create_subscriptions_data(list_id, users_data):
    return [{'method': 'PUT',
             'path': 'lists/{}/members/{}'.format(list_id, user['email_address']),
             'body': json.dumps(user)} for user in users_data]

upload_list(list_id, operations)

The script above will create a tag for a new member, but does not update tags for existing members. I have also tried variations of the code below to see if I can update the tags for a single member, but have had no success:

client.lists.members.tags.update(list_id = list_id, subscriber_hash='xxxxxxxxxxx', data = {'tags': [{'name': 'Purchase'}]})

This does not throw any errors, but also doesn't do anything to existing tags.

The only solution I have come up with is deleting all existing tags manually and updating every single contact with new tags. However, this is extremely inefficient and I would like to 1) avoid updating tags that haven't changed and 2) want to accomplish everything within my app.

All help is greatly appreciated! Thanks in advance!

2

There are 2 best solutions below

0
Melissa Salazar On

I was able to find all tag-related content here: https://mailchimp.com/developer/marketing/guides/organize-contacts-with-tags/. Just make sure to change the coding language in the code boxes from the default, "BASH" to "PYTHON"

0
Jeff On

You cannot update the tags for a user via a patch. It only works as describes in the documentation when you are adding a new subscriber.

I talked to Mailchimp and suggested they at least make that clearer in the documentation.