|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# Encoding: utf-8 |
| 3 | +# |
| 4 | +# Author:: api.dklimkin@gmail.com (Danial Klimkin) |
| 5 | +# |
| 6 | +# Copyright:: Copyright 2011, Google Inc. All Rights Reserved. |
| 7 | +# |
| 8 | +# License:: Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 17 | +# implied. |
| 18 | +# See the License for the specific language governing permissions and |
| 19 | +# limitations under the License. |
| 20 | +# |
| 21 | +# This example gets all alerts for all clients of an MCC account. The effective |
| 22 | +# user (clientCustomerId, or authToken) must be an MCC user to get results. |
| 23 | +# |
| 24 | +# Note: This code example uses MCC-level calls and won't work with Test |
| 25 | +# Accounts, see: https://developers.google.com/adwords/api/docs/test-accounts |
| 26 | +# |
| 27 | +# Tags: AlertService.get |
| 28 | + |
| 29 | +require 'adwords_api' |
| 30 | +require 'adwords_api/utils' |
| 31 | + |
| 32 | +def get_account_alerts() |
| 33 | + # AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml |
| 34 | + # when called without parameters. |
| 35 | + adwords = AdwordsApi::Api.new |
| 36 | + |
| 37 | + # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in |
| 38 | + # the configuration file or provide your own logger: |
| 39 | + # adwords.logger = Logger.new('adwords_xml.log') |
| 40 | + |
| 41 | + alert_srv = adwords.service(:AlertService, API_VERSION) |
| 42 | + |
| 43 | + # Create the selector. |
| 44 | + selector = { |
| 45 | + :query => { |
| 46 | + :filter_spec => 'ALL', |
| 47 | + :client_spec => 'ALL', |
| 48 | + :trigger_time_spec => 'ALL_TIME', |
| 49 | + :severities => ['GREEN', 'YELLOW', 'RED'], |
| 50 | + :types => [ |
| 51 | + 'ACCOUNT_BUDGET_BURN_RATE', 'ACCOUNT_BUDGET_ENDING', |
| 52 | + 'ACCOUNT_ON_TARGET', 'CAMPAIGN_ENDED', 'CAMPAIGN_ENDING', |
| 53 | + 'CREDIT_CARD_EXPIRING', 'DECLINED_PAYMENT', 'MANAGER_LINK_PENDING', |
| 54 | + 'MISSING_BANK_REFERENCE_NUMBER', 'PAYMENT_NOT_ENTERED', |
| 55 | + 'TV_ACCOUNT_BUDGET_ENDING', 'TV_ACCOUNT_ON_TARGET', |
| 56 | + 'TV_ZERO_DAILY_SPENDING_LIMIT', 'USER_INVITE_ACCEPTED', |
| 57 | + 'USER_INVITE_PENDING', 'ZERO_DAILY_SPENDING_LIMIT' |
| 58 | + ] |
| 59 | + }, |
| 60 | + :paging => { |
| 61 | + :start_index => 0, |
| 62 | + :number_results => PAGE_SIZE |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + # Set initial values. |
| 67 | + offset, page = 0, {} |
| 68 | + |
| 69 | + # Get alerts. |
| 70 | + begin |
| 71 | + page = alert_srv.get(selector) |
| 72 | + if page[:entries] |
| 73 | + page[:entries].each_with_index do |alert, index| |
| 74 | + puts "%d) Customer ID is '%s', alert type is '%s', severity is '%s'." |
| 75 | + [AdwordsApi::Utils.format_id(alert[:client_customer_id]), |
| 76 | + alert[:alert_type], alert[:alert_severity]] |
| 77 | + alert[:details].each do |detail| |
| 78 | + puts "\t- triggered at %s" % detail[:trigger_time] |
| 79 | + end |
| 80 | + end |
| 81 | + # Increment values to request the next page. |
| 82 | + offset += PAGE_SIZE |
| 83 | + selector[:paging][:start_index] = offset |
| 84 | + end |
| 85 | + end while page[:total_num_entries] > offset |
| 86 | + |
| 87 | + if page.include?(:total_num_entries) |
| 88 | + puts "\tTotal number of alerts: %d." % page[:total_num_entries] |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +if __FILE__ == $0 |
| 93 | + API_VERSION = :v201406 |
| 94 | + PAGE_SIZE = 500 |
| 95 | + |
| 96 | + begin |
| 97 | + get_account_alerts() |
| 98 | + |
| 99 | + # Authorization error. |
| 100 | + rescue AdsCommon::Errors::OAuth2VerificationRequired => e |
| 101 | + puts "Authorization credentials are not valid. Edit adwords_api.yml for " + |
| 102 | + "OAuth2 client ID and secret and run misc/setup_oauth2.rb example " + |
| 103 | + "to retrieve and store OAuth2 tokens." |
| 104 | + puts "See this wiki page for more details:\n\n " + |
| 105 | + 'http://code.google.com/p/google-api-ads-ruby/wiki/OAuth2' |
| 106 | + |
| 107 | + # HTTP errors. |
| 108 | + rescue AdsCommon::Errors::HttpError => e |
| 109 | + puts "HTTP Error: %s" % e |
| 110 | + |
| 111 | + # API errors. |
| 112 | + rescue AdwordsApi::Errors::ApiException => e |
| 113 | + puts "Message: %s" % e.message |
| 114 | + puts 'Errors:' |
| 115 | + e.errors.each_with_index do |error, index| |
| 116 | + puts "\tError [%d]:" % (index + 1) |
| 117 | + error.each do |field, value| |
| 118 | + puts "\t\t%s: %s" % [field, value] |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | +end |
0 commit comments