1
from klaviyo_api import KlaviyoAPI
2
3
def handler(event, context):
4
private_key = os.getenv("KLAVIYO_PRIVATE_KEY")
5
max_properties = int(os.getenv("MAX_PROPERTIES_TO_STORE"))
6
profile_id = event["data"]["relationships"]["profile"]["data"]["id"]
7
booking_city = event["data"]["attributes"]["event_properties"]["booking_city"]
8
booking_postcard_url = event["data"]["attributes"]["event_properties"]["booking_postcard_url"]
9
10
klaviyo = KlaviyoAPI(private_key, max_delay=60, max_retries=3, test_host=None)
11
profile = klaviyo.Profiles.get_profiles(
12
filter=f'equals(id,"{profile_id}")'
13
)
14
current_bookings = profile["data"][0]["attributes"]["properties"]["Recent bookings"] if ("Recent bookings" in profile["data"][0]["attributes"]["properties"] and profile["data"][0]["attributes"]["properties"]["Recent bookings"]) else []
15
16
current_booking = {
17
"booking_city": booking_city,
18
"booking_postcard_url": booking_postcard_url
19
}
20
21
current_bookings.insert(0, current_booking)
22
23
profile_properties = {
24
"data": {
25
"type": "profile",
26
"id": profile_id,
27
"attributes": {
28
"properties": {
29
"Recent bookings": current_bookings[0:max_properties]
30
}
31
}
32
}
33
}
34
35
klaviyo.Profiles.update_profile(profile_id, profile_properties)
36
37
38