Class: FacebookAds::AdAccount
- Defined in:
- lib/facebook_ads/ad_account.rb
Overview
An ad account has many ad campaigns, ad images, and ad creatives. developers.facebook.com/docs/marketing-api/reference/ad-account
Constant Summary collapse
- FIELDS =
%w[id account_id account_status age created_time currency name].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#ad_audiences ⇒ Object
has_many ad_audiences.
-
#ad_campaigns(effective_status: ['ACTIVE'], limit: 100) ⇒ Object
has_many campaigns.
-
#ad_creatives(limit: 100) ⇒ Object
has_many ad_creatives.
-
#ad_images(hashes: nil, limit: 100) ⇒ Object
has_many ad_images.
-
#ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1) ⇒ Object
has_many ad_insights.
-
#ad_sets(effective_status: ['ACTIVE'], limit: 100) ⇒ Object
has_many ad_sets.
-
#ads(effective_status: ['ACTIVE'], limit: 100) ⇒ Object
has_many ads.
-
#applications ⇒ Object
has_many applications.
-
#create_ad_audience_with_pixel(name:, pixel_id:, event_name:, subtype: 'WEBSITE', retention_days: 15) ⇒ Object
hash_many ad_audiences.
- #create_ad_campaign(name:, objective:, status: 'ACTIVE') ⇒ Object
- #create_ad_creative(creative, creative_type: nil, carousel: false) ⇒ Object
- #create_ad_images(urls) ⇒ Object
- #create_dynamic_ad_campaign(name:, product_catalog_id:, status: 'ACTIVE') ⇒ Object
- #delivery_estimate(targeting:, optimization_goal:, currency: 'USD') ⇒ Object
- #reach_estimate(targeting:, optimization_goal:, currency: 'USD') ⇒ Object
Methods inherited from Base
delete, #destroy, find, get, paginate, post, #save, #update
Class Method Details
.all(query = {}) ⇒ Object
8 9 10 |
# File 'lib/facebook_ads/ad_account.rb', line 8 def all(query = {}) get('/me/adaccounts', query: query, objectify: true) end |
.find_by(conditions) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/facebook_ads/ad_account.rb', line 12 def find_by(conditions) all.detect do |object| conditions.all? do |key, value| object.send(key) == value end end end |
Instance Method Details
#ad_audiences ⇒ Object
has_many ad_audiences
98 99 100 |
# File 'lib/facebook_ads/ad_account.rb', line 98 def ad_audiences AdAudience.paginate("/#{id}/customaudiences") end |
#ad_campaigns(effective_status: ['ACTIVE'], limit: 100) ⇒ Object
has_many campaigns
23 24 25 |
# File 'lib/facebook_ads/ad_account.rb', line 23 def ad_campaigns(effective_status: ['ACTIVE'], limit: 100) AdCampaign.paginate("/#{id}/campaigns", query: { effective_status: effective_status, limit: limit }) end |
#ad_creatives(limit: 100) ⇒ Object
has_many ad_creatives
65 66 67 |
# File 'lib/facebook_ads/ad_account.rb', line 65 def ad_creatives(limit: 100) AdCreative.paginate("/#{id}/adcreatives", query: { limit: limit }) end |
#ad_images(hashes: nil, limit: 100) ⇒ Object
has_many ad_images
44 45 46 47 48 49 50 |
# File 'lib/facebook_ads/ad_account.rb', line 44 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) ⇒ Object
has_many ad_insights
104 105 106 107 108 |
# File 'lib/facebook_ads/ad_account.rb', line 104 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) ⇒ Object
has_many ad_sets
86 87 88 |
# File 'lib/facebook_ads/ad_account.rb', line 86 def ad_sets(effective_status: ['ACTIVE'], limit: 100) AdSet.paginate("/#{id}/adsets", query: { effective_status: effective_status, limit: limit }) end |
#ads(effective_status: ['ACTIVE'], limit: 100) ⇒ Object
has_many ads
92 93 94 |
# File 'lib/facebook_ads/ad_account.rb', line 92 def ads(effective_status: ['ACTIVE'], limit: 100) Ad.paginate("/#{id}/ads", query: { effective_status: effective_status, limit: limit }) end |
#applications ⇒ Object
has_many applications
150 151 152 |
# File 'lib/facebook_ads/ad_account.rb', line 150 def applications self.class.get("/#{id}/advertisable_applications", objectify: false) end |
#create_ad_audience_with_pixel(name:, pixel_id:, event_name:, subtype: 'WEBSITE', retention_days: 15) ⇒ Object
hash_many ad_audiences
156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/facebook_ads/ad_account.rb', line 156 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') ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/facebook_ads/ad_account.rb', line 27 def create_ad_campaign(name:, objective:, status: 'ACTIVE') raise Exception, "Objective must be one of: #{AdCampaign::OBJECTIVES.join(', ')}" unless AdCampaign::OBJECTIVES.include?(objective) raise Exception, "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) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/facebook_ads/ad_account.rb', line 69 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) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/facebook_ads/ad_account.rb', line 52 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.values.each { |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') ⇒ Object
35 36 37 38 39 40 |
# File 'lib/facebook_ads/ad_account.rb', line 35 def create_dynamic_ad_campaign(name:, product_catalog_id:, status: 'ACTIVE') raise Exception, "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') ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/facebook_ads/ad_account.rb', line 129 def delivery_estimate(targeting:, optimization_goal:, currency: 'USD') raise Exception, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.join(', ')}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal) if targeting.is_a?(AdTargeting) if targeting.validate! targeting = targeting.to_hash else raise Exception, 'The provided targeting spec is not valid.' end 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') ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/facebook_ads/ad_account.rb', line 110 def reach_estimate(targeting:, optimization_goal:, currency: 'USD') raise Exception, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.join(', ')}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal) if targeting.is_a?(AdTargeting) if targeting.validate! targeting = targeting.to_hash else raise Exception, 'The provided targeting spec is not valid.' end end query = { targeting_spec: targeting.to_json, optimize_for: optimization_goal, currency: currency } self.class.get("/#{id}/reachestimate", query: query, objectify: false) end |