AS3 MailChimp Library
While building our awesome new website, we wanted to ensure that users could easily subscribe to our newsletter through the Flash version, without having to be redirected anywhere else. Convenience is king, and we wanted to deliver it!
We use MailChimp, the best email marketing platform available! And luckily, they have a great API to work with. Through Flash, we were able to add subscribers to our list and also return any results, i.e. error or success.
While searching Google I found an article by Christian Cox on MailChimp’s API and AS2, but that wasn’t enough. We needed a nice AS3 version of the code. So Justin and I put together this nice, simple code that should work for anyone. If I had more time I would definitely make a class.. Perhaps later this month.
//api and list id keys
var _api:String = "MAIL_CHIMP_API_KEY_HERE";
var _listID:String = "LIST_ID_HERE";
//text box handler
emailAddress.text = "email address";
emailAddress.tabIndex = 1;
emailAddress.addEventListener(FocusEvent.FOCUS_IN, txtFocusIn);
emailAddress.addEventListener(FocusEvent.FOCUS_OUT, txtFocusOut);
function txtFocusIn(e:FocusEvent) {
emailAddress.text = "";
}
function txtFocusOut(e:FocusEvent) {
if (emailAddress.text == "") {
emailAddress.text = "email address";
}
}
//button setup
submitBtn.buttonMode = true;
submitBtn.addEventListener(MouseEvent.CLICK, submitForm);
function submitForm(e:Event) {
var email:String = emailAddress.text;
//check if valid
if (isValidEmail(email)) {
//set response text
responseText.text = "sending...";
//disable the submit button
submitBtn.removeEventListener(MouseEvent.CLICK, submitForm);
//setup POST
var variables:URLVariables = new URLVariables("method=listSubscribe&output=xml&apikey=" + _api + "&id=" + _listID + "&email_address=" + email + "&merge_vars=");
var request:URLRequest = new URLRequest();
request.url = "http://api.mailchimp.com/1.2/?method=listSubscribe";
request.method = URLRequestMethod.POST;
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, completeHandler);
try {
trace("loading...");
responseText.text = "loading...";
loader.load(request);
}
catch(error:Error) {
trace("unable to load URL");
responseText.text = "Oh, that's embarassing. something went wrong, please try again. Thanks!";
trace(e.target.data);
}
function completeHandler(e:Event) {
var _t:String = unescape(e.target.data); //decode the uri
var _xml:XMLList = new XMLList(_t); //parse the xml
trace(_xml.@type);
if (_xml.@type == "array") { //check to see if there is an error
trace(_xml.error);
responseText.text = _xml.error;
resetForm();
} else if (_xml.@type == "boolean") { //check to see if successfully added
trace("successfully added to list");
responseText.text = "You have beeen successfully added to our list. Thank you!";
resetForm();
}
}
} else {
trace ("email invalid");
resetForm ();
responseText.text = "invalid email!";
}
}
//validate given email
function isValidEmail(_e:String):Boolean {
var exp:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
return exp.test(_e);
}
//reset the form elements
function resetForm(){
submitBtn.addEventListener(MouseEvent.CLICK, submitForm);
emailAddress.text = "email address";
}
Done and done. Pretty easy, right? You can also download the mailchimp_api.
UPDATE: A new version has been created that includes First and Last Name data collection [mailchimp_api_1.3].





socialize