|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2021 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +"""Adds a channel-level seasonality adjustment for Smart Bidding. |
| 16 | +
|
| 17 | +The adjustment changes Smart Bidding behavior by the expected change in |
| 18 | +conversion rate for the given future time interval. |
| 19 | +
|
| 20 | +For more information on using seasonality adjustments, see: |
| 21 | +https://developers.google.com/google-ads/api/docs/campaigns/bidding/seasonality-adjustments |
| 22 | +""" |
| 23 | + |
| 24 | + |
| 25 | +import argparse |
| 26 | +import sys |
| 27 | +from uuid import uuid4 |
| 28 | + |
| 29 | +from google.ads.googleads.client import GoogleAdsClient |
| 30 | +from google.ads.googleads.errors import GoogleAdsException |
| 31 | + |
| 32 | + |
| 33 | +def main( |
| 34 | + client, |
| 35 | + customer_id, |
| 36 | + start_date_time, |
| 37 | + end_date_time, |
| 38 | + conversion_rate_modifier, |
| 39 | +): |
| 40 | + """The main method that creates all necessary entities for the example. |
| 41 | +
|
| 42 | + Args: |
| 43 | + client: an initialized GoogleAdsClient instance. |
| 44 | + customer_id: a client customer ID. |
| 45 | + start_date_time: a str of the start date for the exclusion period. |
| 46 | + end_date_time: a str of the end date for the exclusion period. |
| 47 | + conversion_rate_modifier: the modifier to apply to conversions during |
| 48 | + the given time period. |
| 49 | + """ |
| 50 | + # [START add_bidding_seasonality_adjustment] |
| 51 | + bidding_seasonality_adjustment_service = client.get_service( |
| 52 | + "BiddingSeasonalityAdjustmentService" |
| 53 | + ) |
| 54 | + operation = client.get_type("BiddingSeasonalityAdjustmentOperation") |
| 55 | + bidding_seasonality_adjustment = operation.create |
| 56 | + # A unique name is required for every seasonality adjustment. |
| 57 | + bidding_seasonality_adjustment.name = f"Seasonality adjustment #{uuid4()}" |
| 58 | + # The CHANNEL scope applies the conversion_rate_modifier to all campaigns of |
| 59 | + # specific advertising channel types. In this example, the |
| 60 | + # conversion_rate_modifier will only apply to Search campaigns. Use the |
| 61 | + # CAMPAIGN scope to instead limit the scope to specific campaigns. |
| 62 | + bidding_seasonality_adjustment.scope = ( |
| 63 | + client.enums.SeasonalityEventScopeEnum.CHANNEL |
| 64 | + ) |
| 65 | + bidding_seasonality_adjustment.advertising_channel_types.append( |
| 66 | + client.enums.AdvertisingChannelTypeEnum.SEARCH |
| 67 | + ) |
| 68 | + # If setting scope CAMPAIGN, add individual campaign resource name(s) |
| 69 | + # according to the commented out line below. |
| 70 | + # |
| 71 | + # bidding_seasonality_adjustment.campaigns.append( |
| 72 | + # "INSERT_CAMPAIGN_RESOURCE_NAME_HERE" |
| 73 | + # ) |
| 74 | + |
| 75 | + bidding_seasonality_adjustment.start_date_time = start_date_time |
| 76 | + bidding_seasonality_adjustment.end_date_time = end_date_time |
| 77 | + # The conversion_rate_modifier is the expected future conversion rate |
| 78 | + # change. When this field is unset or set to 1.0, no adjustment will be |
| 79 | + # applied to traffic. The allowed range is 0.1 to 10.0. |
| 80 | + bidding_seasonality_adjustment.conversion_rate_modifier = ( |
| 81 | + conversion_rate_modifier |
| 82 | + ) |
| 83 | + |
| 84 | + response = bidding_seasonality_adjustment_service.mutate_bidding_seasonality_adjustments( |
| 85 | + customer_id=customer_id, operations=[operation] |
| 86 | + ) |
| 87 | + |
| 88 | + resource_name = response.results[0].resource_name |
| 89 | + |
| 90 | + print(f"Added seasonality adjustment with resource name: '{resource_name}'") |
| 91 | + # [END add_bidding_seasonality_adjustment] |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 96 | + # home directory if none is specified. |
| 97 | + googleads_client = GoogleAdsClient.load_from_storage(version="v8") |
| 98 | + |
| 99 | + parser = argparse.ArgumentParser( |
| 100 | + description="Adds a seasonality adjustment for conversions in Smart " |
| 101 | + "Bidding for the given time interval." |
| 102 | + ) |
| 103 | + # The following argument(s) should be provided to run the example. |
| 104 | + parser.add_argument( |
| 105 | + "-c", |
| 106 | + "--customer_id", |
| 107 | + type=str, |
| 108 | + required=True, |
| 109 | + help="The Google Ads customer ID.", |
| 110 | + ) |
| 111 | + parser.add_argument( |
| 112 | + "-s", |
| 113 | + "--start_date_time", |
| 114 | + type=str, |
| 115 | + required=True, |
| 116 | + help="The start date for the adjustment period, must be in the format: " |
| 117 | + "'yyyy-MM-dd HH:mm:ss'.", |
| 118 | + ) |
| 119 | + parser.add_argument( |
| 120 | + "-e", |
| 121 | + "--end_date_time", |
| 122 | + type=str, |
| 123 | + required=True, |
| 124 | + help="The end date for the adjustment period, must be in the format: " |
| 125 | + "'yyyy-MM-dd HH:mm:ss'.", |
| 126 | + ) |
| 127 | + parser.add_argument( |
| 128 | + "-m", |
| 129 | + "--conversion_rate_modifier", |
| 130 | + type=float, |
| 131 | + required=True, |
| 132 | + help="The conversion rate modifier that will be applied during the " |
| 133 | + "adjustment period. This value must be in the range 0.1 to 10.0.", |
| 134 | + ) |
| 135 | + |
| 136 | + args = parser.parse_args() |
| 137 | + |
| 138 | + try: |
| 139 | + main( |
| 140 | + googleads_client, |
| 141 | + args.customer_id, |
| 142 | + args.start_date_time, |
| 143 | + args.end_date_time, |
| 144 | + args.conversion_rate_modifier, |
| 145 | + ) |
| 146 | + except GoogleAdsException as ex: |
| 147 | + print( |
| 148 | + f'Request with ID "{ex.request_id}" failed with status ' |
| 149 | + f'"{ex.error.code().name}" and includes the following errors:' |
| 150 | + ) |
| 151 | + for error in ex.failure.errors: |
| 152 | + print(f'Error with message "{error.message}".') |
| 153 | + if error.location: |
| 154 | + for field_path_element in error.location.field_path_elements: |
| 155 | + print(f"\t\tOn field: {field_path_element.field_name}") |
| 156 | + sys.exit(1) |
0 commit comments