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