Class: Madmen::Google::Ad

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

Overview

Represents an AdWords ad. Ads belong to ad groups, but may be accessed via the campaign.

Note that ads may not be directly deleted; rather, assign ads to a campaign or ad group, and any existing ads that are left out will be automatically removed.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Ad

Create a new Ad object. Accepts a hash of optional arguments.

Usage:

Madmen::Google::Ad.new(
  :ad_group_id    => 1, 
  :headline       => 'My Headline',
  :description_1  => 'Ad Line 1',
  :description_2  => 'Ad Line 2',
  :url            => 'http://www.seologic.com/foo',
  :display_url    => 'www.seologic.com'
)

Returns:

An Ad object.



137
138
139
140
141
142
143
144
145
# File 'lib/madmen/google/ad.rb', line 137

def initialize(args)
  @id = args[:id]
  @headline = args[:headline]
  @description_1 = args[:description_1]
  @description_2 = args[:description_2]
  @url = args[:url]
  @display_url = args[:display_url]
  @ad_group_id = args[:ad_group_id]
end

Instance Attribute Details

#ad_group_idObject

Returns the value of attribute ad_group_id.



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

def ad_group_id
  @ad_group_id
end

#description_1Object

Returns the value of attribute description_1.



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

def description_1
  @description_1
end

#description_2Object

Returns the value of attribute description_2.



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

def description_2
  @description_2
end

#display_urlObject

Returns the value of attribute display_url.



16
17
18
# File 'lib/madmen/google/ad.rb', line 16

def display_url
  @display_url
end

#headlineObject

Returns the value of attribute headline.



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

def headline
  @headline
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#urlObject

Returns the value of attribute url.



15
16
17
# File 'lib/madmen/google/ad.rb', line 15

def url
  @url
end

Class Method Details

.create(args) ⇒ Object

Usage:

Ad.create(
  :ad_group_id  => 1,
  :headline     => 'My Headline',
  :description1 => 'Ad Line 1',
  :description2 => 'Ad Line 2',
  :url          => 'http://www.seologic.com/foo',
  :displayUrl   => 'www.seologic.com',
)

Returns:

An Ad object on success; false on failure.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/madmen/google/ad.rb', line 35

def self.create(args)
  return false if Ad.exists?(args)
  operand = Google.ad_group_ad_service.module::AdGroupAd.new
  operand.adGroupId = args[:ad_group_id]
  operand.ad = Google.ad_group_ad_service.module::TextAd.new
  args.keys.each{|key| operand.ad.send("#{key}=", args[key]) unless key == :ad_group_id}
  operand.status = 'ENABLED'
  operation = Google.ad_group_ad_service.module::AdGroupAdOperation.new
  operation.operand = operand
  operation.operator = 'ADD'
  if response = Google.call_service(:ad_group_ad_service, :mutate, [operation])
    ad = response.rval.value.first.ad
    RAILS_DEFAULT_LOGGER.info 'MADMEN: Text ad id %d was successfully added.' % ad.id
    return Ad.new(
      :id => ad.id,
      :ad_group_id => args[:ad_group_id],
      :headline => ad.headline,
      :description_1 => ad.description1,
      :description_2 => ad.description2,
      :url => ad.url,
      :display_url => ad.displayUrl
    )
  else
    return false
  end
end

.exists?(args) ⇒ Boolean

Checks to see if a given ad already exists for the specified ad group.

Usage:

Ad.exists?(
  :ad_group_id => 1,
  :ad => {
    :headline       => 'My Headline',
    :description_1  => 'Ad Line 1',
    :description_2  => 'Ad Line 2',
    :url            => 'http://www.seologic.com/foo',
    :display_url    => 'www.seologic.com'
  }
)

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/madmen/google/ad.rb', line 77

def self.exists?(args)
  _ads = Ad.find_by_ad_group_id(args[:ad_group_id])
  !! _ads.detect{|ad| ad.send(:to_hash) == args[:ad]}
end

.find_by_ad_group_id(id) ⇒ Object

Retrieves a set of ads based on their associated ad group id.

Usage:

Ad.find_by_ad_group_id(1)

Returns:

An array of Ad objects, or an empty array if no ads were found.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/madmen/google/ad.rb', line 92

def self.find_by_ad_group_id(id)
  selector = Google.ad_group_ad_service.module::AdGroupAdSelector.new
  selector.adGroupIds << id
  if response = Google.call_service(:ad_group_ad_service, :get, selector)
    entries = response.rval.entries
    RAILS_DEFAULT_LOGGER.info("MADMEN: Ads from ad group id %d were successfully retrieved." % id)
    @ads = []
    if entries
      entries.map{|entry| entry.ad}.each do |ad|
        @ads << Ad.new(
          :headline => ad.headline,
          :display_url => ad.displayUrl,
          :url => ad.url,
          :description_1 => ad.description1,
          :description_2 => ad.description2,
          :id => ad.id,
          :ad_group_id => id
        )
      end
    end
    return @ads
  else
    return []
  end
end