This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
/
user_event.py
396 lines (335 loc) · 17.5 KB
/
user_event.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
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
393
394
395
396
# -*- coding: utf-8 -*-
# Copyright 2020 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.
#
import proto # type: ignore
from google.cloud.retail_v2.types import common
from google.cloud.retail_v2.types import product as gcr_product
from google.protobuf import timestamp_pb2 # type: ignore
from google.protobuf import wrappers_pb2 # type: ignore
__protobuf__ = proto.module(
package="google.cloud.retail.v2",
manifest={"UserEvent", "ProductDetail", "CompletionDetail", "PurchaseTransaction",},
)
class UserEvent(proto.Message):
r"""UserEvent captures all metadata information Retail API needs
to know about how end users interact with customers' website.
Attributes:
event_type (str):
Required. User event type. Allowed values are:
- ``add-to-cart``: Products being added to cart.
- ``category-page-view``: Special pages such as sale or
promotion pages viewed.
- ``completion``: Completion query result showed/clicked.
- ``detail-page-view``: Products detail page viewed.
- ``home-page-view``: Homepage viewed.
- ``promotion-offered``: Promotion is offered to a user.
- ``promotion-not-offered``: Promotion is not offered to a
user.
- ``purchase-complete``: User finishing a purchase.
- ``search``: Product search.
- ``shopping-cart-page-view``: User viewing a shopping
cart.
visitor_id (str):
Required. A unique identifier for tracking visitors.
For example, this could be implemented with an HTTP cookie,
which should be able to uniquely identify a visitor on a
single device. This unique identifier should not change if
the visitor log in/out of the website.
The field must be a UTF-8 encoded string with a length limit
of 128 characters. Otherwise, an INVALID_ARGUMENT error is
returned.
The field should not contain PII or user-data. We recommend
to use Google Analystics `Client
ID <https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#clientId>`__
for this field.
session_id (str):
A unique identifier for tracking a visitor session with a
length limit of 128 bytes. A session is an aggregation of an
end user behavior in a time span.
A general guideline to populate the sesion_id:
1. If user has no activity for 30 min, a new session_id
should be assigned.
2. The session_id should be unique across users, suggest use
uuid or add visitor_id as prefix.
event_time (google.protobuf.timestamp_pb2.Timestamp):
Only required for
[UserEventService.ImportUserEvents][google.cloud.retail.v2.UserEventService.ImportUserEvents]
method. Timestamp of when the user event happened.
experiment_ids (Sequence[str]):
A list of identifiers for the independent
experiment groups this user event belongs to.
This is used to distinguish between user events
associated with different experiment setups
(e.g. using Retail API, using different
recommendation models).
attribution_token (str):
Highly recommended for user events that are the result of
[PredictionService.Predict][google.cloud.retail.v2.PredictionService.Predict].
This field enables accurate attribution of recommendation
model performance.
The value must be a valid
[PredictResponse.attribution_token][google.cloud.retail.v2.PredictResponse.attribution_token]
for user events that are the result of
[PredictionService.Predict][google.cloud.retail.v2.PredictionService.Predict].
The value must be a valid
[SearchResponse.attribution_token][google.cloud.retail.v2.SearchResponse.attribution_token]
for user events that are the result of
[SearchService.Search][google.cloud.retail.v2.SearchService.Search].
This token enables us to accurately attribute page view or
purchase back to the event and the particular predict
response containing this clicked/purchased product. If user
clicks on product K in the recommendation results, pass
[PredictResponse.attribution_token][google.cloud.retail.v2.PredictResponse.attribution_token]
as a URL parameter to product K's page. When recording
events on product K's page, log the
[PredictResponse.attribution_token][google.cloud.retail.v2.PredictResponse.attribution_token]
to this field.
product_details (Sequence[google.cloud.retail_v2.types.ProductDetail]):
The main product details related to the event.
This field is required for the following event types:
- ``add-to-cart``
- ``detail-page-view``
- ``purchase-complete``
In a ``search`` event, this field represents the products
returned to the end user on the current page (the end user
may have not finished browsing the whole page yet). When a
new page is returned to the end user, after
pagination/filtering/ordering even for the same query, a new
``search`` event with different
[product_details][google.cloud.retail.v2.UserEvent.product_details]
is desired. The end user may have not finished browsing the
whole page yet.
completion_detail (google.cloud.retail_v2.types.CompletionDetail):
The main completion details related to the event.
In a ``completion`` event, this field represents the
completions returned to the end user and the clicked
completion by the end user. In a ``search`` event, it
represents the search event happens after clicking
completion.
attributes (Sequence[google.cloud.retail_v2.types.UserEvent.AttributesEntry]):
Extra user event features to include in the recommendation
model.
The key must be a UTF-8 encoded string with a length limit
of 5,000 characters. Otherwise, an INVALID_ARGUMENT error is
returned.
For product recommendation, an example of extra user
information is traffic_channel, i.e. how user arrives at the
site. Users can arrive at the site by coming to the site
directly, or coming through Google search, and etc.
cart_id (str):
The ID or name of the associated shopping cart. This ID is
used to associate multiple items added or present in the
cart before purchase.
This can only be set for ``add-to-cart``,
``purchase-complete``, or ``shopping-cart-page-view``
events.
purchase_transaction (google.cloud.retail_v2.types.PurchaseTransaction):
A transaction represents the entire purchase transaction.
Required for ``purchase-complete`` events. Other event types
should not set this field. Otherwise, an INVALID_ARGUMENT
error is returned.
search_query (str):
The user's search query.
See
[SearchRequest.query][google.cloud.retail.v2.SearchRequest.query]
for definition.
The value must be a UTF-8 encoded string with a length limit
of 5,000 characters. Otherwise, an INVALID_ARGUMENT error is
returned.
At least one of
[search_query][google.cloud.retail.v2.UserEvent.search_query]
or
[page_categories][google.cloud.retail.v2.UserEvent.page_categories]
is required for ``search`` events. Other event types should
not set this field. Otherwise, an INVALID_ARGUMENT error is
returned.
filter (str):
The filter syntax consists of an expression language for
constructing a predicate from one or more fields of the
products being filtered.
See
[SearchRequest.filter][google.cloud.retail.v2.SearchRequest.filter]
for definition and syntax.
The value must be a UTF-8 encoded string with a length limit
of 1,000 characters. Otherwise, an INVALID_ARGUMENT error is
returned.
order_by (str):
The order in which products are returned.
See
[SearchRequest.order_by][google.cloud.retail.v2.SearchRequest.order_by]
for definition and syntax.
The value must be a UTF-8 encoded string with a length limit
of 1,000 characters. Otherwise, an INVALID_ARGUMENT error is
returned.
This can only be set for ``search`` events. Other event
types should not set this field. Otherwise, an
INVALID_ARGUMENT error is returned.
offset (int):
An integer that specifies the current offset for pagination
(the 0-indexed starting location, amongst the products
deemed by the API as relevant).
See
[SearchRequest.offset][google.cloud.retail.v2.SearchRequest.offset]
for definition.
If this field is negative, an INVALID_ARGUMENT is returned.
This can only be set for ``search`` events. Other event
types should not set this field. Otherwise, an
INVALID_ARGUMENT error is returned.
page_categories (Sequence[str]):
The categories associated with a category page.
To represent full path of category, use '>' sign to separate
different hierarchies. If '>' is part of the category name,
please replace it with other character(s).
Category pages include special pages such as sales or
promotions. For instance, a special sale page may have the
category hierarchy: "pageCategories" : ["Sales > 2017 Black
Friday Deals"].
Required for ``category-page-view`` events. At least one of
[search_query][google.cloud.retail.v2.UserEvent.search_query]
or
[page_categories][google.cloud.retail.v2.UserEvent.page_categories]
is required for ``search`` events. Other event types should
not set this field. Otherwise, an INVALID_ARGUMENT error is
returned.
user_info (google.cloud.retail_v2.types.UserInfo):
User information.
uri (str):
Complete URL (window.location.href) of the
user's current page.
When using the client side event reporting with
JavaScript pixel and Google Tag Manager, this
value is filled in automatically. Maximum length
5,000 characters.
referrer_uri (str):
The referrer URL of the current page.
When using the client side event reporting with
JavaScript pixel and Google Tag Manager, this
value is filled in automatically.
page_view_id (str):
A unique ID of a web page view.
This should be kept the same for all user events triggered
from the same pageview. For example, an item detail page
view could trigger multiple events as the user is browsing
the page. The ``pageViewId`` property should be kept the
same for all these events so that they can be grouped
together properly.
When using the client side event reporting with JavaScript
pixel and Google Tag Manager, this value is filled in
automatically.
"""
event_type = proto.Field(proto.STRING, number=1,)
visitor_id = proto.Field(proto.STRING, number=2,)
session_id = proto.Field(proto.STRING, number=21,)
event_time = proto.Field(proto.MESSAGE, number=3, message=timestamp_pb2.Timestamp,)
experiment_ids = proto.RepeatedField(proto.STRING, number=4,)
attribution_token = proto.Field(proto.STRING, number=5,)
product_details = proto.RepeatedField(
proto.MESSAGE, number=6, message="ProductDetail",
)
completion_detail = proto.Field(
proto.MESSAGE, number=22, message="CompletionDetail",
)
attributes = proto.MapField(
proto.STRING, proto.MESSAGE, number=7, message=common.CustomAttribute,
)
cart_id = proto.Field(proto.STRING, number=8,)
purchase_transaction = proto.Field(
proto.MESSAGE, number=9, message="PurchaseTransaction",
)
search_query = proto.Field(proto.STRING, number=10,)
filter = proto.Field(proto.STRING, number=16,)
order_by = proto.Field(proto.STRING, number=17,)
offset = proto.Field(proto.INT32, number=18,)
page_categories = proto.RepeatedField(proto.STRING, number=11,)
user_info = proto.Field(proto.MESSAGE, number=12, message=common.UserInfo,)
uri = proto.Field(proto.STRING, number=13,)
referrer_uri = proto.Field(proto.STRING, number=14,)
page_view_id = proto.Field(proto.STRING, number=15,)
class ProductDetail(proto.Message):
r"""Detailed product information associated with a user event.
Attributes:
product (google.cloud.retail_v2.types.Product):
Required. [Product][google.cloud.retail.v2.Product]
information.
Required field(s):
- [Product.id][google.cloud.retail.v2.Product.id]
Optional override field(s):
- [Product.price_info][google.cloud.retail.v2.Product.price_info]
If any supported optional fields are provided, we will treat
them as a full override when looking up product information
from the catalog. Thus, it is important to ensure that the
overriding fields are accurate and complete.
All other product fields are ignored and instead populated
via catalog lookup after event ingestion.
quantity (google.protobuf.wrappers_pb2.Int32Value):
Quantity of the product associated with the user event.
For example, this field will be 2 if two products are added
to the shopping cart for ``purchase-complete`` event.
Required for ``add-to-cart`` and ``purchase-complete`` event
types.
"""
product = proto.Field(proto.MESSAGE, number=1, message=gcr_product.Product,)
quantity = proto.Field(proto.MESSAGE, number=2, message=wrappers_pb2.Int32Value,)
class CompletionDetail(proto.Message):
r"""Detailed completion information including completion
attribution token and clicked completion info.
Attributes:
completion_attribution_token (str):
Completion attribution token in
[CompleteQueryResponse.attribution_token][google.cloud.retail.v2.CompleteQueryResponse.attribution_token].
selected_suggestion (str):
End user selected
[CompleteQueryResponse.CompletionResult.suggestion][google.cloud.retail.v2.CompleteQueryResponse.CompletionResult.suggestion].
selected_position (int):
End user selected
[CompleteQueryResponse.CompletionResult.suggestion][google.cloud.retail.v2.CompleteQueryResponse.CompletionResult.suggestion]
position, starting from 0.
"""
completion_attribution_token = proto.Field(proto.STRING, number=1,)
selected_suggestion = proto.Field(proto.STRING, number=2,)
selected_position = proto.Field(proto.INT32, number=3,)
class PurchaseTransaction(proto.Message):
r"""A transaction represents the entire purchase transaction.
Attributes:
id (str):
The transaction ID with a length limit of 128
characters.
revenue (float):
Required. Total non-zero revenue or grand
total associated with the transaction. This
value include shipping, tax, or other
adjustments to total revenue that you want to
include as part of your revenue calculations.
tax (float):
All the taxes associated with the
transaction.
cost (float):
All the costs associated with the products. These can be
manufacturing costs, shipping expenses not borne by the end
user, or any other costs, such that:
- Profit =
[revenue][google.cloud.retail.v2.PurchaseTransaction.revenue]
- [tax][google.cloud.retail.v2.PurchaseTransaction.tax] -
[cost][google.cloud.retail.v2.PurchaseTransaction.cost]
currency_code (str):
Required. Currency code. Use three-character
ISO-4217 code.
"""
id = proto.Field(proto.STRING, number=1,)
revenue = proto.Field(proto.FLOAT, number=2,)
tax = proto.Field(proto.FLOAT, number=3,)
cost = proto.Field(proto.FLOAT, number=4,)
currency_code = proto.Field(proto.STRING, number=5,)
__all__ = tuple(sorted(__protobuf__.manifest))