forked from googleads/google-ads-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_call.py
More file actions
executable file
·184 lines (161 loc) · 6.65 KB
/
add_call.py
File metadata and controls
executable file
·184 lines (161 loc) · 6.65 KB
1
2
3
4
5
6
7
8
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This example adds a call extension to a specific account."""
import argparse
import sys
from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException
# Country code is a two-letter ISO-3166 code, for a list of all codes see:
# https://developers.google.com/google-ads/api/reference/data/codes-formats#expandable-17
_DEFAULT_PHONE_COUNTRY = "US"
def main(
client, customer_id, phone_number, phone_country, conversion_action_id
):
"""The main method that creates all necessary entities for the example.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
phone_number: a phone number for your business, e.g. '(123) 456-7890'.
phone_country: a two-letter ISO-3166 code.
conversion_action_id: an ID for a conversion action.
"""
asset_resource_name = add_extension_asset(
client, customer_id, phone_number, phone_country, conversion_action_id
)
link_asset_to_account(client, customer_id, asset_resource_name)
def add_extension_asset(
client, customer_id, phone_number, phone_country, conversion_action_id
):
"""Creates a new asset for the call.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
phone_number: a phone number for your business, e.g. '(123) 456-7890'.
phone_country: a two-letter ISO-3166 code.
conversion_action_id: an ID for a conversion action.
Returns:
a resource name for a new call asset.
"""
operation = client.get_type("AssetOperation")
# Creates the call asset.
asset = operation.create.call_asset
asset.country_code = phone_country
asset.phone_number = phone_number
# Optional: Specifies day and time intervals for which the asset may serve.
ad_schedule = client.get_type("AdScheduleInfo")
# Sets the day of this schedule as Monday.
ad_schedule.day_of_week = client.enums.DayOfWeekEnum.MONDAY
# Sets the start hour to 9am.
ad_schedule.start_hour = 9
# Sets the end hour to 5pm.
ad_schedule.end_hour = 17
# Sets the start and end minute of zero, for example: 9:00 and 5:00.
ad_schedule.start_minute = client.enums.MinuteOfHourEnum.ZERO
ad_schedule.end_minute = client.enums.MinuteOfHourEnum.ZERO
# Appends the ad schedule to the list of ad schedule targets on the asset.
asset.ad_schedule_targets.append(ad_schedule)
# Sets the conversion action ID if provided.
if conversion_action_id:
googleads_service = client.get_service("GoogleAdsService")
asset.call_conversion_action = googleads_service.conversion_action_path(
customer_id, conversion_action_id
)
asset.call_conversion_reporting_state = (
client.enums.CallConversionReportingStateEnum.USE_RESOURCE_LEVEL_CALL_CONVERSION_ACTION
)
# Issues a mutate request to add the asset.
asset_service = client.get_service("AssetService")
response = asset_service.mutate_assets(
customer_id=customer_id, operations=[operation]
)
resource_name = response.results[0].resource_name
print(f"Created a call asset with resource name: '{resource_name}'")
return resource_name
def link_asset_to_account(client, customer_id, asset_resource_name):
"""Links the call asset at the account level to serve in eligible campaigns.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID.
asset_resource_name: a resource name for the call asset.
"""
operation = client.get_type("CustomerAssetOperation")
customer_asset = operation.create
customer_asset.asset = asset_resource_name
customer_asset.field_type = client.enums.AssetFieldTypeEnum.CALL
customer_asset_service = client.get_service("CustomerAssetService")
response = customer_asset_service.mutate_customer_assets(
customer_id=customer_id, operations=[operation]
)
resource_name = response.results[0].resource_name
print(f"Created a customer asset with resource name: '{resource_name}'")
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
googleads_client = GoogleAdsClient.load_from_storage(version="v11")
parser = argparse.ArgumentParser(
description=("Adds a call extension to a specific account.")
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
parser.add_argument(
"-n",
"--phone_number",
type=str,
required=True,
help=("A phone number for your business, e.g. '(123) 456-7890'"),
)
parser.add_argument(
"-p",
"--phone_country",
type=str,
default=_DEFAULT_PHONE_COUNTRY,
help=(
"A two-letter ISO-3166 code representing a country code, for a "
"list of all codes see: "
"https://developers.google.com/google-ads/api/reference/data/codes-formats#expandable-17"
),
)
parser.add_argument(
"-v",
"--conversion_action_id",
type=str,
help=("An optional conversion action ID to attribute conversions to."),
)
args = parser.parse_args()
try:
main(
googleads_client,
args.customer_id,
args.phone_number,
args.phone_country,
args.conversion_action_id,
)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'Error with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)