Python for Entrepreneurs Transcripts
Chapter: Building your email list
Lecture: Concept: The MailChimp API

Login or purchase this course to watch this video and the rest of the course contents.
0:03 So what does it take to integrate with MailChimp? Well, you saw after we created our account we just have to pip install MailChimp
0:10 and we got that from PyPi, that was awesome, and then we had to figure out a way to get our secrets, our API key and our list id,
0:19 API key is really important to keep secret, list id not so much. You'll see that you'd have to actually put that in some of the forms
0:27 that you put on your page if you take some of the form code I think, but the API key, you want to make this is not shared, so be careful with it.
0:35 Here you saw we put it into our project.ini file, we have the development.ini and the production.ini
0:41 and then we're going to use the config.get_settings to pull that out by key so here config.get_settings gets that settings dictionary right there
0:49 and then we just get the value from the dictionary, it's just a Python dictionary by name,
0:54 so we store the API key and the mailing list id and then we pass that on to our app, we had crated that mailing list service class
1:00 that we just stored it globally as a static field there. Now we want to add subscribers, of course, we get the email address,
1:08 if you can get their first name and last name that's great, if you don't have it, you can just put the email address,
1:13 and we're going to use MailChimp's class, so mailchimp the package.mailchimp the class and we create that by calling initializer with the API key
1:23 and it gets us the API, then we just say api.lists.subscibe and it takes the list id,
1:28 the email address as a dictionary, funky, double_optin True or False, that's up to you,
1:34 you've got to decide for yourself, and so update_existing=True, replace_interests=False. Now, why do you want to do this False?
1:41 Well, we can look at segmentation, just for a minute. If we want to actually put people into different groups,
1:48 maybe they are coming in as an unqualified lead, and then once they have bought an album,
1:53 we could both indicate both which album they bought as well as that they are a purchaser of an album and once they have attended the live event,
2:01 we could put them in yet another group and we could go to MailChimp and say "show me all the people who have come to a live event,
2:06 show me all the people who have bought one of our albums. I want to mail everybody who bought this album,
2:13 and tell them there is now a new bonus track that they could download." So you would use segmentation and groups for that,
2:17 so here is the code you've got to write for that, it's not super obvious but it's not hard either.
2:21 So you've got to go to MailChimp, and actually go to the list and go to the interest groups then create a group and then within there
2:29 you have to create little subgroups called interest and groupings and once you do that, you can take those and subscribe people to it,
2:36 so suppose I have a group or an interest named Talk Python groups and one of the subgroups is called Training,
2:46 so there will be all the people who took my classes, right, as opposed to people who signed up through the podcast
2:51 or some other random source that they may be signed up. So, here would be Training and if I want to indicate put on this list
2:59 and flag them or segment them to know that they came from the training angle, not the podcast angle, then I would do something like this,
3:05 I would create this group and this subgroup and then piece in code and then I would use the merge_vars with groupings set to that value
3:13 and both lower case and uppercase because sometimes it varies, that's kind of funky but you know, you do what works sometimes
3:20 when you are working with APIs right? And it's in this case where replace_interests may be important to be False,
3:27 if you want to have them additionally in say like the PyGrass group and the Training group if you knew that they came from both locations,
3:34 right, did you say replace_interest is False.


Talk Python's Mastodon Michael Kennedy's Mastodon