Python for Entrepreneurs Transcripts
Chapter: Building your email list
Lecture: Using the MailChimp API
0:03 Alright, now we are ready, we've got our mail list service,
0:07 ready to manage subscribers, adding, removing, updating subscribers,
0:10 all those kinds of things, we've got our forms and our controller,
0:14 a GET / POST / Redirect pattern ready to do this gathers the data from the users,
0:18 all that is left is to actually call the MailChimp API itself.
0:27 So it probably doesn't surprise you to know that
0:29 MailChimp is available right here on PyPi.
0:34 So all we've got to do is "pip install mailchimp",
0:36 now, there is a couple of ways we can do this,
0:38 remember, we're using this virtual environment,
0:41 yours probably is different but this is the one I am using for this demo
0:44 so I need to make sure whatever I do I do it here,
0:46 now we could come up here, this is pretty sleek,
0:48 I could just import MailChimp like this, now notice,
0:52 MailChimp has a little red thing saying there is no module named MailChimp,
0:56 but PyCharm is smart and it knows that this is a package on PyPi
0:59 and we could install it, so one way to install it is
1:02 just hit Enter and let PyCharm do it here;
1:04 another one is we could actually go to the interpreter and say
1:07 I want to add MailChimp and it will go find it, here, so it's just to show you,
1:13 the third way you can install this of course is to go over here
1:16 and activate that virtual environment, so source or ". activate", the path to it,
1:23 and then we could just "pip install mailchimp", it's going to download, install it,
1:30 boom, everything is ready to go,
1:33 one thing that you need to be careful of with MailChimp
1:36 is this docopt requirement, the current version with docopt is 0.6.3,
1:41 but this won't run with 6.3 this only runs with 0.4.0
1:46 so you have to pin that version here for docopt, which is really frustrating
1:49 but whatever, that's the way it works.
1:52 OK, so we have this installed, we need to put this as a requirement in our setup.py
1:55 so when we install this into our server,
1:59 it's automatically going to bring along the dependencies MailChimp, docopt and so on,
2:03 after all that, we are ready to create this, call this API here,
2:07 so the way it works is we say mailchimp.Mailchimp like so,
2:11 and what we need to pass is you can see is the API key,
2:14 so this is going to be MailingListService.mailchimp_api maybe we should call that key,
2:19 I don't know, whatever, that is what we're going to use,
2:21 and then it's just literally one function call, so maybe we want to do some tests,
2:26 we'll say "if not email... return false", we don't really want to take an empty email,
2:31 now, the MailChimp API throws exceptions and we maybe don't want to throw
2:38 a straight up exception, so we could just catch them and return True or False,
2:41 so let's do this, we'll say "api", now in here there is a lot of things, folders, lists,
2:47 so what we care about are lists, and on the list, we can do things like batch subscribe,
2:53 or get the clients, we'll look at growth history but what we just want to do is subscribe
2:57 and there is a whole bunch of options, I'll show you what will work well,
3:01 so first of all, we're going to pass in the list id, like so, so that goes in,
3:08 remember that came out of our ini file or configuration file, through the dunder init,
3:13 back over to here; next, we have to pass the email and we pass this as a dictionary,
3:19 which is kind of funky, but it's OK, it'll work,
3:22 so we are going to have our email,
3:26 and we probably want to do things like strip it, so if there is any spaces,
3:30 maybe lower-case it, so it's sort of canonically just the lower case,
3:34 no white space variant here, the next thing is merge_vars,
3:37 we are not going to use merge_vars here, these play important roles
3:40 for things like first name, last name, they allow you to do segmentation,
3:46 so like paying customers versus free customers, and all sorts of stuff like that,
3:51 we'll talk about that when we get to the concept section,
3:54 but we're going to set two other things here,
3:56 we're going to set double_optin to be in false,
3:58 so what double_optin is and you can decide what you put here,
4:02 MailChimp, if I say True, will actually send and email to the email address entered saying
4:07 "hey it looks like you want to subscribe to this mailing list,
4:10 click this button to confirm your subscription there."
4:13 I am going to assume people coming to my site are qualified leads,
4:16 maybe you want to set that to be True, the default is True, and so on.
4:20 So we'll say update_existing, this is pretty important, if you are doing this multiple times,
4:25 or different levels, especially in the segmentation stuff,
4:27 so if this is already here and sort of failing to subscribe, I just want to update them,
4:32 and replace_interests is false.
4:35 So, interest this doesn't matter so much here but I want to put it here anyway
4:39 so when you get to the next section, if you go farther with MailChimp
4:43 that you don't accidentally leave this out, so what is this about,
4:46 this has to do with interest groups, and you can use interest groups for segmentation,
4:50 so like I said, paying customers, people who bought this course,
4:53 people who came from here and there, if you say True there, the default,
4:56 it will only allow them to be in one group, if you say False,
5:00 you will be able to build them up so a customer can be both
5:03 the paying customer and originating from a web cast and interested in data science,
5:07 or whatever it is like your groups are consisting of.
5:12 OK, so we've got one little squiggly thing here,
5:14 what is this unhappy about? The missing except,
5:18 OK, so if that worked, we are going to return True, if it doesn't work,
5:22 let's catch an exception, do a print and later we could log this to somewhere
5:27 like roll bar or something, but we'll just catch it, like so,
5:30 and we'll print "Error during mailing",
5:34 so here we can print that out and then let's return False.
5:38 OK, so I think we're ready, what do you guys think?
5:41 Do you want to try this? do you want to sign up to our email list?
5:43 We could email ourselves, that sounds cool, let's run it.
5:46 OK, here we are again, it's running, it didn't crash,
5:51 that's good that means that we've probably setup MailChimp alright,
5:54 so I'll come down I'll put my email address in again,
5:57 and let's get notified, actually let me use my other one,
6:00 the one I used to register at MailChimp, so, I'll use my
6:03 firstname.lastname@example.org, are we ready?
6:06 Go! Awesome, you're on the list, OK, so I think that worked,
6:11 we actually could have caught the response form the API and print it out,
6:15 the response, but let's just go look at the MailChimp results
6:18 see if we're now on our list.
6:20 Anyway, here is what I decided to write: "Awesome, you are on the list", something fun and playful.
6:24 "Thanks for signing up to be notified about upcoming events and albums",
6:27 remember this is our band, "your address is in good hands, we won't spam you
6:31 and by the way, hey why don't you sign up and follow our twitter account as well?
6:34 Like if you are willing to sign up on the list
6:36 you are probably willing to follow our twitter account."
6:39 So, this is great, let's go check it at MailChimp.
6:42 Awesome, look, our Blue / Yellow Band has a subscriber, exciting,
6:46 let's go look who is it, want to bet? I think I know who it is,
6:50 awesome, email@example.com.
6:53 So, check this out, we just subscribed it, apparently it's evening time,
6:58 maybe I should go eat dinner, I don't know,
7:00 we can even see more information about ourselves here like what groups we're in,
7:03 I can make this customer or this person an important person in my list
7:08 so I can manage them separately, maybe they are like the head of an account,
7:11 at some company I am trying to work with, so I want to make sure I treat them well,
7:15 you can see all the activity about like past campaigns,
7:18 how I as a subscriber am basically responding.
7:24 MailChimp does e-commerce tracking
7:26 for certain things, I don't use that but you could here,
7:28 social profiles like I could, you know, I think it will start pulling stuff
7:32 like if it finds my Gmail registered somewhere but that email I just used,
7:37 is not really registered anywhere, it's just something that I know will get to me,
7:40 so I doubt you'll see it anywhere in that list.
7:44 Alright, so here you go, how are we, we were signed up via the API,
7:50 we did not detect the language because guess what,
7:53 I didn't use the form that they filled out, instead I used just the API,
7:56 and this is the time zone in GMT.
7:59 Great, so this is a really good start,
8:01 but, hopefully we end up with thousands of people in this list,
8:04 not just me signing up to myself, but that's how you use MailChimp.
8:07 Pretty easy, create an account, create a list, get the list id,
8:11 create API key, "pip install" the MailChimp API and boom api.list.subscribe.