Class: TheTradeDeskAds::AdCampaign

Inherits:
Base
  • Object
show all
Defined in:
lib/the_trade_desk_ads/ad_campaign.rb

Overview

An ad campaign has many ad sets and belongs to an ad account. developers.facebook.com/docs/marketing-api/reference/ad-campaign-group

Constant Summary collapse

FIELDS =
%w[
  id
  account_id
  buying_type
  can_use_spend_cap
  configured_status
  created_time
  effective_status
  name
  objective
  start_time
  stop_time
  updated_time spend_cap
].freeze
STATUSES =
%w[
  ACTIVE PAUSED
  DELETED
  PENDING_REVIEW
  DISAPPROVED
  PREAPPROVED
  PENDING_BILLING_INFO
  CAMPAIGN_PAUSED
  ARCHIVED
  ADSET_PAUSED
].freeze
OBJECTIVES =
%w[
  BRAND_AWARENESS
  CANVAS_APP_ENGAGEMENT
  CANVAS_APP_INSTALLS
  EVENT_RESPONSES
  LEAD_GENERATION
  LOCAL_AWARENESS
  MOBILE_APP_ENGAGEMENT
  MOBILE_APP_INSTALLS
  NONE
  OFFER_CLAIMS
  PAGE_LIKES
  POST_ENGAGEMENT
  LINK_CLICKS
  CONVERSIONS
  VIDEO_VIEWS
  PRODUCT_CATALOG_SALES
].freeze

Instance Method Summary collapse

Methods inherited from Base

auth, delete, #destroy, find, get, paginate, post, #save, #update

Instance Method Details

#ad_accountObject

belongs_to ad_account



53
54
55
# File 'lib/the_trade_desk_ads/ad_campaign.rb', line 53

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

#ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1) ⇒ Object

has_many ad_insights



91
92
93
94
95
96
97
98
# File 'lib/the_trade_desk_ads/ad_campaign.rb', line 91

def ad_insights(range: Date.today..Date.today, level: 'ad', time_increment: 1)
  query = {
    level: level,
    time_increment: time_increment,
    time_range: { 'since': range.first.to_s, 'until': range.last.to_s }
  }
  AdInsight.paginate("/#{id}/insights", query: query)
end

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

has_many ad_sets



59
60
61
# File 'lib/the_trade_desk_ads/ad_campaign.rb', line 59

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

#create_ad_set(name:, promoted_object:, targeting:, daily_budget:, optimization_goal:, billing_event: 'IMPRESSIONS', status: 'ACTIVE', is_autobid: true) ⇒ Object

Raises:

  • (Exception)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/the_trade_desk_ads/ad_campaign.rb', line 63

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.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
  else
    targeting.validate! # Will raise if invalid.
    targeting = targeting.to_hash
  end

  query = {
    campaign_id: id,
    name: name,
    targeting: targeting.to_json,
    promoted_object: promoted_object.to_json,
    optimization_goal: optimization_goal,
    daily_budget: daily_budget,
    billing_event: billing_event,
    status: status,
    is_autobid: is_autobid
  }
  result = AdSet.post("/act_#{account_id}/adsets", query: query)
  AdSet.find(result['id'])
end