Class: Embulk::Input::GoogleAdwords

Inherits:
InputPlugin
  • Object
show all
Defined in:
lib/embulk/input/google_adwords.rb

Constant Summary collapse

API_VERSION =
:v201809

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.resume(task, columns, count, &control) ⇒ Object



57
58
59
60
61
62
# File 'lib/embulk/input/google_adwords.rb', line 57

def self.resume(task, columns, count, &control)
  task_reports = yield(task, columns, count)

  next_config_diff = {}
  return next_config_diff
end

.transaction(config, &control) ⇒ Object

Raises:

  • (ConfigError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/embulk/input/google_adwords.rb', line 9

def self.transaction(config, &control)
  # configuration code:
  task = {
    "adwords_api_options" => {
      "authentication" => {
        "method" => config.param("auth_method", :string),
        "oauth2_client_id" => config.param("auth_oauth2_client_id", :string),
        "oauth2_client_secret" => config.param("auth_oauth2_client_secret", :string),
        "developer_token" => config.param("auth_developer_token", :string),
        "client_customer_id" => config.param("auth_client_customer_id", :string),
        "user_agent" => config.param("auth_user_agent", :string),
        "oauth2_token" => {
          "access_token" => config.param("oauth2_access_token", :string),
          "refresh_token" => config.param("oauth2_refresh_token", :string),
          "issued_at" => config.param("oauth2_issued_at", :string),
          "expires_in" => config.param("oauth2_expires_in", :string),
          "id_token" => ""
        }
      },
      "service" => {
        "environment" => "PRODUCTION"
      },
      "connection" => {
        "enable_gzip" => false
      },
      "library" => {
        "log_level" => config.param("log_level", :string, default: "INFO"),
        "skip_report_header" => true,
        "skip_column_header" => true,
        "skip_report_summary" => true
      }
    },
    "report_type" => config.param("report_type", :string),
    "fields" => config.param("fields", :array),
    "conditions" => config.param("conditions", :array, default: []),
    "daterange" => config.param("daterange", :string, default: "")
  }

  raise ConfigError.new("The parameter report_type must not be empty.") if task["report_type"].empty?
  raise ConfigError.new("The parameter fields must not be empty array.") if task["fields"].empty?

  columns = task["fields"].map do |col_name|
    Column.new(nil, col_name, :string)
  end

  resume(task, columns, 1, &control)
end

Instance Method Details

#initObject

TODO def self.guess(config)

sample_records = [
  {"example"=>"a", "column"=>1, "value"=>0.1},
  {"example"=>"a", "column"=>2, "value"=>0.2},
]
columns = Guess::SchemaGuess.from_hash_records(sample_records)
return {"columns" => columns}

end



74
75
76
# File 'lib/embulk/input/google_adwords.rb', line 74

def init
  # initialization code:
end

#query_report_results(query, &block) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/embulk/input/google_adwords.rb', line 112

def query_report_results(query, &block)
  # AdwordsApi::Api
  adwords = AdwordsApi::Api.new(task["adwords_api_options"])

  # Get report utilities for the version.
  report_utils = adwords.report_utils(API_VERSION)

  # Allowing rows with zero impressions to show is not supported with AWQL.
  adwords.include_zero_impressions = false

  report_utils.get_stream_helper_with_awql(query, 'TSV').each_line do |line|
    row = line.split("\t")
    block.call row
  end
end

#runObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/embulk/input/google_adwords.rb', line 78

def run
  selectors = task["fields"].join(", ")
  conditions = task["conditions"].join(" AND ")

  query = "SELECT " + selectors + " FROM " + task["report_type"]
  query << " WHERE " + conditions unless conditions.empty?
  query << " DURING " + task["daterange"] unless task["daterange"].empty?

  begin
    query_report_results(query) do |row|
      page_builder.add row
    end

  # Authorization error.
  rescue AdsCommon::Errors::OAuth2VerificationRequired => e
    raise ConfigError.new(e.message)

  # HTTP errors.
  rescue AdsCommon::Errors::HttpError => e
    raise ConfigError.new(e.message)

  # API errors.
  rescue AdwordsApi::Errors::ReportError => e
    raise ConfigError.new(e.message)
  end

  page_builder.finish

  task_report = {}
  return task_report
end