Skip to content

Instantly share code, notes, and snippets.

@natarius
Created February 14, 2013 23:09
Show Gist options
  • Save natarius/4957245 to your computer and use it in GitHub Desktop.
Save natarius/4957245 to your computer and use it in GitHub Desktop.
Automatically add the users that sign up to your rails app to a mailchimp list. I usually do this async via Resque and not exactly like in this snippet
Gibbon.api_key = ENV["MAILCHIMP_API_KEY"]
require 'gibbon'
after_create :sync_with_mailchimp
def sync_with_mailchimp
begin
list_name = "signed up users"
cm_user_list = Gibbon.lists({:filters => {:list_name => list_name}})
cm_user_list_id = cm_user_list["data"].first["id"]
Gibbon.list_subscribe(
{
:id => cm_user_list_id,
:email_address => self.email,
:double_optin => false,
:update_existing => true,
:merge_vars => {:FNAME => self.get_first_name, :LNAME => self.get_last_name}
}
)
rescue => e
Rails.logger.error "Error adding new user to mailchimp. Msg: #{e}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment