/
add_prices.py
executable file
·157 lines (134 loc) · 5.38 KB
/
add_prices.py
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
#!/usr/bin/env python
#
# Copyright 2016 Google Inc. All Rights Reserved.
#
# 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
#
# http://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.
"""Adds a price extension and associates it with an account.
Campaign targeting is also set usin the specified campaign ID. To get campaigns,
run get_campaigns.py.
The LoadFromStorage method is pulling credentials and properties from a
"googleads.yaml" file. By default, it looks for this file in your home
directory. For more information, see the "Caching authentication information"
section of our README.
"""
from googleads import adwords
from googleads import errors
CAMPAIGN_ID = 'INSERT_CAMPAIGN_ID_HERE'
MICROS_PER_DOLLAR = 1000000
def main(client, campaign_id):
# Initialize appropriate service.
customer_extension_setting_service = client.GetService(
'CustomerExtensionSettingService', 'v201809')
# To create a price extension, at least three table rows are needed.
table_rows = [
CreatePriceTableRow('Scrubs', 'Body Scrub, Salt Scrub',
'https://www.example.com/scrubs',
60 * MICROS_PER_DOLLAR, 'USD', 'PER_HOUR',
final_mobile_url='http://m.example.com/scrubs'),
CreatePriceTableRow('Hair Cuts', 'Once a month',
'https://www.example.com/haircuts',
75 * MICROS_PER_DOLLAR, 'USD', 'PER_MONTH',
final_mobile_url='http://m.example.com/haircuts'),
CreatePriceTableRow('Skin Care Package', 'Four times a month',
'https://www.examples.com/skincarepackage',
250 * MICROS_PER_DOLLAR, 'USD', 'PER_MONTH',
final_mobile_url=(
'http://m.example.com/skincarepackage'))
]
# Create the price extension feed item.
customer_extension_setting = {
'extensionType': 'PRICE',
'extensionSetting': {
'extensions': [{
'priceExtensionType': 'SERVICES',
'trackingUrlTemplate': 'http://tracker.example.com/?u={lpurl}',
'language': 'en',
'campaignTargeting': {
'TargetingCampaignId': campaign_id
},
'scheduling': {
'feedItemSchedules': [
{
'dayOfWeek': 'SATURDAY',
'startHour': 10,
'startMinute': 'ZERO',
'endHour': 22,
'endMinute': 'ZERO'
},
{
'dayOfWeek': 'SUNDAY',
'startHour': 10,
'startMinute': 'ZERO',
'endHour': 18,
'endMinute': 'ZERO'
}
]
},
'tableRows': table_rows,
# Price qualifier is optional.
'priceQualifier': 'FROM',
'xsi_type': 'PriceFeedItem'
}]
}
}
# Create an operation to add the feed.
operations = [{
'operator': 'ADD',
'operand': customer_extension_setting
}]
# Add the price extension.
response = customer_extension_setting_service.mutate(operations)
# Print the results.
if 'value' in response:
print ('Extension setting with type "%s" was added to your account.'
% response['value'][0]['extensionType'])
else:
raise errors.GoogleAdsError('No extension settings were added.')
def CreatePriceTableRow(header, description, final_url, price_in_micros,
currency_code, price_unit, final_mobile_url=None):
"""Helper function to generate a single row of a price table.
Args:
header: A str containing the header text of this row.
description: A str description of this row in the price table.
final_url: A str containing the final URL after all cross domain redirects.
price_in_micros: An int indicating the price of the given currency in
micros.
currency_code: A str indicating the currency code being used.
price_unit: A str enum indicating the price unit for this row.
final_mobile_url: A str containing the final mobile URL after all cross
domain redirects.
Returns:
A dictionary containing the contents of the generated price table row.
"""
table_row = {
'header': header,
'description': description,
'finalUrls': {'urls': [final_url]},
'price': {
'money': {
'microAmount': price_in_micros,
},
'currencyCode': currency_code
},
'priceUnit': price_unit,
'xsi_type': 'PriceTableRow'
}
if final_mobile_url:
table_row['finalMobileUrls'] = {
'urls': [final_mobile_url]
}
return table_row
if __name__ == '__main__':
# Initialize client object.
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client, CAMPAIGN_ID)