Skip to content

Instantly share code, notes, and snippets.

@cgallagher
Last active December 20, 2015 03:19
Show Gist options
  • Save cgallagher/6062987 to your computer and use it in GitHub Desktop.
Save cgallagher/6062987 to your computer and use it in GitHub Desktop.
Not really production ready code in any sense of the word but will build up an ad and point it at a facebook custom audience... you can also generate a lookalike audience if you're bothered.
# not production code - just a bunch of calls that run through the process of creating an ad and pointing it at a custom audience on Facebook.
class Traffikr
cattr_accessor :custom_audience_id, :access_token, :users_json, :account_id, :campaign_id, :image_hash, :creative_id
class << self
def play_all
self.users_json = []
self.account_id = Settings.facebook.account_id
self.access_token = Settings.facebook.access_token
self.custom_audience_id = create_custom_audience
import_users_into_custom_audience
create_campaign
imported_file = stub_image_for_upload
create_ad_image(imported_file)
create_ad_creative
create_adgroup
end
def create_custom_audience
name = "super awesome custom audience #{Time.now.to_s}"
resp = Bookface.post("#{Settings.facebook.account_id}/customaudiences", query: { access_token: self.access_token, name: name})
return resp[:id]
end
def create_lookalike_audience
# this just creates a lookalike audience for one of the ones in our lib already
params = {
access_token: Settings.facebook.access_token,
name: "Looks like something!",
origin_audience: '6004988188980',
lookalike_spec: { type: 'similarity', country: 'IE'}.to_json
}
resp = Bookface.post("#{Settings.facebook.account_id}/customaudiences", query: params)
puts resp
end
def import_users_into_custom_audience
# some bs users from Waterworks for testing
ids = user_id_seed_data
puts "Attempting to import #{ids.length} users into the custom audience with an id of #{self.custom_audience_id}"
#hack range in for the time being.
user_chunks = ids[0..10].each_slice(50).to_a
user_chunks.each_with_index do |chunk, index|
puts "processing chunk #{index+1}"
import_ca_user_chunk(chunk)
end
puts "Users imported into custom audience - this may take some time to finish processing!"
return true
end
def create_campaign(name = "Super awesome campaign!", budget = "2000")
resp = Bookface.post("#{self.account_id}/adcampaigns", query: { access_token: self.access_token, name: name, daily_budget: budget, campaign_status: 2 })
puts "Campaign created successfully with an id of: #{resp[:id]}"
self.campaign_id = resp[:id]
return true
end
def create_adgroup
params = {
access_token: self.access_token,
campaign_id: self.campaign_id,
bid_type: 1,
max_bid: 15,
targeting: { countries: ['IE'], custom_audiences: [{id: self.custom_audience_id, name: 'customaudience'}]}.to_json,
creative: { creative_id: self.creative_id },
name: "Super Adgroup name #yolo!"
}
resp = Bookface.post("#{self.account_id}/adgroups", query: params)
puts resp
end
def create_ad_image(image)
resp = AdClient.post("/#{self.account_id}/adimages", :query => {
:access_token => self.access_token,
:"creative.jpg" => File.open(image.path),
})
upload_response = resp["images"]
self.image_hash = upload_response[upload_response.keys.first]["hash"]
end
def create_ad_creative
params = {
access_token: self.access_token,
type: 1,
title: "less than 25 characters!",
body: "Some really nice descriptive text now!",
image_hash: self.image_hash,
link_url: "http://www.betapond.com",
name: "My super awesome creative name for this!"
}
resp = Bookface.post("#{self.account_id}/adcreatives", query: params)
self.creative_id = resp[:id]
end
# using this method to pull in an image via url for upload into the fb api.
def stub_image_for_upload
imported_file = Tempfile.new(["creative", ".jpg"])
imported_file.binmode
open("http://cdn.fansided.com/wp-content/blogs.dir/229/files/2013/04/tywin-lannister-1024.jpg") { |data| imported_file.write data.read }
return imported_file
end
def user_id_seed_data
# put some uids in here
return []
end
private
def import_ca_user_chunk(users)
users_json = prepare_ca_user_json(users)
resp = Bookface.post("#{self.custom_audience_id}/users", query: { access_token: self.access_token, users: self.users_json })
puts resp
end
def prepare_ca_user_json(users)
users.each do |uid|
self.users_json << {'id' => uid.to_s}
end
self.users_json = self.users_json
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment