Skip to content

Commit

Permalink
Add reach_estimate to AdAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
cte committed Apr 26, 2017
1 parent dc7f406 commit ae35878
Show file tree
Hide file tree
Showing 7 changed files with 455 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Style/IndentationWidth:
Enabled: false
Style/ElseAlignment:
Enabled: false
Style/GuardClause:
Enabled: false

Lint/EndAlignment:
Enabled: false
27 changes: 24 additions & 3 deletions lib/facebook_ads/ad_account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,39 @@ def ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1)
end.flatten
end

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

# has_many applications

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

def create_ad_audience_with_pixel(name:, pixel_id:, event_name:)
# hash_many ad_audiences

def create_ad_audience_with_pixel(name:, pixel_id:, event_name:, subtype: 'WEBSITE', retention_days: 15)
query = {
name: name,
pixel_id: pixel_id,
subtype: 'WEBSITE',
retention_days: 15,
subtype: subtype,
retention_days: retention_days,
rule: { event: { i_contains: event_name } }.to_json,
prefill: 1
}
Expand Down
4 changes: 2 additions & 2 deletions lib/facebook_ads/ad_campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def ad_sets(effective_status: ['ACTIVE'], limit: 100)
end

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

if targeting.is_a?(Hash)
# NOP
Expand Down
2 changes: 1 addition & 1 deletion lib/facebook_ads/ad_targeting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def to_hash
user_os: user_os,
user_device: user_device,
app_install_state: app_install_state
}.compact
}.reject { |_k, v| v.nil? }
end
end
end
12 changes: 12 additions & 0 deletions spec/facebook_ads/ad_account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@
verify(format: :json) { JSON.dump(ads) }
end
end

describe '.reach_estimate' do
it 'estimates the reach of a targeting spec', :vcr do
targeting = FacebookAds::AdTargeting.new
targeting.genders = [FacebookAds::AdTargeting::WOMEN]
targeting.age_min = 18
targeting.age_max = 20
targeting.countries = ['US']
reach = account.reach_estimate(targeting: targeting, optimization_goal: 'OFFSITE_CONVERSIONS')
verify(format: :json) { JSON.dump(reach) }
end
end
end
Loading

0 comments on commit ae35878

Please sign in to comment.