Module: Zuck::AdKeyword

Extended by:
AdKeyword, Helpers
Included in:
AdKeyword
Defined in:
lib/zuck/facebook/ad_keyword.rb

Instance Method Summary collapse

Methods included from Helpers

normalize_array, normalize_countries

Instance Method Details

#best_guess(graph, keyword) ⇒ Object

Given a keyword, make a guess on what the closest keyword on the facebook ads api is. It tends to use a # prefixed keyword if available, and also a more popular one over a less popular one



10
11
12
13
14
15
16
17
18
# File 'lib/zuck/facebook/ad_keyword.rb', line 10

def best_guess(graph, keyword)
  search(graph, keyword).sort do |a,b|
    if a[:audience].to_i > 0 || b[:audience].to_i > 0
      a[:audience].to_i <=> b[:audience].to_i
    else
      b[:keyword].length <=> a[:keyword].length
    end
  end.last
end

#search(graph, keyword) ⇒ Object

Ad keyword search



33
34
35
36
37
38
39
40
41
42
# File 'lib/zuck/facebook/ad_keyword.rb', line 33

def search(graph, keyword)
  results = graph.search(keyword, type: :adkeyword).map do |r|
    audience = r['description'].scan(/[0-9]+/).join('').to_i rescue nil
    {
      keyword: r['name'],
      id:   r['id'],
      audience: audience
    }
  end
end

#validate(graph, keywords) ⇒ Hash

Checks the ad api to see if the given keywords are valid

Returns:

  • (Hash)

    The keys are the (lowercased) keywords and the values their validity



22
23
24
25
26
27
28
29
30
# File 'lib/zuck/facebook/ad_keyword.rb', line 22

def validate(graph, keywords)
  keywords = normalize_array(keywords).map{|k| k.gsub(',', '%2C')}
  search = graph.search(nil, type: 'adkeywordvalid', keyword_list: keywords.join(","))
  results = {}
  search.each do |r|
    results[r['name']] = r['valid']
  end
  results
end