Question: How do I calculate the time for a contact in the queue in Amazon Connect?

I want to calculate the time for a contact being spent in the queue in Amazon Connect.

Short descriptionYou can calculate the time for a contact in the queue in Amazon Connect for active and completed contacts.To calculate time spent in the queue for active contacts, use the following methods:Track Amazon Connect metrics sent to CloudWatch for QueueSize and LongestQueueWaitTimeUse the GetCurrentMetricData API to track CONTACTS_IN_QUEUE and OLDEST_CONTACT_AGEUse Amazon Connect contact events for tracking individual contactsTo calculate time spent in the queue for completed contacts, use the following methods:Track maximum queued time using historical metricsTrack duration using the QueueInfo data in contact records for individual contacts

ResolutionFor active contactsTrack QueueSize and LongestQueueWaitTime metricsOpen the Amazon CloudWatch console.In the navigation pane, choose Metrics, and then choose All metrics.On the Metrics tab, choose Connect, and then choose Queue metrics.Select the metrics QueueSize and LongestQueueWaitTime.Choose the Graphed metrics tab. Then, for Statistic, choose Maximum.Review both the QueueSize and LongestQueueWaitTime.The QueueSize is the number of contacts in the queue. The LongestQueueWaitTime shows the longest amount of time in seconds that a contact waited in a queue. For more information, see Monitoring your instance using CloudWatch.Tip: You can set a CloudWatch alarm on the LongestQueueWaitTime metric to get a notification if it reaches a certain threshold. For additional information, see Creating an alarm from a metric on a graph.Use the GetCurrentMetricData API to track CONTACTS_IN_QUEUE and OLDEST_CONTACT_AGEFirst, to find the QueueID and InstanceID for the API request parameters, do the following:Log in to your Amazon Connect instance using your access URL (https://alias.awsapps.com/connect/login -or- https://domain.my.connect.aws). You must use the administrator account or the emergency access Amazon Connect instance login.In the navigation menu, choose Routing, and then choose Queues.Choose the name of the queue that you want to review.In the Queue Details, choose the show additional queue information.Find the queue ARN shown as arn:aws:connect:region:account-id:instance/instance-id/queue/queue-id. Make note of the AWS Region, instance-id, and queue-id for the next steps.Then, to run the GetCurrentMetricData API, do the following:1.    Navigate to AWS CloudShell.2.    Run the following AWS Command Line Interface (AWS CLI) command:Note: Replace queue-id, instance-id, and region with your values.aws connect get-current-metric-data --filters Queues=<queue-id> --instance-id <instance-id> --current-metrics Name=CONTACTS_IN_QUEUE,Unit=COUNT Name=OLDEST_CONTACT_AGE,Unit=SECONDS --groupings QUEUE --region <region>Note: If you receive errors when running AWS CLI commands, make sure that you’re using the most recent version of the AWS CLI.You receive an output similar to the following:{    "MetricResults": [        {            "Dimensions": {                "Queue": {                    "Id": "<queue-id>",                    "Arn": "<queue-arn>"                }            },            "Collections": [                {                    "Metric": {                        "Name": "CONTACTS_IN_QUEUE",                        "Unit": "COUNT"                    },                    "Value": 0.0                },                {                    "Metric": {                        "Name": "OLDEST_CONTACT_AGE",                        "Unit": "SECONDS"                    },                    "Value": 0.0                }            ]        }    ],    "DataSnapshotTime": "<The time at which the metrics were retrieved and cached for pagination.>"}Use contact events for tracking individual contacts1.    Open the Amazon EventBridge console.2.    In the navigation pane, choose Rules.3.    Choose Create rule.4.    For Rule type, choose Rule with an event pattern.5.    Choose Next.6.    In the Creation Method, choose Use pattern form.7.    In the Event pattern, select Event source as AWS Services, AWS Service as Amazon Connect, and Event Type as Amazon Connect Contact Event.8.    Under Target1, choose Target type as AWS Service.9.    Under Select a target, choose Lambda function. For the function, do the following:Create a Lambda function with the console, using runtime Python 3.8.For the Lambda function code, use the following:import jsondef lambda_handler(event, context):    # TODO implement    print(event)    return {        'statusCode': 200,        'body': json.dumps('Hello from Lambda!')    }Note: The Lambda function prints all events and is intended for testing. The process to calculate the time spent by a specific contact in a queue must be set up manually.10.    Choose Skip to Review and create, and then choose Create rule.11.    Access the Amazon CloudWatch logs for AWS Lambda to see a near real-time stream of contacts, such as, voice calls, chat, and task events. For example, you can see if a call is queued in your Amazon Connect contact center.Note: Available contact events are INITIATED, CONNECTED_TO_SYSTEM, QUEUED, CONNECTED_TO_AGENT, and DISCONNECTED. Events are released on a best effort basis.12.    To determine the time spent by a specific contact in the queue, first locate the following information:The QUEUED event timestamp for a specific contact ID.The CONNECTED_TO_AGENT event timestamp for the same contact ID.13.    To calculate the time spent by the specific contact in the queue, subtract the QUEUED timestamp from the CONNECTED_TO_AGENT timestamp.For completed contactsTrack queued time using historical metricsTo view the historical metric reports, do the following:Log in to your Amazon Connect instance using your access URL (https://alias.awsapps.com/connect/login -or- https://domain.my.connect.aws).Important: You must log in as a user that has sufficient permissions required to view historical metrics reports.In the navigation menu, choose Analytics and optimization, Historical metrics.Choose the Queues report type.Choose the gear icon.On the Metrics tab, choose Maximum queued time.On the Interval & Time range tab, set the Interval, Time Zone, and Time range.When you're finished customizing your report, choose Apply. The Maximum queued time shows the longest time that a contact spent waiting in the queue for the selected interval and time range.(Optional) To save your report for future use, choose Save, provide a name for the report, and then choose Save.Tip: You can schedule a historical metrics report for future use.You can also use the GetMetricData API to track QUEUED_TIME. The GetMetricData API metrics are available for only a 24 hour duration.Track duration in the QueueInfo using the contact search for individual contactsTo use the contact search, do the following:View a contact record in the UI to open the contact trace record (CTR) that you want to view.If the contact was queued, then the Queue section populates and lists the duration of time the contact spent in the queue. Note: Data retention for CTR is 24 months upon contact initiation.To retain contact data for longer than 24 months, stream the CTRs using the following method:Create an Amazon Kinesis Data Firehose delivery stream or an Amazon Kinesis data stream. Then, activate data streaming in your instance.Note: For an alternate method, see Analyze Amazon Connect contact trace record with Amazon Athena and Amazon QuickSight.