Skip to content

Commit ae35878

Browse files
committed
Add reach_estimate to AdAccount
1 parent dc7f406 commit ae35878

File tree

7 files changed

+455
-6
lines changed

7 files changed

+455
-6
lines changed

.rubocop.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Style/IndentationWidth:
2626
Enabled: false
2727
Style/ElseAlignment:
2828
Enabled: false
29+
Style/GuardClause:
30+
Enabled: false
2931

3032
Lint/EndAlignment:
3133
Enabled: false

lib/facebook_ads/ad_account.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,39 @@ def ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1)
9696
end.flatten
9797
end
9898

99+
def reach_estimate(targeting:, optimization_goal:, currency: 'USD')
100+
raise Exception, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.join(', ')}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal)
101+
102+
if targeting.is_a?(AdTargeting)
103+
if targeting.validate!
104+
targeting = targeting.to_hash
105+
else
106+
raise Exception, 'The provided targeting spec is not valid.'
107+
end
108+
end
109+
110+
query = {
111+
targeting_spec: targeting.to_json,
112+
optimize_for: optimization_goal,
113+
currency: currency
114+
}
115+
self.class.get("/#{id}/reachestimate", query: query, objectify: false)
116+
end
117+
99118
# has_many applications
100119

101120
def applications
102121
self.class.get("/#{id}/advertisable_applications", objectify: false)
103122
end
104123

105-
def create_ad_audience_with_pixel(name:, pixel_id:, event_name:)
124+
# hash_many ad_audiences
125+
126+
def create_ad_audience_with_pixel(name:, pixel_id:, event_name:, subtype: 'WEBSITE', retention_days: 15)
106127
query = {
107128
name: name,
108129
pixel_id: pixel_id,
109-
subtype: 'WEBSITE',
110-
retention_days: 15,
130+
subtype: subtype,
131+
retention_days: retention_days,
111132
rule: { event: { i_contains: event_name } }.to_json,
112133
prefill: 1
113134
}

lib/facebook_ads/ad_campaign.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ def ad_sets(effective_status: ['ACTIVE'], limit: 100)
1919
end
2020

2121
def create_ad_set(name:, promoted_object:, targeting:, daily_budget:, optimization_goal:, billing_event: 'IMPRESSIONS', status: 'ACTIVE', is_autobid: true)
22-
raise Exception, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.to_sentence}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal)
23-
raise Exception, "Billing event must be one of: #{AdSet::BILLING_EVENTS.to_sentence}" unless AdSet::BILLING_EVENTS.include?(billing_event)
22+
raise Exception, "Optimization goal must be one of: #{AdSet::OPTIMIZATION_GOALS.join(', ')}" unless AdSet::OPTIMIZATION_GOALS.include?(optimization_goal)
23+
raise Exception, "Billing event must be one of: #{AdSet::BILLING_EVENTS.join(', ')}" unless AdSet::BILLING_EVENTS.include?(billing_event)
2424

2525
if targeting.is_a?(Hash)
2626
# NOP

lib/facebook_ads/ad_targeting.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def to_hash
5353
user_os: user_os,
5454
user_device: user_device,
5555
app_install_state: app_install_state
56-
}.compact
56+
}.reject { |_k, v| v.nil? }
5757
end
5858
end
5959
end

spec/facebook_ads/ad_account_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,16 @@
7878
verify(format: :json) { JSON.dump(ads) }
7979
end
8080
end
81+
82+
describe '.reach_estimate' do
83+
it 'estimates the reach of a targeting spec', :vcr do
84+
targeting = FacebookAds::AdTargeting.new
85+
targeting.genders = [FacebookAds::AdTargeting::WOMEN]
86+
targeting.age_min = 18
87+
targeting.age_max = 20
88+
targeting.countries = ['US']
89+
reach = account.reach_estimate(targeting: targeting, optimization_goal: 'OFFSITE_CONVERSIONS')
90+
verify(format: :json) { JSON.dump(reach) }
91+
end
92+
end
8193
end

0 commit comments

Comments
 (0)