Created
January 24, 2014 10:36
-
-
Save oriolgual/8595179 to your computer and use it in GitHub Desktop.
MailChimp + FullContact
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gem install fullcontact pry gibbon | |
# Setup these fields at mailchimp: FNAME, LNAME, TWITTER, FACEBOOK, GITHUB, LOCATION, GENDER. | |
# Usage: ruby mailchimp_full_contact.rb MAILCHIMP_LIST_NAME | |
require "rubygems" | |
require "fullcontact" | |
require "pry" | |
require 'gibbon' | |
# This could go in an initializer | |
FullContact.configure do |config| | |
config.api_key = "FULL_CONTACT_API_KEY" | |
end | |
Gibbon::API.api_key = "MAILCHIMP_API_KEY" | |
def batch_data(member) | |
{ | |
email: {euid: member['euid']}, | |
merge_vars: member['merges'].update(person_data(member['email'])) | |
} | |
end | |
def person_data(email) | |
puts "Getting data for #{email}" | |
begin | |
person = FullContact.person(email: email) | |
rescue FullContact::Error | |
person = nil | |
end | |
return {} unless person | |
data = {} | |
data["FNAME"] = person.contact_info.given_name if person.contact_info | |
data["LNAME"] = person.contact_info.family_name if person.contact_info | |
data["TWITTER"] = find_social_profile_url(person, 'twitter') | |
data["FACEBOOK"] = find_social_profile_url(person, 'facebook') | |
data["GITHUB"] = find_social_profile_url(person, 'github') | |
data["LOCATION"] = person.demographics.location_general if person.demographics | |
data["GENDER"] = person.demographics.gender if person.demographics | |
data.delete_if {|key,value| value == nil} | |
end | |
def find_social_profile_url(person, name) | |
return unless person.social_profiles | |
profile = person.social_profiles.find {|profile| profile.type == name} | |
profile && profile['url'] | |
end | |
def full_profile?(member) | |
member['merges'].values.all? {|value| value.to_s != ''} | |
end | |
gb = Gibbon::API.new | |
list = gb.lists.list({:filters => {:list_name => ARGV.first}}) | |
list_id = list['data'].first["id"] | |
# TODO: This just processes the first 300 emails. | |
members = 3.times.map do |index| | |
data = gb.lists.members({:id => list_id, opts: {start: index + 1, limit: 100} }) | |
sleep(0.5) | |
data['data'] | |
end | |
members = members.flatten(1) | |
full_contact_data = members.map do |member| | |
next if full_profile?(member) | |
batch_data(member) | |
end | |
binding.pry | |
gb.lists.batch_subscribe(id: list_id, batch: full_contact_data, update_existing: true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment