Method: Adapi::Campaign#create

Defined in:
lib/adapi/campaign.rb

#createObject

create campaign with ad_groups and ads



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/adapi/campaign.rb', line 119

def create
  return false unless self.valid?      
  
  # set defaults for budget for campaign.create only
  self.budget = budget.reverse_merge( period: 'DAILY', delivery_method: 'STANDARD' )

  # create basic campaign attributes
  operand = Hash[
    [ :name, :status, :start_date, :end_date,
      :budget, :bidding_strategy, :network_setting, :settings ].map do |k|
      [ k.to_sym, self.send(k) ] if self.send(k)
    end.compact
  ]

  # set default values for settings (for create only - should we set it also for update?)
  # PS: KeywordMatchSetting is required since 201206
  operand[:settings] ||= []
  unless operand[:settings].map { |s| s[:xsi_type] }.include?('KeywordMatchSetting')
    operand[:settings] << { :xsi_type => 'KeywordMatchSetting', :opt_in => false }
  end

  response = self.mutate( 
    operator: 'ADD', 
    operand: operand
  )

  check_for_errors(self)

  self.id = response[:value].first[:id] rescue nil
  
  if criteria && criteria.size > 0
    new_criteria = Adapi::CampaignCriterion.create(
      campaign_id: @id,
      criteria: criteria
    )

    check_for_errors(new_criteria)
  end

  ad_groups.each do |ad_group_data|
    ad_group = Adapi::AdGroup.create(
      ad_group_data.merge( campaign_id: @id )
    )

    check_for_errors(ad_group, :prefix => "AdGroup \"#{ad_group[:id] || ad_group[:name]}\"")
  end

  self.errors.empty?

rescue CampaignError => e
  false
end