Sasheem
Sasheem

Reputation: 131

Mailchimp API Node - create campaign for list based on tags

I'm making an async api request with a firebase cloud function to create a campaign within mailchimp for a specific set of users from a list. I read in the documentation that this can be done with tags this way I can build my own structure. I'm building a donation system for a nonprofit and would like the tag to represent the name of a client who is currently being donated to.

Below is my firebase function. I'm stuck at the segment_opts object. I want to define a segment based on whether the list member has a tag equivalent my clients name.

The doc says segment_opts is "An object representing all segmentation options. This object should contain a saved_segment_id to use an existing segment, or you can create a new segment by including both match and conditions options.". I don't have any other segments set up so I figured I'd create a new one that specifies the tags to contain the client's name.

This post helped me get to this point. Stackoverflow post

I now see that condition is supposed to be a Segment Type but in the dropdown I don't see an option for Tags. Here is a link to the documentation reference. Reference

const response = await mailchimp.post('/campaigns', {
    type: 'regular',
    recipients: {
        list_id: functions.config().mailchimp.test,
        segment_opts: {
            "match": "any",
            "conditions": match: 'any',
            conditions: [
                {
                    condition_type: 'StaticSegment',
                    field: 'static_segment',
                    op: 'static_is',
                    value: ??? (Int),
                },
            ],
        }
    },
});

For now I removed segment_opts and will settle on sending campaign to entire list until I figure out how to segment by tags. This version works and creates a campaign within my mailchimp account and from the UI I can see the segment options offered in the documentation but don't see an option to filter by tags

    const response = await mailchimp.post('/campaigns', {
        type: 'regular',
        recipients: {
            list_id: functions.config().mailchimp.test,
        },
        settings: {
            subject_line: `${firstName} has been funded!`,
            preview_text: `$${goal} has been raised for ${firstName}.`,
            title: `${firstName} has been funded`,
            from_name: 'Organization name',
            reply_to: '[email protected]',
        },
    });

Here is a screenshot of the dropdown options in Mailchimp dashboard. Dropdown screenshot

Upvotes: 5

Views: 1388

Answers (1)

blissweb
blissweb

Reputation: 3863

This is what I have for my campaign segment options. Here I'm checking for two conditions. Is the SITE merge tag = the site variable I pass in, and also does the member belong to the tag/segment called tagName. However, I can't pass a tagName, only a tagId which I lookup beforehand.

'segment_opts': 
                {
                    'match': 'all',
                    'conditions': [
                        {
                            'condition_type': 'TextMerge',
                            'field': 'SITE',
                            'op': 'is',
                            'value': site
                        },
                        {
                            'condition_type': 'StaticSegment',
                            'field': 'static_segment',
                            'op': 'static_is',
                            'value': tagId
                        }
                    ]
                }

To get the tagId I use this Python function:

tagId, segments = self.getSegmentIdFromTagName(tagName)

This is the Python code to get the tagId from the tagName, which gets all the Segments/Tags from the system and then looks for the name you pass in:

 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: 6

Related Questions