-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathimport_event_inline.py
More file actions
executable file
·95 lines (82 loc) · 2.14 KB
/
Copy pathimport_event_inline.py
File metadata and controls
executable file
·95 lines (82 loc) · 2.14 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
#!/usr/bin/python3
""" Inline import user events using Retail API
This example shows how to construct a timestamp (using current time)
"""
import datetime
from google.cloud import retail
from google.oauth2 import service_account
from google.protobuf.timestamp_pb2 import Timestamp
from google.protobuf.wrappers_pb2 import Int32Value
SERVICE_ACCOUNT_FILE = "<path to service account json file>"
PROJECT_NUM = "<project # or id>"
SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
client = retail.UserEventServiceClient(credentials=credentials)
timestamp = Timestamp()
timestamp.FromDatetime(dt=datetime.datetime.now())
quantity = Int32Value(value=1)
user_events = {
"user_events": [
{
"event_type": "home-page-view",
"event_time": timestamp,
"visitor_id": "visitor-1",
"user_info": {
"user_id": "user12345"
}
},
{
"event_type": "detail-page-view",
"event_time": timestamp,
"visitor_id": "visitor-1",
"user_info": {
"user_id": "user12345"
},
"product_details": [{
"product": {
"id": "12345"
}
}]
},
{
"event_type": "add-to-cart",
"event_time": timestamp,
"visitor_id": "visitor-1",
"user_info": {
"user_id": "user12345"
},
"product_details": [{
"product": {
"id": "12345"
},
"quantity": quantity
}]
},
{
"event_type": "purchase-complete",
"event_time": timestamp,
"visitor_id": "visitor-1",
"user_info": {
"user_id": "user12345"
},
"product_details": [{
"product": {
"id": "12345"
},
"quantity": quantity
}],
"purchase_transaction": {
"id": "567",
"revenue": 19.95,
"currency_code": "USD"
}
}
]
}
request = {
"parent":
'projects/' + PROJECT_NUM + '/locations/global/catalogs/default_catalog',
"input_config": {"user_event_inline_source": user_events}
}
response = client.import_user_events(request)