-
Notifications
You must be signed in to change notification settings - Fork 974
/
add_rule_based_user_lists.py
executable file
·235 lines (201 loc) · 7.29 KB
/
add_rule_based_user_lists.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/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.
"""This example adds two rule-based remarketing user lists.
Adds two rule-based remarketing user lists; one with no site visit date
restrictions and another that will only include users who visit your site in
the next six months.
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.
"""
import calendar
from datetime import date
from datetime import datetime
from datetime import timedelta
from googleads import adwords
def main(client):
# Initialize appropriate service.
adwords_user_list_service = client.GetService(
'AdwordsUserListService', version='v201802')
# First rule item group - users who visited the checkout page and had more
# than one item in their shopping cart.
checkout_rule_item = {
'StringRuleItem': {
'key': {
'name': 'ecomm_pagetype'
},
'op': 'EQUALS',
'value': 'checkout'
}
}
cart_size_rule_item = {
'NumberRuleItem': {
'key': {
'name': 'cartsize'
},
'op': 'GREATER_THAN',
'value': '1.0'
}
}
# Combine the two rule items into a RuleItemGroup so AdWords will logically
# AND the rules together.
checkout_multiple_item_group = {
'items': [checkout_rule_item, cart_size_rule_item]
}
# Second rule item group - users who checked out within the next 3 months.
today = date.today()
start_date_rule_item = {
'DateRuleItem': {
'key': {
'name': 'checkoutdate'
},
'op': 'AFTER',
'value': today.strftime('%Y%m%d')
}
}
three_months_later = AddMonths(today, 3)
three_months_later_rule_item = {
'DateRuleItem': {
'key': {
'name': 'checkoutdate'
},
'op': 'BEFORE',
'value': three_months_later.strftime('%Y%m%d')
}
}
# Combine the date rule items into a RuleItemGroup
checked_out_date_range_item_group = {
'items': [start_date_rule_item, three_months_later_rule_item]
}
# Combine the rule item groups into a Rule so AdWords knows how to apply the
# rules.
rule = {
'groups': [
checkout_multiple_item_group,
checked_out_date_range_item_group
],
# ExpressionRuleUserLists can use either CNF or DNF for matching. CNF
# means 'at least one item in each rule item group must match', and DNF
# means 'at least one entire rule item group must match'.
# DateSpecificRuleUserList only supports DNF. You can also omit the rule
# type altogether to default to DNF.
'ruleType': 'DNF'
}
# Third and fourth rule item groups.
# Visitors of a page who visited another page.
site1_rule_item = {
'StringRuleItem': {
'key': {'name': 'url__'},
'op': 'EQUALS',
'value': 'example.com/example1'
}
}
site2_rule_item = {
'StringRuleItem': {
'key': {'name': 'url__'},
'op': 'EQUALS',
'value': 'example.com/example2'
}
}
# Create two rules to show that a visitor browsed two sites.
user_visited_site1_rule = {
'groups': [{
'items': [site1_rule_item]
}]
}
user_visited_site2_rule = {
'groups': [{
'items': [site2_rule_item]
}]
}
# Create the user list with no restrictions on site visit date.
expression_user_list = {
'xsi_type': 'ExpressionRuleUserList',
'name': 'Expression-based user list created at %s'
% datetime.today().strftime('%Y%m%d %H:%M:%S'),
'description': 'Users who checked out in three month window OR visited'
' the checkout page with more than one item in their'
' cart.',
'rule': rule,
# Optional: Set the populationStatus to REQUESTED to include past users in
# the user list.
'prepopulationStatus': 'REQUESTED'
}
# Create the user list restricted to users who visit your site within the next
# six months.
end_date = AddMonths(today, 6)
date_user_list = {
'xsi_type': 'DateSpecificRuleUserList',
'name': 'Date rule user list created at %s'
% datetime.today().strftime('%Y%m%d %H:%M:%S'),
'description': 'Users who visited the site between %s and %s and checked'
' out in three month window OR visited the checkout page'
' with more than one item in their cart.'
% (today.strftime('%Y%m%d'), end_date.strftime('%Y%m%d')),
'rule': rule,
'startDate': today.strftime('%Y%m%d'),
'endDate': end_date.strftime('%Y%m%d')
}
# Create the user list for "Visitors of a page who did visit another page".
# To create a user list for "Visitors of a page who did not visit another
# page", change the ruleOperator from AND to AND_NOT.
combined_user_list = {
'xsi_type': 'CombinedRuleUserList',
'name': 'Combined rule user lst create at ${creation_time}',
'description': 'Users who visited two sites.',
'leftOperand': user_visited_site1_rule,
'rightOperand': user_visited_site2_rule,
'ruleOperator': 'AND'
}
# Create operations to add the user lists.
operations = [
{
'operand': user_list,
'operator': 'ADD',
} for user_list in [expression_user_list, date_user_list,
combined_user_list]
]
# Submit the operations.
user_lists = adwords_user_list_service.mutate(operations)
# Display results.
for user_list in user_lists['value']:
print (('User list added with ID %d, name "%s", status "%s", list type'
' "%s", accountUserListStatus "%s", description "%s".') %
(user_list['id'], user_list['name'],
user_list['status'], user_list['listType'],
user_list['accountUserListStatus'], user_list['description']))
def AddMonths(start_date, months):
"""A simple convenience utility for adding months to a given start date.
This increments the months by adding the number of days in the current month
to the current month, for each month.
Args:
start_date: date The date months are being added to.
months: int The number of months to add.
Returns:
A date equal to the start date incremented by the given number of months.
"""
current_date = start_date
i = 0
while i < months:
month_days = calendar.monthrange(current_date.year, current_date.month)[1]
current_date += timedelta(days=month_days)
i += 1
return current_date
if __name__ == '__main__':
# Initialize client object.
adwords_client = adwords.AdWordsClient.LoadFromStorage()
main(adwords_client)