Effectively using MailChimp and its API to communicate with your customers

Image representing MailChimp as depicted in Cr...Image via CrunchBase
I've been managing a mailing list with MailChimp for a few months now and I'm tremendously impressed with it.  It's a powerful application that produces effective email campaigns and it has a huge array of features. I thought I'd write up some work I just did with their API this week to show you how to effectively manage your communication strategy.




User Registration


So you've got a signup form where people can get an account to use your new whizbang application and you have a mailing list setup but how do you keep them synchronized?  What if you want to send them a nice, fancy welcome email as soon as they register?


You're going to need to know the list ID for the list you'll be working with.  You can find that by clicking on the List Settings tab and scrolling down to the bottom of the page.  It will look like this:



Also, I'll be using the PHP class that encapsulates the MailChimp API which you can download here and using version 1.2 of the API.


Here is my little library file:


<?php
   include($_SERVER['DOCUMENT_ROOT']."/lib/MCAPI.class.php");
    //API Key - see http://admin.mailchimp.com/account/api or run login() once
    $apikey = 'e21***********582***********9e-us1';

    //the Username & Password you use to login to your MailChimp account
    $username = 'joeuser';
    $password = 'password';

    define('MAILCHIMP_LIST_ID','6e****7b');

   function mailchimp_add($email,$merge_vars,$format = 'html', $doubleoptin = false,
         $update = false,$replace_int = true, $welcome = true) {

      global $apikey;

      $api = new MCAPI($apikey);
      $retval = $api->listSubscribe(MAILCHIMP_LIST_ID, $email, $merge_vars,'html',$doubleoptin,
         $update,$replace_int,$welcome);

      if ($api->errorCode){
         echo "Unable to load listSubscribe()!\n";
         echo "\tCode=".$api->errorCode."\n";
         echo "\tMsg=".$api->errorMessage."\n";

         return false;
      } else {
         return true;
      }
   }

   function mailchimp_update($email,$merge_vars) {
      global $apikey;
      $api = new MCAPI($apikey);
      $rv = $api->listUpdateMember(MAILCHIMP_LIST_ID,$email,$merge_vars);
   }
?>



listSubscribe()


The listSubscribe() method allows you to add a new email address to an existing list. 


You pass it a list ID, an email address and an array of Merge Variables so you can store more information about the user.  I modified my user registration function so that after the other registration steps the user gets added to the beta user mailing list automatically.


<?php
   function user_register($request) {

   // .... do a bunch of stuf.... 

         $merge_vars = array(
            "FNAME" => $request['first_name'],
            "LNAME" => $request['last_name'],
            "ACTIVE" => "Yes",
            "SOURCE" => $request['source']
         );
         $rc = mailchimp_add($request['email'],$merge_vars);

         return true;
      }

?>


So what does that all mean? The Merge variables have to match your list definitions. Those you set up on the List Settings tab by clicking on the "List Fields and *|MERGE|* Tags" link but we'll get into that more later. I added the SOURCE and ACTIVE variables to my OtherNum User List to do segmentation.

If you set the $welcome variable to true the user will get a welcome email which you can setup in advance by going to the "Create Forms" tab and selecting the "Final Welcome Email" template from the drop down menu.





Segmentation


So what do those merge variables do?  Well, I wanted a way to be able to easily email all the people who had signed up for an OtherNum  account but had never logged in or had not done so in over 30 days.  I also wanted to be able to email everyone who signed up for an account during StartupRiot because we have a few surprises / bonuses for those people.


I wrote a simple PHP script that does a MySQL query to get all of our users and perform some simple logic to determine who was 'Active' and who isn't.  Then I call a simple update function to change the values in the MailChimp list!


<?php
   while($row = mysql_fetch_array($q,MYSQL_ASSOC)) {
      $active = "Yes";

      if ($row['idle'] == "" || $row['idle'] > 15) {
         $active = "No";
      }
      $merge = array(
         'FIRST' => $row['first_name'],
         'LAST' => $row['last_name'],
         'ACTIVE' => $active,
         'SOURCE' => $row['source']
      );

      mailchimp_update($row['email'],$merge);

      printf("Name: %s %s Email: %s IDLE: %s ACTIVE: %s\n",$row['first_name'],$row['last_name'],$row['email'],$row['idle'],$active);
   }
?>





So now you have your users in your mailing list flagged as either active or idle and you have them broken out by how they got to you.  Now you can set up a campaign that specifically target those groups!




Create a new campaign of any type and to the right of the list names, you will see this:





Click "send to segment of list" and you'll see a screen like this:


So there you go! Now you can run this script on a regular basis and keep in touch with your customers in different ways depending on various aspects of their behavior! You could just as easily do this to create segments by class of product or any demographic information.

I hope you find this helpful! MailChimp is a great tool, it's free to use unless your list grows above 500 users (a nice problem to have...) and the site itself is extensively documented!

Have you done something similar with your MailChimp account? I'd love to hear about it!

Reblog this post [with Zemanta]

blog comments powered by Disqus