1
import klaviyo
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
profile = klaviyo.Profiles.get_profiles(filter=f'equals(id,"{profile_id}")')
11
current_bookings = profile["data"][0]["attributes"]["properties"].get("Recent bookings", [])
12
13
current_booking = {
14
"booking_city": booking_city,
15
"booking_postcard_url": booking_postcard_url
16
}
17
current_bookings.insert(0, current_booking)
18
profile_properties = {
19
"data": {
20
"type": "profile",
21
"id": profile_id,
22
"attributes": {
23
"properties": {"Recent bookings": current_bookings[0:max_properties]}
24
}
25
}
26
}
27
28
klaviyo.Profiles.update_profile(profile_id, profile_properties)