Class: Madmen::Google::Keyword

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Keyword

Create a new Keyword object. Accepts a hash of optional arguments:

id
text
match_type
ad_group_id

Match type must be one of: broad, exact, or phrase



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

def initialize(args)
  @id = args[:id]
  @text = args[:text]
  @match_type = args[:match_type].downcase if args[:match_type]
  @ad_group_id = args[:ad_group_id]
end

Instance Attribute Details

#ad_group_idObject

Returns the value of attribute ad_group_id.



8
9
10
# File 'lib/madmen/google/keyword.rb', line 8

def ad_group_id
  @ad_group_id
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/madmen/google/keyword.rb', line 5

def id
  @id
end

#match_typeObject

Returns the value of attribute match_type.



6
7
8
# File 'lib/madmen/google/keyword.rb', line 6

def match_type
  @match_type
end

#textObject

Returns the value of attribute text.



7
8
9
# File 'lib/madmen/google/keyword.rb', line 7

def text
  @text
end

Class Method Details

.create(args) ⇒ Object

Creates a new keyword in the specified ad group.

Usage:

Keyword.create(:ad_group_id => 1, :text => 'foo', :match_type => 'broad')


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/madmen/google/keyword.rb', line 18

def self.create(args)
  return false if Keyword.exists?(args)
  operation = Google.ad_group_criterion_service.module::AdGroupCriterionOperation.new
  operand = Google.ad_group_criterion_service.module::BiddableAdGroupCriterion.new
  operand.adGroupId = args[:ad_group_id]
  keyword = Google.ad_group_criterion_service.module::Keyword.new
  keyword.matchType = args[:match_type].upcase
  keyword.text = args[:text]
  operand.criterion = keyword
  operation.operand = operand
  operation.operator = 'ADD'
  operation.exemptionRequests = []

  if response = Google.call_service(:ad_group_criterion_service, :mutate, [operation])
    criterion = response.rval.value.first.criterion
    keyword = Keyword.new(:id => criterion.id, :text => criterion.text, :match_type => criterion.matchType)
    RAILS_DEFAULT_LOGGER.info 'MADMEN: Keyword id %d was successfully created.' % keyword.id
    return keyword
  else
    return false
  end
end

.exists?(args) ⇒ Boolean

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

Usage:

Keyword.exists?(:ad_group_id => 1, :text => 'foo', :match_type =>'exact')

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/madmen/google/keyword.rb', line 47

def self.exists?(args)
  _keywords = Keyword.find_by_ad_group_id(args[:ad_group_id])
  return false if _keywords.blank?
  !! _keywords.detect{|keyword| keyword.text == args[:text] && keyword.match_type.upcase == args[:match_type].upcase}
end

.find_by_ad_group_id(id) ⇒ Object

Retrieves a set of keywords from the specified ad group.

Usage:

Keyword.find_by_ad_group_id(1)

Returns:

An array of Keyword objects, or an empty array if no keywords were found.

Raises:

  • (ArgumentError)


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

def self.find_by_ad_group_id(id)
  raise ArgumentError, 'Cannot find keywords without an ad group ID!' unless id
  selector = Google.ad_group_criterion_service.module::AdGroupCriterionSelector.new
  filter = Google.ad_group_criterion_service.module::AdGroupCriterionIdFilter.new
  filter.adGroupId = id
  selector.idFilters = filter
  
  if response = Google.call_service(:ad_group_criterion_service, :get, selector)
    entries = response.rval.entries
    RAILS_DEFAULT_LOGGER.info("MADMEN: Keywords from ad group id %d were successfully retrieved." % id)
    keywords = []
    keywords = entries.map{|entry| entry.criterion}.inject([]){|keywords, keyword| keywords << Keyword.new(:text => keyword.text, :match_type => keyword.matchType, :id => keyword.id, :ad_group_id => id); keywords } if entries
    return keywords
  else
    return []
  end
end

Instance Method Details

#deleteObject

Deletes a keyword from its ad group.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/madmen/google/keyword.rb', line 100

def delete
  operation = Google.ad_group_criterion_service.module::AdGroupCriterionOperation.new
  operand = Google.ad_group_criterion_service.module::AdGroupCriterion.new
  keyword = Google.ad_group_criterion_service.module::Keyword.new
  keyword.id = self.id
  keyword.text = self.text
  keyword.matchType = self.match_type.upcase
  operand.adGroupId = self.ad_group_id
  operand.criterion = keyword
  operation.operand = operand
  operation.operator = 'REMOVE'
  
  if response = Google.call_service(:ad_group_criterion_service, :mutate, [operation])
    RAILS_DEFAULT_LOGGER.info 'MADMEN: Keyword id %d was successfully removed.' % self.id
    return true
  else
    return false
  end
end