class FacebookAds::AdAccount

developers.facebook.com/docs/marketing-api/reference/ad-account

Constants

FIELDS

Public Class Methods

all(query = {}) click to toggle source
# File lib/facebook_ads/ad_account.rb, line 9
def all(query = {})
  get('/me/adaccounts', query: query, objectify: true)
end

Public Instance Methods

ad_audiences(limit: 100) click to toggle source

AdAudience

# File lib/facebook_ads/ad_account.rb, line 104
def ad_audiences(limit: 100)
  AdAudience.paginate("/#{id}/customaudiences", query: { limit: limit })
end
ad_campaigns(effective_status: ['ACTIVE'], limit: 100) click to toggle source

AdCampaign

# File lib/facebook_ads/ad_account.rb, line 28
def ad_campaigns(effective_status: ['ACTIVE'], limit: 100)
  AdCampaign.paginate("/#{id}/campaigns", query: { effective_status: effective_status, limit: limit })
end
ad_creatives(limit: 100) click to toggle source

AdCreative

# File lib/facebook_ads/ad_account.rb, line 82
def ad_creatives(limit: 100)
  AdCreative.paginate("/#{id}/adcreatives", query: { limit: limit })
end
ad_images(hashes: nil, limit: 100) click to toggle source

AdImage

# File lib/facebook_ads/ad_account.rb, line 61
def ad_images(hashes: nil, limit: 100)
  if !hashes.nil?
    AdImage.get("/#{id}/adimages", query: { hashes: hashes }, objectify: true)
  else
    AdImage.paginate("/#{id}/adimages", query: { limit: limit })
  end
end
ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1) click to toggle source

AdInsight

# File lib/facebook_ads/ad_account.rb, line 124
def ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1)
  ad_campaigns.map do |ad_campaign|
    ad_campaign.ad_insights(range: range, level: level, time_increment: time_increment)
  end.flatten
end
ad_sets(effective_status: ['ACTIVE'], limit: 100) click to toggle source

AdSet

# File lib/facebook_ads/ad_account.rb, line 49
def ad_sets(effective_status: ['ACTIVE'], limit: 100)
  AdSet.paginate("/#{id}/adsets", query: { effective_status: effective_status, limit: limit })
end
ad_users(limit: 100) click to toggle source

AdUser

# File lib/facebook_ads/ad_account.rb, line 22
def ad_users(limit: 100)
  AdCampaign.paginate("/#{id}/users", query: { limit: limit })
end
ads(effective_status: ['ACTIVE'], limit: 100) click to toggle source

Ad

# File lib/facebook_ads/ad_account.rb, line 55
def ads(effective_status: ['ACTIVE'], limit: 100)
  Ad.paginate("/#{id}/ads", query: { effective_status: effective_status, limit: limit })
end
advertisable_applications(limit: 100) click to toggle source

AdvertisableApplication

# File lib/facebook_ads/ad_account.rb, line 16
def advertisable_applications(limit: 100)
  AdvertisableApplication.paginate("/#{id}/advertisable_applications", query: { limit: limit })
end
create_ad_audience_with_pixel(name:, pixel_id:, event_name:, subtype: 'WEBSITE', retention_days: 15) click to toggle source
# File lib/facebook_ads/ad_account.rb, line 108
def create_ad_audience_with_pixel(name:, pixel_id:, event_name:, subtype: 'WEBSITE', retention_days: 15)
  query = {
    name: name,
    pixel_id: pixel_id,
    subtype: subtype,
    retention_days: retention_days,
    rule: { event: { i_contains: event_name } }.to_json,
    prefill: 1
  }

  result = AdAudience.post("/#{id}/customaudiences", query: query)
  AdAudience.find(result['id'])
end
create_ad_campaign(name:, objective:, status: 'ACTIVE') click to toggle source
# File lib/facebook_ads/ad_account.rb, line 32
def create_ad_campaign(name:, objective:, status: 'ACTIVE')
  raise ArgumentError, "Objective must be one of: #{AdCampaign::OBJECTIVES.join(', ')}" unless AdCampaign::OBJECTIVES.include?(objective)
  raise ArgumentError, "Status must be one of: #{AdCampaign::STATUSES.join(', ')}" unless AdCampaign::STATUSES.include?(status)
  query = { name: name, objective: objective, status: status }
  result = AdCampaign.post("/#{id}/campaigns", query: query)
  AdCampaign.find(result['id'])
end
create_ad_creative(creative, creative_type: nil, carousel: false) click to toggle source
# File lib/facebook_ads/ad_account.rb, line 86
def create_ad_creative(creative, creative_type: nil, carousel: false)
  # Support old deprecated carousel param.
  return create_carousel_ad_creative(creative) if carousel

  case creative_type
  when 'carousel'
    create_carousel_ad_creative(creative)
  when 'link'
    create_link_ad_creative(creative)
  when 'image'
    create_image_ad_creative(creative)
  else
    create_image_ad_creative(creative)
  end
end
create_ad_images(urls) click to toggle source
# File lib/facebook_ads/ad_account.rb, line 69
def create_ad_images(urls)
  files = urls.map do |url|
    name, path = download(url)
    [name, File.open(path)]
  end.to_h

  response = AdImage.post("/#{id}/adimages", query: files)
  files.each_value { |file| File.delete(file.path) }
  !response['images'].nil? ? ad_images(hashes: response['images'].map { |_key, hash| hash['hash'] }) : []
end
create_dynamic_ad_campaign(name:, product_catalog_id:, status: 'ACTIVE') click to toggle source
# File lib/facebook_ads/ad_account.rb, line 40
def create_dynamic_ad_campaign(name:, product_catalog_id:, status: 'ACTIVE')
  raise ArgumentError, "Status must be one of: #{AdCampaign::STATUSES.join(', ')}" unless AdCampaign::STATUSES.include?(status)
  query = { name: name, objective: 'PRODUCT_CATALOG_SALES', status: status, promoted_object: { product_catalog_id: product_catalog_id } }
  result = AdCampaign.post("/#{id}/campaigns", query: query)
  AdCampaign.find(result['id'])
end
delivery_estimate(targeting:, optimization_goal:, currency: 'USD') click to toggle source
# File lib/facebook_ads/ad_account.rb, line 147
def delivery_estimate(targeting:, optimization_goal:, currency: 'USD')
  raise ArgumentError, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.join(', ')}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal)

  if targeting.is_a?(AdTargeting)
    raise ArgumentError, 'The provided targeting spec is not valid.' unless targeting.validate!
    targeting = targeting.to_hash
  end

  query = {
    targeting_spec: targeting.to_json,
    optimization_goal: optimization_goal,
    currency: currency
  }

  self.class.get("/#{id}/delivery_estimate", query: query, objectify: false)
end
reach_estimate(targeting:, optimization_goal:, currency: 'USD') click to toggle source
# File lib/facebook_ads/ad_account.rb, line 130
def reach_estimate(targeting:, optimization_goal:, currency: 'USD')
  raise ArgumentError, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.join(', ')}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal)

  if targeting.is_a?(AdTargeting)
    raise ArgumentError, 'The provided targeting spec is not valid.' unless targeting.validate!
    targeting = targeting.to_hash
  end

  query = {
    targeting_spec: targeting.to_json,
    optimize_for: optimization_goal,
    currency: currency
  }

  self.class.get("/#{id}/reachestimate", query: query, objectify: false)
end

Private Instance Methods

create_image_ad_creative(creative) click to toggle source
# File lib/facebook_ads/ad_account.rb, line 193
def create_image_ad_creative(creative)
  required = %i[name page_id message link link_title image_hash call_to_action_type]

  unless (keys = required - creative.keys).length.zero?
    raise ArgumentError, "Creative is missing the following: #{keys.join(', ')}"
  end

  raise ArgumentError, "Creative call_to_action_type must be one of: #{AdCreative::CALL_TO_ACTION_TYPES.join(', ')}" unless AdCreative::CALL_TO_ACTION_TYPES.include?(creative[:call_to_action_type])
  query = AdCreative.photo(creative)
  result = AdCreative.post("/#{id}/adcreatives", query: query)
  AdCreative.find(result['id'])
end
download(url) click to toggle source
# File lib/facebook_ads/ad_account.rb, line 218
def download(url)
  pathname = Pathname.new(url)
  name = "#{pathname.dirname.basename}.jpg"
  # @FIXME: Need to handle exception here.
  data = RestClient.get(url).body
  file = File.open("/tmp/#{name}", 'w') # Assume *nix-based system.
  file.binmode
  file.write(data)
  file.close
  [name, file.path]
end