> For the complete documentation index, see [llms.txt](https://alhena.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alhena.gitbook.io/docs/developer-reference/website-sdk/examples/auto-close-after-handoff.md).

# Auto-Close After Handoff

This example shows how to automatically close the chat widget after a conversation has been transferred to a human agent. This is useful for cleaning up the UI once support handoff is complete.

## What You'll Learn

* Listen for the `ticket:agent_handoff` event
* Use `closeTicket()` to end the conversation
* Use `close()` to hide the widget

## Use Case

When a customer is transferred to a human agent, you may want to:

1. Keep the widget open briefly so the user sees the confirmation
2. Automatically close the widget after a delay
3. Start fresh for the next interaction

This prevents abandoned chat widgets from cluttering the screen after support handoff.

## Prerequisites

* Alhena SDK installed on your website
* Helpdesk integration configured (Zendesk, Freshdesk, etc.)

## Code Example

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Auto-Close Example</title>
</head>
<body>

<h1>Support Page</h1>
<p>Click the chat widget for assistance.</p>

<!-- Alhena SDK -->
<script>
document.gleenConfig = {
    company: 'your-company-key',
    apiBaseUrl: 'https://app.alhena.ai'
};
</script>
<script src="https://app.alhena.ai/sdk/gleenWidget.js"></script>

<!-- Auto-close logic -->
<script>
window.gleenWidget.on('ticket:agent_handoff', function(data) {
    console.log('Handoff complete. Ticket ID:', data.ticketId);

    // Wait 60 seconds, then close
    setTimeout(function() {
        window.gleenWidget.closeTicket(); // End the conversation
        window.gleenWidget.close();        // Hide the widget
    }, 60000);
});
</script>

</body>
</html>
```

## How It Works

1. **Event fires**: When a user submits their email and a ticket is created in your helpdesk, the `ticket:agent_handoff` event fires
2. **Delay**: We wait 60 seconds (configurable) to give the user time to see the confirmation
3. **Close ticket**: `closeTicket()` ends the current conversation, preparing the widget for a new conversation
4. **Hide widget**: `close()` minimizes the widget

## Customization

### Change the delay

Adjust the timeout value (in milliseconds):

```javascript
setTimeout(function() {
    window.gleenWidget.closeTicket();
    window.gleenWidget.close();
}, 30000); // 30 seconds instead of 60
```

### Show a notification before closing

```javascript
window.gleenWidget.on('ticket:agent_handoff', function(data) {
    // Show a custom notification
    showNotification('Your ticket has been created. A support agent will contact you via email.');

    setTimeout(function() {
        window.gleenWidget.closeTicket();
        window.gleenWidget.close();
    }, 60000);
});
```

### Keep widget visible but reset conversation

If you want to close just the ticket but keep the widget visible:

```javascript
window.gleenWidget.on('ticket:agent_handoff', function() {
    setTimeout(function() {
        window.gleenWidget.closeTicket(); // Only close the ticket
        // Widget stays visible, ready for new conversation
    }, 60000);
});
```

## Related

* [Events Reference](/docs/developer-reference/website-sdk/events.md#ticket-agent_handoff) - Full event documentation
* [Human Handoff Widget](/docs/developer-reference/website-sdk/examples/human-handoff-widget.md) - Switch to external support widget
* [JavaScript API](/docs/developer-reference/website-sdk/javascript-api.md) - Complete method reference
