Kay Galinsky
Kay Galinsky

Reputation: 21

mailchimp3 (Python-mailchimp-api) How to set tags

I tried:

    client.lists.members.create('1111111', {
        'email_address' : '[email protected]',
        'status'        : 'subscribed',
        "tags": [{'name': 'frogger', 'status' : 'active'}],  
    })

and get:

mailchimp3.mailchimpclient.MailChimpError: {
    'type': 'http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/',
    'title': 'Invalid Resource', 
    'status': 400, 
    'detail': 'Expected argument of type "string", "stdClass" given',
    'instance': '5ae1b966-ed35-49f1-ad32-0a39c3d63593'
}

Without the "tags" line the satement works

Upvotes: 2

Views: 1196

Answers (2)

blissweb
blissweb

Reputation: 3873

I think in the latest version the tags are just strings. At least, this is what I use.

    client.lists.members.create('1111111', {
        'email_address' : '[email protected]',
        'status'        : 'subscribed',
        'tags': ['frogger','invaders']  
    })

Also, to be certain, make sure tags exist in the system. Especially if doing an update rather than insert. Its not super consistant about dealing with unknown tags.

Also of course, make sure the '1111111' audience id exists.

SUPPLEMENTAL CODE

Of course, I've found a better way to ensure what you want is to use other methods to add to tags rather than on insert. You may have to tweak the below functions as they are part of a class, but they should give you other ideas on how to add and remove tags from users

    def bulkUpdateTagNamesForEmails(self, emailsToUpdate, tagNames, remove=False):

        audienceId = self.audienceId

        if remove:
            data = {'members_to_remove':emailsToUpdate}
        else:
            data = {'members_to_add':emailsToUpdate}

        segments = None
        for tagName in tagNames:
            segId,segments = self.getSegmentIdFromTagName(tagName,segments)
            if segId:
                self.brResponse = self.mcClient.lists.segments.update_members(list_id=audienceId, segment_id=segId, data=data)


    def createTagNameIfNeeded(self, tagName):

        audienceId = self.audienceId

        # Check for tag name
        found = False
        segments = self.mcClient.lists.segments.all(list_id=audienceId, get_all=False) 
        for segment in segments['segments']:
            if segment['name'] == tagName:
                found = True
                print("Found tag")

        # If not found, create it
        if not found:
            print(f"Creating new tag {tagName}")
            data = {'name': tagName,'static_segment': []}
            self.mcClient.lists.segments.create(list_id=audienceId, data=data)


    def getSegmentIdFromTagName(self,reqTagName,segments=None):
        audienceId = self.audienceId
        reqId = None
        if not segments:
            segments =     self.mcClient.lists.segments.all(list_id=audienceId, get_all=True)
        for segment in segments['segments']:
            segName = segment['name']
            segId = segment['id']
            if segName == reqTagName:
                reqId = segId
                break
        return reqId,segments

Upvotes: 1

Allen
Allen

Reputation: 722

This works for me (you need hash the email address)

hash = get_subscriber_hash(email_address) 

data = {
        'tags': [
            {'name': 'foo', 'status': 'active'},
            {'name': 'bar', 'status': 'inactive'}
        ]
}

client.lists.members.tags.update(list_id=list_id, subscriber_hash=hash, data=data)

Upvotes: 2

Related Questions