|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# Encoding: utf-8 |
| 3 | +# |
| 4 | +# Author:: api.davidtorres@gmail.com (David Torres) |
| 5 | +# |
| 6 | +# Copyright:: Copyright 2013, 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 creates new activity groups. To determine which activity groups |
| 22 | +# exist, run get_all_activity_groups.rb. |
| 23 | +# |
| 24 | +# Tags: ActivityGroupService.createActivityGroups |
| 25 | + |
| 26 | +require 'dfp_api' |
| 27 | + |
| 28 | +API_VERSION = :v201311 |
| 29 | + |
| 30 | +def create_activity_groups() |
| 31 | + # Get DfpApi instance and load configuration from ~/dfp_api.yml. |
| 32 | + dfp = DfpApi::Api.new |
| 33 | + |
| 34 | + # To enable logging of SOAP requests, set the log_level value to 'DEBUG' in |
| 35 | + # the configuration file or provide your own logger: |
| 36 | + # dfp.logger = Logger.new('dfp_xml.log') |
| 37 | + |
| 38 | + # Get the ActivityGroupService. |
| 39 | + activity_group_service = dfp.service(:ActivityGroupService, API_VERSION) |
| 40 | + |
| 41 | + # Set the ID of the advertiser company this activity group is associated |
| 42 | + # with. |
| 43 | + advertiser_company_id = 'INSERT_ADVERTISER_COMPANY_ID_HERE'; |
| 44 | + |
| 45 | + # Create a short-term activity group. |
| 46 | + short_term_activity_group = { |
| 47 | + :name => 'Short-term activity group', |
| 48 | + :company_ids => [advertiser_company_id], |
| 49 | + :clicks_lookback => 1, |
| 50 | + :impressions_lookback => 1 |
| 51 | + } |
| 52 | + |
| 53 | + # Create a long-term activity group. |
| 54 | + long_term_activity_group = { |
| 55 | + :name => 'Long-term activity group', |
| 56 | + :company_ids => [advertiser_company_id], |
| 57 | + :clicks_lookback => 30, |
| 58 | + :impressions_lookback => 30 |
| 59 | + } |
| 60 | + |
| 61 | + # Create the activity groups on the server. |
| 62 | + return_activity_groups = activity_group_service.create_activity_groups([ |
| 63 | + short_term_activity_group, long_term_activity_group]) |
| 64 | + |
| 65 | + if return_activity_groups |
| 66 | + return_activity_groups.each do |activity_group| |
| 67 | + puts "An activity group with ID: %d and name: %s was created." % |
| 68 | + [activity_group[:id], activity_group[:name]] |
| 69 | + end |
| 70 | + else |
| 71 | + raise 'No activity groups were created.' |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +if __FILE__ == $0 |
| 76 | + begin |
| 77 | + create_activity_groups() |
| 78 | + |
| 79 | + # HTTP errors. |
| 80 | + rescue AdsCommon::Errors::HttpError => e |
| 81 | + puts "HTTP Error: %s" % e |
| 82 | + |
| 83 | + # API errors. |
| 84 | + rescue DfpApi::Errors::ApiException => e |
| 85 | + puts "Message: %s" % e.message |
| 86 | + puts 'Errors:' |
| 87 | + e.errors.each_with_index do |error, index| |
| 88 | + puts "\tError [%d]:" % (index + 1) |
| 89 | + error.each do |field, value| |
| 90 | + puts "\t\t%s: %s" % [field, value] |
| 91 | + end |
| 92 | + end |
| 93 | + end |
| 94 | +end |
0 commit comments