Method: Adapi::Campaign#update

Defined in:
lib/adapi/campaign.rb

#update(params = {}) ⇒ Object

Sets campaign data en masse, including criteria and ad_groups with keywords and ads

Warning: campaign data are not refreshed after update! We’d have to do it by get method and that would slow us down. If you want to see latest data, you have to fetch them again manually: Campaign#find or Campaign#find_complete

TODO implement primarily as class method, instance will be just a redirect with campaign_id



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/adapi/campaign.rb', line 180

def update(params = {})
  # REFACTOR for the moment, we use separate campaign object just to prepare and execute 
  # campaign update request. This is kinda ugly and should be eventually refactored (if
  # only because of weird transfer of potential errors later when dealing with response). 
  #
  # campaign basic data workflow: 
  # parse given params by loading them into Campaign.new and reading them back, parsed
  # REFACTOR should be parsed by separate Campaign class method
  #
  campaign = Adapi::Campaign.new(params)
  # HOTFIX remove :service_name param inserted byu initialize method
  params.delete(:service_name)
  # ...and load parsed params back into the hash
  params.keys.each { |k| params[k] = campaign.send(k) }
  params[:id] = @id

  @criteria = params.delete(:criteria)
  params.delete(:targets)
  @ad_groups = params.delete(:ad_groups) || []

  @bidding_strategy = params.delete(:bidding_strategy)

  operation = { 
    operator: 'SET', 
    operand: params
  }

  # BiddingStrategy update has slightly different DSL from other params 
  # https://developers.google.com/adwords/api/docs/reference/v201109_1/CampaignService.BiddingTransition
  #
  # See this post about BiddingTransition limitations:
  # https://groups.google.com/forum/?fromgroups#!topic/adwords-api/tmRk1m7PbhU
  # "ManualCPC can transition to anything and everything else can only transition to ManualCPC" 
  if @bidding_strategy
    operation[:bidding_transition] = { target_bidding_strategy: @bidding_strategy }
  end
 
  campaign.mutate(operation)

  check_for_errors(campaign)

  # update campaign criteria
  if @criteria && @criteria.size > 0
    new_criteria = Adapi::CampaignCriterion.new(
      :campaign_id => @id,
      :criteria => @criteria
    )

    new_criteria.update!

    check_for_errors(new_criteria)        
  end

  self.update_ad_groups!(@ad_groups)

  self.errors.empty?

rescue CampaignError => e
  false
end