-
Notifications
You must be signed in to change notification settings - Fork 4
/
targeted_pmax_criteria_view.tf
392 lines (387 loc) · 20 KB
/
targeted_pmax_criteria_view.tf
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# Copyright 2023 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
#
# 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.
# Creates a snapshot of standard shopping criteria view.
#
# The view parse the adgroup criteria into multiple columns that will used to join with the GMC data
# to find the targeted products.
resource "google_bigquery_job" "pmax_criteria_view" {
depends_on = [google_bigquery_table.geo_targets_table]
for_each = { for pair in var.accounts_table : pair.mc => pair }
job_id = "pmax_criteria_view_${each.value.gads}_${each.value.mc}_${random_id.id.hex}"
location = var.zombies_data_location
query {
create_disposition = ""
write_disposition = ""
query = <<EOF
# Copyright 2023 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
#
# 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.
# Creates a snapshot of PMax shopping criteria view.
#
# The view parse the asset_group_listing_group_filter into multiple columns that will used to join
# with the GMC data to find the targeted products.
CREATE OR REPLACE VIEW `${var.gcp_project}.${var.zombies_dataset_name}.pmax_criteria_view_${each.value.gads}`
AS (
# Use recursive join to find all the ancestors.
WITH RECURSIVE
# Get all the criteria.
AssetGroupListingGroupFilters AS (
SELECT DISTINCT
_DATA_DATE,
_LATEST_DATE,
CAST(asset_group_listing_group_filter_id AS INT64) AS listing_group_filter_id,
CAST(
SPLIT(
asset_group_listing_group_filter_parent_listing_group_filter,
'~')[
SAFE_OFFSET(1)]
AS INT64) AS parent_listing_group_filter_id,
CAST(
SPLIT(
asset_group_listing_group_filter_asset_group,
'/')[
SAFE_OFFSET(3)]
AS INT64) AS asset_group_id,
asset_group_listing_group_filter_type,
IF(
asset_group_listing_group_filter_case_value_product_custom_attribute_index = 'INDEX0',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_custom_attribute_value)),
NULL) AS custom_label0,
IF(
asset_group_listing_group_filter_case_value_product_custom_attribute_index = 'INDEX1',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_custom_attribute_value)),
NULL) AS custom_label1,
IF(
asset_group_listing_group_filter_case_value_product_custom_attribute_index = 'INDEX2',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_custom_attribute_value)),
NULL) AS custom_label2,
IF(
asset_group_listing_group_filter_case_value_product_custom_attribute_index = 'INDEX3',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_custom_attribute_value)),
NULL) AS custom_label3,
IF(
asset_group_listing_group_filter_case_value_product_custom_attribute_index = 'INDEX4',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_custom_attribute_value)),
NULL) AS custom_label4,
IF(
asset_group_listing_group_filter_case_value_product_type_level = 'LEVEL1',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_type_value)),
NULL) AS product_type_l1,
IF(
asset_group_listing_group_filter_case_value_product_type_level = 'LEVEL2',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_type_value)),
NULL) AS product_type_l2,
IF(
asset_group_listing_group_filter_case_value_product_type_level = 'LEVEL3',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_type_value)),
NULL) AS product_type_l3,
IF(
asset_group_listing_group_filter_case_value_product_type_level = 'LEVEL4',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_type_value)),
NULL) AS product_type_l4,
IF(
asset_group_listing_group_filter_case_value_product_type_level = 'LEVEL5',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_type_value)),
NULL) AS product_type_l5,
IF(
asset_group_listing_group_filter_case_value_product_category_level = 'LEVEL1',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_category_category_id)),
NULL) AS google_product_category_l1,
IF(
asset_group_listing_group_filter_case_value_product_category_level = 'LEVEL2',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_category_category_id)),
NULL) AS google_product_category_l2,
IF(
asset_group_listing_group_filter_case_value_product_category_level = 'LEVEL3',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_category_category_id)),
NULL) AS google_product_category_l3,
IF(
asset_group_listing_group_filter_case_value_product_category_level = 'LEVEL4',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_category_category_id)),
NULL) AS google_product_category_l4,
IF(
asset_group_listing_group_filter_case_value_product_category_level = 'LEVEL5',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_category_category_id)),
NULL) AS google_product_category_l5,
IF(
asset_group_listing_group_filter_case_value_product_channel_channel != 'UNSPECIFIED',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_channel_channel)),
NULL) AS channel,
IF(
asset_group_listing_group_filter_case_value_product_condition_condition != 'UNSPECIFIED',
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_condition_condition)),
NULL) AS condition,
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_brand_value)) AS brand,
TRIM(LOWER(asset_group_listing_group_filter_case_value_product_item_id_value)) AS offer_id
FROM
`${var.gcp_merchant_and_gads_dataset_project}.${var.gads_dataset_name}.ads_AssetGroupListingGroupFilter_${each.value.gads}`
),
# Aggregates the criteria to be used for "Everything else" by grouping the same parent. At this
# point, we only know the sibling (same parent), not the grandparent criteria.
AggregatedCriteria AS (
SELECT
_DATA_DATE,
_LATEST_DATE,
asset_group_id,
parent_listing_group_filter_id,
ARRAY_AGG(DISTINCT custom_label0 IGNORE NULLS) AS agg_custom_label0,
ARRAY_AGG(DISTINCT custom_label1 IGNORE NULLS) AS agg_custom_label1,
ARRAY_AGG(DISTINCT custom_label2 IGNORE NULLS) AS agg_custom_label2,
ARRAY_AGG(DISTINCT custom_label3 IGNORE NULLS) AS agg_custom_label3,
ARRAY_AGG(DISTINCT custom_label4 IGNORE NULLS) AS agg_custom_label4,
ARRAY_AGG(DISTINCT product_type_l1 IGNORE NULLS) AS agg_product_type_l1,
ARRAY_AGG(DISTINCT product_type_l2 IGNORE NULLS) AS agg_product_type_l2,
ARRAY_AGG(DISTINCT product_type_l3 IGNORE NULLS) AS agg_product_type_l3,
ARRAY_AGG(DISTINCT product_type_l4 IGNORE NULLS) AS agg_product_type_l4,
ARRAY_AGG(DISTINCT product_type_l5 IGNORE NULLS) AS agg_product_type_l5,
ARRAY_AGG(DISTINCT google_product_category_l1 IGNORE NULLS)
AS agg_google_product_category_l1,
ARRAY_AGG(DISTINCT google_product_category_l2 IGNORE NULLS)
AS agg_google_product_category_l2,
ARRAY_AGG(DISTINCT google_product_category_l3 IGNORE NULLS)
AS agg_google_product_category_l3,
ARRAY_AGG(DISTINCT google_product_category_l4 IGNORE NULLS)
AS agg_google_product_category_l4,
ARRAY_AGG(DISTINCT google_product_category_l5 IGNORE NULLS)
AS agg_google_product_category_l5,
ARRAY_AGG(DISTINCT channel IGNORE NULLS) AS agg_channel,
ARRAY_AGG(DISTINCT condition IGNORE NULLS) AS agg_condition,
ARRAY_AGG(DISTINCT brand IGNORE NULLS) AS agg_brand,
ARRAY_AGG(DISTINCT offer_id IGNORE NULLS) AS agg_offer_id
FROM
AssetGroupListingGroupFilters
GROUP BY
1, 2, 3, 4
),
# Find the active asset group
AssetGroups AS (
SELECT DISTINCT
_DATA_DATE,
_LATEST_DATE,
CAST(asset_group_id AS INT64) AS asset_group_id,
CAST(SPLIT(asset_group_campaign, '/')[OFFSET(3)] AS INT64) AS campaign_id
FROM
`${var.gcp_merchant_and_gads_dataset_project}.${var.gads_dataset_name}.ads_AssetGroup_${each.value.gads}`
WHERE
asset_group_status = 'ENABLED'
),
# Find the active campaign.
Campaigns AS (
SELECT DISTINCT
_DATA_DATE,
_LATEST_DATE,
campaign_id
FROM
`${var.gcp_merchant_and_gads_dataset_project}.${var.gads_dataset_name}.ads_Campaign_${each.value.gads}`
WHERE
campaign_status = 'ENABLED'
),
# Get merchant id.
Merchants AS (
SELECT DISTINCT
ShoppingProductStats.campaign_id,
ShoppingProductStats.segments_product_merchant_id AS merchant_id,
GeoTargets.country_code AS target_country
FROM
`${var.gcp_merchant_and_gads_dataset_project}.${var.gads_dataset_name}.ads_ShoppingProductStats_${each.value.gads}`
AS ShoppingProductStats
INNER JOIN
(select distinct parent_id, country_code from `${var.gcp_project}.${var.zombies_dataset_name}.geo_targets`) AS GeoTargets
ON
(
SPLIT(
ShoppingProductStats.segments_product_country,
'/')[
SAFE_OFFSET(1)]
)
= GeoTargets.parent_id
),
# Get the active criteria only.
FilteredData AS (
SELECT
*
FROM Campaigns
INNER JOIN AssetGroups
USING (_DATA_DATE, _LATEST_DATE, campaign_id)
INNER JOIN AssetGroupListingGroupFilters
USING (_DATA_DATE, _LATEST_DATE, asset_group_id)
),
# Join recursively to traverse from the leaf node(the last criterion) to the root(all products).
JoinedData AS (
# Get the leaf node of the listing group
SELECT
# This used for troubleshooting purpose only. It is unique id for each row.
0 AS index,
# It is common id for each branch(route).
listing_group_filter_id AS leaf_node_listing_group_filter_id,
*
FROM FilteredData
WHERE
# This will get the leaf node only. The middle layer is 'SUBDIVISION'.
asset_group_listing_group_filter_type = 'UNIT_INCLUDED'
UNION ALL
# Traverse through all rules via parent_listing_group_filter_id
SELECT
# Increases the number by 1, so each row is unique.
Child.index + 1 AS index,
# Retains the leaf node id, so each route is unique(traceable).
Child.leaf_node_listing_group_filter_id,
Parent.*
FROM FilteredData AS Parent
INNER JOIN JoinedData AS Child
ON
Child.parent_listing_group_filter_id = Parent.listing_group_filter_id
AND Child._DATA_DATE = Parent._DATA_DATE
),
# Aggregates the inclusive criteria.
InclusiveCriteria AS (
SELECT
_DATA_DATE,
_LATEST_DATE,
asset_group_id,
campaign_id,
# Each branch is a criteria, aggregates to get all effective criterion.
leaf_node_listing_group_filter_id,
# Aggregates the parent ids to find the family tree
ARRAY_AGG(IFNULL(parent_listing_group_filter_id, 0)) AS parent_listing_group_filter_ids,
MAX(custom_label0) AS custom_label0,
MAX(custom_label1) AS custom_label1,
MAX(custom_label2) AS custom_label2,
MAX(custom_label3) AS custom_label3,
MAX(custom_label4) AS custom_label4,
MAX(product_type_l1) AS product_type_l1,
MAX(product_type_l2) AS product_type_l2,
MAX(product_type_l3) AS product_type_l3,
MAX(product_type_l4) AS product_type_l4,
MAX(product_type_l5) AS product_type_l5,
MAX(google_product_category_l1) AS google_product_category_l1,
MAX(google_product_category_l2) AS google_product_category_l2,
MAX(google_product_category_l3) AS google_product_category_l3,
MAX(google_product_category_l4) AS google_product_category_l4,
MAX(google_product_category_l5) AS google_product_category_l5,
MAX(channel) AS channel,
MAX(condition) AS condition,
MAX(brand) AS brand,
MAX(offer_id) AS offer_id
FROM
JoinedData
GROUP BY 1, 2, 3, 4, 5
),
# Lastly, add the 'Everything else' array to the table by matching all the parent(SUBDIVISION).
Criteria AS (
SELECT
InclusiveCriteria._DATA_DATE,
InclusiveCriteria._LATEST_DATE,
InclusiveCriteria.asset_group_id,
InclusiveCriteria.campaign_id,
InclusiveCriteria.leaf_node_listing_group_filter_id,
InclusiveCriteria.custom_label0,
InclusiveCriteria.custom_label1,
InclusiveCriteria.custom_label2,
InclusiveCriteria.custom_label3,
InclusiveCriteria.custom_label4,
InclusiveCriteria.product_type_l1,
InclusiveCriteria.product_type_l2,
InclusiveCriteria.product_type_l3,
InclusiveCriteria.product_type_l4,
InclusiveCriteria.product_type_l5,
InclusiveCriteria.google_product_category_l1,
InclusiveCriteria.google_product_category_l2,
InclusiveCriteria.google_product_category_l3,
InclusiveCriteria.google_product_category_l4,
InclusiveCriteria.google_product_category_l5,
InclusiveCriteria.channel,
InclusiveCriteria.condition,
InclusiveCriteria.brand,
InclusiveCriteria.offer_id,
ANY_VALUE(IF(custom_label0 IS NULL, AggregatedCriteria.agg_custom_label0, []))
AS neg_custom_label0,
ANY_VALUE(IF(custom_label1 IS NULL, AggregatedCriteria.agg_custom_label1, []))
AS neg_custom_label1,
ANY_VALUE(IF(custom_label2 IS NULL, AggregatedCriteria.agg_custom_label2, []))
AS neg_custom_label2,
ANY_VALUE(IF(custom_label3 IS NULL, AggregatedCriteria.agg_custom_label3, []))
AS neg_custom_label3,
ANY_VALUE(IF(custom_label4 IS NULL, AggregatedCriteria.agg_custom_label4, []))
AS neg_custom_label4,
ANY_VALUE(IF(product_type_l1 IS NULL, AggregatedCriteria.agg_product_type_l1, []))
AS neg_product_type_l1,
ANY_VALUE(IF(product_type_l2 IS NULL, AggregatedCriteria.agg_product_type_l2, []))
AS neg_product_type_l2,
ANY_VALUE(IF(product_type_l3 IS NULL, AggregatedCriteria.agg_product_type_l3, []))
AS neg_product_type_l3,
ANY_VALUE(IF(product_type_l4 IS NULL, AggregatedCriteria.agg_product_type_l4, []))
AS neg_product_type_l4,
ANY_VALUE(IF(product_type_l5 IS NULL, AggregatedCriteria.agg_product_type_l5, []))
AS neg_product_type_l5,
ANY_VALUE(
IF(
google_product_category_l1 IS NULL,
AggregatedCriteria.agg_google_product_category_l1,
[])) AS neg_google_product_category_l1,
ANY_VALUE(
IF(
google_product_category_l2 IS NULL,
AggregatedCriteria.agg_google_product_category_l2,
[])) AS neg_google_product_category_l2,
ANY_VALUE(
IF(
google_product_category_l3 IS NULL,
AggregatedCriteria.agg_google_product_category_l3,
[])) AS neg_google_product_category_l3,
ANY_VALUE(
IF(
google_product_category_l4 IS NULL,
AggregatedCriteria.agg_google_product_category_l4,
[])) AS neg_google_product_category_l4,
ANY_VALUE(
IF(
google_product_category_l5 IS NULL,
AggregatedCriteria.agg_google_product_category_l5,
[])) AS neg_google_product_category_l5,
ANY_VALUE(IF(channel IS NULL, AggregatedCriteria.agg_channel, [])) AS neg_channel,
ANY_VALUE(IF(condition IS NULL, AggregatedCriteria.agg_condition, [])) AS neg_condition,
ANY_VALUE(IF(brand IS NULL, AggregatedCriteria.agg_brand, [])) AS neg_brand,
ANY_VALUE(IF(offer_id IS NULL, AggregatedCriteria.agg_offer_id, [])) AS neg_offer_id
FROM
InclusiveCriteria
LEFT JOIN
AggregatedCriteria
ON
AggregatedCriteria.asset_group_id = InclusiveCriteria.asset_group_id
AND AggregatedCriteria._DATA_DATE = InclusiveCriteria._DATA_DATE
AND AggregatedCriteria.parent_listing_group_filter_id
IN UNNEST(InclusiveCriteria.parent_listing_group_filter_ids)
GROUP BY
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
)
SELECT
*
FROM Criteria
INNER JOIN Merchants
USING (campaign_id)
);
EOF
}
}