Class: Madmen::Google::AdGroup

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

Overview

An AdGroup represents a Google AdWords ad group object.

Refer to Madmen::Google::AdGroup for details.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ AdGroup

Creates an instance of the AdGroup class.

Usage:

Madmen::Google::AdGroup.new(:name => 'My AdGroup', :status => 'enabled')

Returns:

An AdGroup object.



95
96
97
98
99
100
101
# File 'lib/madmen/google/ad_group.rb', line 95

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

Instance Attribute Details

#ad_idsObject

Returns the value of attribute ad_ids.



13
14
15
# File 'lib/madmen/google/ad_group.rb', line 13

def ad_ids
  @ad_ids
end

#campaign_idObject

Returns the value of attribute campaign_id.



14
15
16
# File 'lib/madmen/google/ad_group.rb', line 14

def campaign_id
  @campaign_id
end

#detailsObject

A dump of the associated adwords4r object



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

def details
  @details
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Class Method Details

.create(args) ⇒ Object

Creates a new AdGroup in Google AdWords.

Usage:

Madmen::Google::AdGroup.create(
  :campaign => my_adwords_campaign_object
)

Returns:

An AdGroup object on success, false on failure.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/madmen/google/ad_group.rb', line 30

def self.create(args)
  _campaign = args[:campaign]
  operation = {
    :operand => {
      :name => "#{_campaign.name} Ad Group #{Time.zone.now.to_i}",
      :status => 'ENABLED',
      :campaignId =>  _campaign.id
    },
    :operator => 'ADD'
  }
  if response = Google.call_service(:ad_group_service, :mutate, [operation])
    ad_group = response.rval.value.first
    RAILS_DEFAULT_LOGGER.info 'MADMEN: Ad group id %d was successfully added.' % ad_group.id
    return AdGroup.new(
      :name => ad_group.name,
      :id => ad_group.id,
      :status => ad_group.status,
      :details => ad_group
    )
  else
    return false
  end
end

.find_by_campaign_id(id) ⇒ Object

Finds the ad group associated with a campaign through the campaign id.

Usage:

Madmen::Google::AdGroup.find_by_campaign_id(1)

Returns:

An AdGroup object on success, nil on failure.

Raises:

  • (ArgumentError)


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

def self.find_by_campaign_id(id)
  raise ArgumentError, "Cannot find a campaign without an id!" unless id
  selector = Google.ad_group_service.module::AdGroupSelector.new
  selector.campaignId = id
  if response = Google.call_service(:ad_group_service, :get, selector)
    return nil unless response.rval && ! response.rval.entries.blank?
    ad_group = response.rval.entries.first
    RAILS_DEFAULT_LOGGER.info("MADMEN: Ad group id %d was successfully retrieved." % ad_group.id)
    return AdGroup.new(
      :name => ad_group.name,
      :id => ad_group.id,
      :status => ad_group.status,
      :details => ad_group
    )
  else
    return nil
  end
end

Instance Method Details

#adsObject

Returns an array of ads associated with this ad group.



104
105
106
# File 'lib/madmen/google/ad_group.rb', line 104

def ads
  @ads ||= Google::Ad.find_by_ad_group_id(self.id)
end

#ads=(ads) ⇒ Object

Creates ads associated with this ad group, automatically skipping existing ads in the arguments and deleting any existing ads that are not specified in the arguments.

Usage:

my_ad_group.ads = (
  { 
    :headline       => 'My Headline',
    :description_1  => 'Desc 1',
    :description_2  => 'Desc 2',
    :url            => 'http://www.seologic.com/foo',
    :display_url    => 'www.seologic.com'
  },
  { 
    :headline       => 'My Headline 2',
    :description_1  => 'Desc 1',
    :description_2  => 'Desc 2',
    :url            => 'http://www.seologic.com/foo',
    :display_url    => 'www.seologic.com'
  }
)

Returns:

An array of Ad objects.



135
136
137
138
139
140
141
142
143
144
# File 'lib/madmen/google/ad_group.rb', line 135

def ads=(ads)
  # Delete existing keywords not in the arg list
  self.ads.each do |ad|
    ad.send(:delete) unless ads.include?({:headline => ad.headline, :description_1 => ad.description_1, :description_2 => ad.description_2, :url => ad.url, :display_url => ad.display_url})
  end

  ads.each{ |ad| Ad.create(:ad => ad, :ad_group_id => self.id) }
  @ads = nil
  return self.ads
end

#deleteObject

Deletes this ad group.



152
153
154
# File 'lib/madmen/google/ad_group.rb', line 152

def delete
  set_status('deleted')
end

#enableObject

Sets the status of this ad group to ‘enabled’



147
148
149
# File 'lib/madmen/google/ad_group.rb', line 147

def enable
  set_status('enabled')
end

#keywordsObject

Returns an array of keywords associated with this ad group.



157
158
159
# File 'lib/madmen/google/ad_group.rb', line 157

def keywords
  @keywords ||= Google::Keyword.find_by_ad_group_id(self.id)
end

#keywords=(keywords_array) ⇒ Object

Sets keywords associated with this ad group.

Note that existing keywords in the arguments list will be ignored, while existing keywords that are not present in the arguments will be deleted.

Usage:

ad_group.keywords = [
  {:text => 'foo', :match_type => 'exact'},
  {:text => 'bar', :match_type => 'exact'}
]

Returns:

An array of Keyword objects.



177
178
179
180
181
182
183
184
185
186
# File 'lib/madmen/google/ad_group.rb', line 177

def keywords=(keywords_array)
  # Delete existing keywords not in the arg list
  self.keywords.each do |keyword|
    keyword.delete unless keywords_array.include?({:text => keyword.text, :match_type => keyword.match_type})
  end
  # Create keywords
  keywords_array.each{ |keyword| Keyword.create(:text => keyword[:text], :match_type => keyword[:match_type], :ad_group_id => self.id) }
  @keywords = nil
  return self.keywords
end

#pauseObject

Sets the status of this ad group to ‘paused’



189
190
191
# File 'lib/madmen/google/ad_group.rb', line 189

def pause
  set_status('paused')
end