Class: FacebookAds::AdSet

Inherits:
Base
  • Object
show all
Defined in:
lib/facebook_ads/ad_set.rb

Overview

Constant Summary collapse

FIELDS =
%w[
  id
  account_id
  campaign_id
  name
  status
  configured_status
  effective_status
  bid_strategy
  bid_amount
  billing_event
  optimization_goal
  pacing_type
  daily_budget
  budget_remaining
  lifetime_budget
  promoted_object
  targeting
  created_time
  updated_time
].freeze
STATUSES =
%w[
  ACTIVE
  PAUSED
  DELETED
  PENDING_REVIEW
  DISAPPROVED
  PREAPPROVED
  PENDING_BILLING_INFO
  CAMPAIGN_PAUSED
  ARCHIVED
  ADSET_PAUSED
].freeze
BILLING_EVENTS =
%w[APP_INSTALLS IMPRESSIONS].freeze
OPTIMIZATION_GOALS =
%w[
  NONE
  APP_INSTALLS
  BRAND_AWARENESS
  AD_RECALL_LIFT
  CLICKS
  ENGAGED_USERS
  EVENT_RESPONSES
  IMPRESSIONS
  LEAD_GENERATION
  LINK_CLICKS
  OFFER_CLAIMS
  OFFSITE_CONVERSIONS
  PAGE_ENGAGEMENT
  PAGE_LIKES
  POST_ENGAGEMENT
  REACH
  SOCIAL_IMPRESSIONS
  VIDEO_VIEWS
  APP_DOWNLOADS
  LANDING_PAGE_VIEWS
].freeze
BID_STRATEGIES =
%w[
  LOWEST_COST_WITHOUT_CAP
  LOWEST_COST_WITH_BID_CAP
  TARGET_COST
].freeze

Instance Method Summary collapse

Methods inherited from Base

all, delete, #destroy, find, find_by, get, paginate, post, #save, #update

Instance Method Details

#activities(since: 1.day.ago) ⇒ Object

Retrieves activities for ad set (in last 24 hours by default)



113
114
115
# File 'lib/facebook_ads/ad_set.rb', line 113

def activities(since: 1.day.ago)
  AdSetActivity.get("/#{id}/activities", objectify: true).select { |activity| activity['event_time'] > since }
end

#ad_accountObject

AdAccount



74
75
76
# File 'lib/facebook_ads/ad_set.rb', line 74

def 
  @ad_account ||= AdAccount.find("act_#{}")
end

#ad_campaignObject

AdCampaign



80
81
82
# File 'lib/facebook_ads/ad_set.rb', line 80

def ad_campaign
  @campaign ||= AdCampaign.find(campaign_id)
end

#ad_insights(range: Date.today..Date.today, level: nil, breakdowns: [], fields: [], limit: 1_000) ⇒ Object

levels = enum adset, campaign, account breakdowns = list<enum product_id, …>



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/facebook_ads/ad_set.rb', line 100

def ad_insights(range: Date.today..Date.today, level: nil, breakdowns: [], fields: [], limit: 1_000)
  query = {
    time_range: { since: range.first.to_s, until: range.last.to_s },
    level: level,
    breakdowns: breakdowns&.join(','),
    fields: fields&.join(','),
    limit: limit
  }.reject { |_key, value| value.nil? || (value.respond_to?(:empty?) && value.empty?) }

  AdInsight.paginate("/#{id}/insights", query: query)
end

#ads(effective_status: ['ACTIVE'], limit: 100) ⇒ Object

Ad



86
87
88
# File 'lib/facebook_ads/ad_set.rb', line 86

def ads(effective_status: ['ACTIVE'], limit: 100)
  Ad.paginate("/#{id}/ads", query: { effective_status: effective_status, limit: limit })
end

#create_ad(name:, creative_id:, status: 'PAUSED') ⇒ Object



90
91
92
93
94
# File 'lib/facebook_ads/ad_set.rb', line 90

def create_ad(name:, creative_id:, status: 'PAUSED')
  query = { name: name, adset_id: id, creative: { creative_id: creative_id }.to_json, status: status }
  result = Ad.post("/act_#{}/ads", query: query)
  Ad.find(result['id'])
end