Essentials

Event Listeners

Subscribe to any Tawk.to event in Nuxt using useTawk(). Every listener returns a cleanup function to avoid memory leaks.

All Tawk.to event listeners are available via useTawk(). Every listener returns a cleanup function — call it in onUnmounted to remove the listener and avoid memory leaks.

Basic Pattern

composables/useChatEvents.ts
import { onMounted, onUnmounted } from 'vue'

const { onLoad, onStatusChange, onChatStarted } = useTawk()

onMounted(() => {
  const cleanupLoad = onLoad(() => {
    console.log('Tawk.to widget loaded')
  })

  const cleanupStatus = onStatusChange((status) => {
    console.log('Agent status:', status) // 'online' | 'away' | 'offline'
  })

  const cleanupStarted = onChatStarted(() => {
    console.log('Chat session started')
  })

  onUnmounted(() => {
    cleanupLoad()
    cleanupStatus()
    cleanupStarted()
  })
})

Available Listeners

ListenerCallback SignatureDescription
onLoad() => voidFires when the widget has fully loaded.
onBeforeLoad() => voidFires just before the widget loads.
onStatusChange(status: TawkStatus) => voidFires when agent availability changes.
onChatMaximized() => voidFires when the chat window is opened.
onChatMinimized() => voidFires when the chat window is minimized.
onChatHidden() => voidFires when the widget bubble is hidden.
onChatStarted() => voidFires when a chat session starts.
onChatEnded() => voidFires when a chat session ends.
onPrechatSubmit(data: Record<string, unknown>) => voidFires when the pre-chat form is submitted.
onOfflineSubmit(data: Record<string, unknown>) => voidFires when the offline form is submitted.
onChatMessageVisitor(message: string) => voidFires when the visitor sends a message.
onChatMessageAgent(message: string) => voidFires when an agent sends a message.
onChatMessageSystem(message: string) => voidFires on system messages.
onAgentJoinChat(data: Record<string, unknown>) => voidFires when an agent joins the chat.
onAgentLeaveChat(data: Record<string, unknown>) => voidFires when an agent leaves the chat.
onChatSatisfaction(satisfaction: number) => voidFires when the visitor submits a rating.
onVisitorNameChanged(visitorName: string) => voidFires when the visitor's name changes.
onFileUpload(link: string) => voidFires when a file is uploaded in chat.
onTagsUpdated(data: Record<string, unknown>) => voidFires when conversation tags change.
onUnreadCountChanged(count: number) => voidFires when the unread message count changes.

Real-World Example

Show a notification badge and track chat engagement:

components/ChatWidget.vue
<script setup lang="ts">
import { onMounted, onUnmounted } from 'vue'

const {
  isHidden,
  unreadCount,
  status,
  showWidget,
  onLoad,
  onUnreadCountChanged,
  onChatStarted
} = useTawk()

const cleanupFns: Array<() => void> = []

onMounted(() => {
  cleanupFns.push(
    onLoad(() => {
      console.log('Widget ready, agent is:', status.value)
    }),
    onUnreadCountChanged((count) => {
      console.log('New unread messages:', count)
    }),
    onChatStarted(() => {
      console.log('Chat started — tracking engagement')
    })
  )
})

onUnmounted(() => {
  cleanupFns.forEach(fn => fn())
})
</script>

<template>
  <button
    v-if="isHidden"
    class="fixed bottom-4 right-4 rounded-full"
    @click="showWidget()"
  >
    💬
    <span v-if="unreadCount > 0" class="badge">{{ unreadCount }}</span>
  </button>
</template>