Class: Madmen::Google::Campaign

Inherits:
Object
  • Object
show all
Defined in:
lib/madmen/google/campaign.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Campaign

Instantiates a Campaign object, accepts a hash of optional parameters.

Usage:

Madmen::Google::Campaign.new(:name => 'My Campaign')

Returns:

A Campaign object.



93
94
95
96
97
98
# File 'lib/madmen/google/campaign.rb', line 93

def initialize(args)
  @id = args[:id]
  @name = args[:name]
  @status = args[:status]
  @daily_budget = args[:daily_budget]
end

Instance Attribute Details

#daily_budgetObject

Returns the value of attribute daily_budget.



12
13
14
# File 'lib/madmen/google/campaign.rb', line 12

def daily_budget
  @daily_budget
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/madmen/google/campaign.rb', line 9

def id
  @id
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'lib/madmen/google/campaign.rb', line 10

def name
  @name
end

#statusObject

Returns the value of attribute status.



11
12
13
# File 'lib/madmen/google/campaign.rb', line 11

def status
  @status
end

Class Method Details

.create(args) ⇒ Object

Creates a new campaign.

Usage:

c = Madmen::Google::Campaign.create(
  :name => "My Campaign",
  :status => 'PAUSED',
  :budget => {
    :period => 'DAILY',
    :amount => { :microAmount => 100 * 1000000 },
    :deliveryMethod => 'STANDARD'
  }
)

Returns:

A Campaign object.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/madmen/google/campaign.rb', line 34

def self.create(args)
  operand = args
  operand[:biddingStrategy] = Google.campaign_service.module::BudgetOptimizer.new
  if response = Google.call_service(:campaign_service, :mutate, [{:operand => operand, :operator => 'ADD'}])
    campaign = response.rval.value.first
    RAILS_DEFAULT_LOGGER.info("MADMEN: Campaign id %d was successfully added." % campaign.id)
    return Campaign.new(
      :name => campaign.name,
      :id => campaign.id,
      :status => campaign.status,
      :daily_budget => campaign.budget.amount.microAmount / 1000000
    )
  else
    return false
  end
end

.find(id) ⇒ Object

Retrieves an AdWords campaign from Google using the provided campaign id and instantiates a Campaign object with its attributes.

Usage:

Madmen::Google::Campaign.find(1)

Returns:

A Campaign object.

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/madmen/google/campaign.rb', line 62

def self.find(id)
  raise ArgumentError, "Cannot find a campaign without an id!" unless id
  selector = Google.campaign_service.module::CampaignSelector.new
  selector.ids << id
  if response = Google.call_service(:campaign_service, :get, selector)
    return false unless response.rval.entries
    campaign = response.rval.entries.first
    RAILS_DEFAULT_LOGGER.info("MADMEN: Campaign id %d was successfully retrieved." % campaign.id)
    return Campaign.new(
      :name => campaign.name,
      :id => campaign.id,
      :status => campaign.status,
      :daily_budget => campaign.budget.amount.microAmount / 1000000
    )
  else
    return false
  end
end

Instance Method Details

#activateObject

Sets the status of the campaign to active.



101
102
103
# File 'lib/madmen/google/campaign.rb', line 101

def activate
  set_status('active')
end

#ad_groupObject

Returns the ad group associated with this campaign.



118
119
120
# File 'lib/madmen/google/campaign.rb', line 118

def ad_group
  @ad_group ||= AdGroup.find_by_campaign_id(self.id)
end

#adsObject

Returns associated ads (via its ad group)



106
107
108
109
# File 'lib/madmen/google/campaign.rb', line 106

def ads
  return self.ad_group.ads if self.ad_group
  return nil
end

#ads=(ad_array) ⇒ Object

Sets associated ads, via its ad group. Creates an ad group if necessary.



112
113
114
115
# File 'lib/madmen/google/campaign.rb', line 112

def ads=(ad_array)
  create_ad_group unless self.ad_group
  return self.ad_group.ads = ad_array
end

#create_ad_groupObject

Creates an ad group for this campaign.



123
124
125
126
# File 'lib/madmen/google/campaign.rb', line 123

def create_ad_group
  return false if self.ad_group
  return AdGroup.create( :campaign => self )
end

#deleteObject

Deletes the campaign from AdWords.



148
149
150
# File 'lib/madmen/google/campaign.rb', line 148

def delete
  set_status('deleted')
end

#keywordsObject

Retrieves the keywords associated with this ad campaign.



153
154
155
156
# File 'lib/madmen/google/campaign.rb', line 153

def keywords
  return [] unless self.ad_group
  return self.ad_group.keywords
end

#keywords=(args) ⇒ Object

Sets the keywords associated with this ad campaign.



159
160
161
162
# File 'lib/madmen/google/campaign.rb', line 159

def keywords=(args)
  create_ad_group unless self.ad_group
  return self.ad_group.keywords = args
end

#pauseObject

Sets the status of the campaign to paused.



165
166
167
# File 'lib/madmen/google/campaign.rb', line 165

def pause
  set_status('paused')
end

#reset_targetingObject

Clear existing targeting information.



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/madmen/google/campaign.rb', line 170

def reset_targeting
  operand = Google.campaign_target_service.module::GeoTargetList.new
  operand.campaignId = self.id
  operand.targets = []
  
  if response = Google.call_service(:campaign_target_service, :mutate, [{:operand => operand, :operator => 'SET'}])
    RAILS_DEFAULT_LOGGER.info "MADMEN: Cleared targeting for campaign"
    return true
  else
    RAILS_DEFAULT_LOGGER.info "MADMEN: Unable to clear targeting for campaign"
    return false
  end
end

#targetingObject

Returns the targeting type and details for the campaign.



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
# File 'lib/madmen/google/campaign.rb', line 185

def targeting
  selector = Google.campaign_target_service.module::CampaignTargetSelector.new
  selector << self.id
  if response = Google.call_service(:campaign_target_service, :get, selector)
    return nil unless response.rval && response.rval.entries.first
    _targeting = response.rval.entries.select{|e| e.targets}.first.targets
    case _targeting.first.target_Type
      when "ProximityTarget"
        return {
          :kind => 'Proximity',
          :targets => _targeting.inject([]){ |targets, t| targets << {:lat => t.geoPoint.longitudeInMicroDegrees / 1000000.0, :lng => t.geoPoint.latitudeInMicroDegrees / 1000000.0}; targets }
        }
      when "ProvinceTarget"
        return {
          :kind => 'States',
          :targets => _targeting.inject([]){ |targets, t| targets << t.provinceCode.gsub(/US-/,'') }
        }
      when "CityTarget"
        return {
          :kind => 'Cities',
          :targets => _targeting.inject([]){ |targets, t| targets << "#{t.cityName}, #{t.provinceCode.gsub(/US-/,'')}" }
        }
    end
  else
    return nil
  end
end

#targeting=(args) ⇒ Object

Sets the geotarget for the campaign.

Usage:

# Specific cities
adwords_campaign.targeting = {
  :cities => [
    {:city => 'Richmond', :state => 'VA'},
    {:city => 'Petersburg', :state => 'VA'}
  ]
}

# Specific states
adwords_campaign.targeting = {
  :states => ["VA","DC"]
}

# Specific coordinates
adwords_campaign.targeting = {
  :coords => [
    {:lat => 38.0578, :lng => -97.9323}
  ]
}

# Specific country
adwords_campaign.targeting = { :country => "US" }


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/madmen/google/campaign.rb', line 240

def targeting=(args)
  
  self.reset_targeting
  targets = []

  if args[:cities]
    args[:cities].each do |city|
      target = Google.campaign_target_service.module::CityTarget.new
      target.cityName = city[:city]
      target.provinceCode = "US-#{city[:state]}"
      target.countryCode = "US"
      targets << target
    end
  elsif args[:states]
    args[:states].each do |state|
      target = Google.campaign_target_service.module::ProvinceTarget.new
      target.provinceCode = "US-#{state}"
      targets << target
    end
  elsif args[:coords]
    args[:coords].each do |coord|
      target = Google.campaign_target_service.module::ProximityTarget.new
      geo_point = Google.campaign_target_service.module::GeoPoint.new
      geo_point.longitudeInMicroDegrees = (coord[:lng] * 1000000).to_i
      geo_point.latitudeInMicroDegrees = (coord[:lat] * 1000000).to_i
      target.geoPoint = geo_point
      target.radiusDistanceUnits = 'MILES'
      target.radiusInUnits = 100
      targets << target
    end
  elsif args[:country] # nationwide
    target = Google.campaign_target_service.module::CountryTarget.new
    target.countryCode = args[:country]
    targets << target
  else
    raise ArgumentError, "Madmen: Could not determine targeting type from #{args}"
  end

  operand = Google.campaign_target_service.module::GeoTargetList.new
  operand.campaignId = self.id
  operand.targets = targets
  
  if response = Google.call_service(:campaign_target_service, :mutate, [{:operand => operand, :operator => 'SET'}])
    RAILS_DEFAULT_LOGGER.info "Madmen: Set geotargeting to #{args.keys.first.to_s}"
    return true
  else
    return false
  end
end