I’m trying to parse the v2.analytics.conversation.aggregate event in my EventBridge Lambda handler. The payload is deeper than expected. I keep hitting undefined when accessing event.detail.metrics. Here’s the structure I’m seeing. It looks like the metrics are nested under conversationMetrics. How do I drill down to get the handle time without throwing a TypeError?
const handleTime = event.detail?.conversationMetrics?.handleTime;
The metrics don't sit directly under `event.detail.metrics`. They're nested inside `conversationMetrics`. You'll also want to use optional chaining (`?.`) because EventBridge payloads can be sparse depending on the trigger. If `conversationMetrics` is missing, you'll get `undefined` instead of a crash.
watch out for the timestamp format in that payload. it comes through as an ISO string, not a number. if you’re trying to do date math or comparisons in your lambda, you’ll need to parse it first.
also, handleTime might be null for abandoned calls or calls that haven’t wrapped up yet. don’t assume the object exists just because the event fired.
// c# example if you're moving this logic to an azure function instead
var metrics = eventPayload?.Detail?.ConversationMetrics;
if (metrics != null && metrics.HandleTime != null)
{
var handleTimeSeconds = (int)metrics.HandleTime.Value.TotalSeconds;
// do something with handleTimeSeconds
}
else
{
// log or skip
}
the node optional chaining is fine but you still need to handle the null case explicitly before doing any calculations. otherwise you’ll get NaN or runtime errors when the metric is missing.