-
Notifications
You must be signed in to change notification settings - Fork 92
feat: Tawk.to #886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
atlaxt
wants to merge
7
commits into
nuxt:main
Choose a base branch
from
atlaxt:feat/tawk-to-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Tawk.to #886
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6bf0cb
feat: Tawk.to
atlaxt 6580ef9
feat/tawk-to-registry
atlaxt aec8457
fix(tawk-to): use documented inline/embed window type, warn on post-l…
harlan-zw 3c7dda6
fix(tawk-to): boolean onLoaded readiness and create Tawk_API stub in …
harlan-zw 167bdd4
fix(tawk-to): stub-safe getters, isHidden resync on visibility comman…
harlan-zw d1cf7df
fix(tawk-to): add optional E.164 phone field to TawkToVisitor
harlan-zw aa2869b
fix(tawk-to): include phone in setVisitor post-load warning and docs …
harlan-zw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| --- | ||
| title: Tawk.to | ||
| description: Load the Tawk.to live chat widget and drive it through a typed proxy, reactive state, and event listeners. | ||
| links: | ||
| - label: Source | ||
| icon: i-simple-icons-github | ||
| to: https://github.com/nuxt/scripts/blob/main/packages/script/src/runtime/registry/tawk-to.ts | ||
| size: xs | ||
| --- | ||
|
|
||
| [Tawk.to](https://www.tawk.to/) is a free live chat widget. | ||
|
|
||
| [`useScriptTawkTo()`{lang="ts"}](/scripts/tawk-to) loads the widget, types the `window.Tawk_API` command surface, and bridges the `window` events the embed script dispatches into reactive state and typed listeners. | ||
|
|
||
| ::script-stats | ||
| :: | ||
|
|
||
| ::script-docs | ||
| :: | ||
|
|
||
| The composable uses these defaults: | ||
|
|
||
| - **Trigger: `onNuxtReady`.** The script loads after Nuxt hydration, using the module-wide default. | ||
| - **Bundle and proxy: off.** Tawk's runtime network behavior (whether the embed script derives its own API origin from its `src`, whether it opens connections a proxy would sit in front of for live-chat polling) hasn't been verified, so neither capability is declared yet. | ||
|
|
||
| The widget needs both `propertyId` and `widgetId` to load. Find them under **Administration Settings → Channels → Chat Widget** in your Tawk.to dashboard. | ||
|
|
||
| ::code-group | ||
|
|
||
| ```ts [Proxy] | ||
| const { proxy } = useScriptTawkTo({ | ||
| propertyId: 'your-property-id', | ||
| widgetId: 'your-widget-id', | ||
| }) | ||
|
|
||
| function openChat() { | ||
| proxy.maximize() | ||
| } | ||
| ``` | ||
|
|
||
| ```ts [onLoaded] | ||
| const { onLoaded } = useScriptTawkTo({ | ||
| propertyId: 'your-property-id', | ||
| widgetId: 'your-widget-id', | ||
| }) | ||
|
|
||
| onLoaded((Tawk_API) => { | ||
| Tawk_API.maximize() | ||
| }) | ||
| ``` | ||
|
|
||
| :: | ||
|
|
||
| ## Reactive state and events | ||
|
|
||
| Tawk's embed script dispatches `window` `CustomEvent`s (`tawkLoad`, `tawkStatusChange`, `tawkChatMaximized`, …) alongside its documented `Tawk_API.onXxx = fn` callback-property API. `useScriptTawkTo()`{lang="ts"} bridges those events into five readonly refs and twenty typed listeners, so you don't have to wire `window.addEventListener` yourself: | ||
|
|
||
| ```vue | ||
| <script setup lang="ts"> | ||
| const { isHidden, isMinimized, isMaximized, chatStatus, unreadCount, onChatStarted, onChatEnded } = useScriptTawkTo({ | ||
| propertyId: 'your-property-id', | ||
| widgetId: 'your-widget-id', | ||
| }) | ||
|
|
||
| onChatStarted(() => { | ||
| console.log('visitor started a chat') | ||
| }) | ||
| onChatEnded(() => { | ||
| console.log('chat ended') | ||
| }) | ||
| </script> | ||
|
|
||
| <template> | ||
| <div v-if="!isHidden"> | ||
| {{ chatStatus }} · {{ unreadCount }} unread · {{ isMinimized ? 'minimized' : isMaximized ? 'maximized' : 'default' }} | ||
| </div> | ||
| </template> | ||
| ``` | ||
|
|
||
| `chatStatus` is Tawk's own online/away/offline operator status (`getStatus()`{lang="ts"}). It's distinct from `status`, the generic script-load state every registry entry exposes. | ||
|
|
||
| Every `onXxx` listener returns a teardown function for use with `onScopeDispose`, mirroring the rest of the registry's event-listener helpers. | ||
|
|
||
| The state refs are a single instance shared by every `useScriptTawkTo()`{lang="ts"} call on the page (there's only ever one Tawk widget), not one instance per call. | ||
|
|
||
| ## Getters | ||
|
|
||
| `proxy` is fire-and-forget: calls queue until the script loads and replay once it does, but their return value is always discarded, even after loading. That's fine for actions like `proxy.maximize()`{lang="ts"}, which don't return anything meaningful anyway, but it can't carry a real synchronous getter. `getWindowType`, `getStatus`, `isChatMaximized`, `isChatMinimized`, `isChatHidden`, `isChatOngoing`, `isVisitorEngaged`, and `widgetPosition` are exposed directly on `useScriptTawkTo()`{lang="ts"}'s return value instead, calling straight through to `window.Tawk_API`: | ||
|
|
||
| ```ts | ||
| const { getStatus, isChatHidden } = useScriptTawkTo({ | ||
| propertyId: 'your-property-id', | ||
| widgetId: 'your-widget-id', | ||
| }) | ||
|
|
||
| getStatus() // 'online' | 'away' | 'offline' | undefined | ||
| isChatHidden() // boolean, false before the widget has loaded | ||
| ``` | ||
|
|
||
| ## Identifying visitors | ||
|
|
||
| `proxy.visitor = {...}` doesn't work for the same reason: unhead's script proxy has no `set` trap, so a property assignment through it never reaches the real `Tawk_API`. Use `setVisitor()`{lang="ts"} instead: | ||
|
|
||
| `setVisitor()`{lang="ts"} is pre-load only. Tawk honors `Tawk_API.visitor` before the embed script loads and ignores it afterwards. If the widget is already loaded (`onLoaded` is set), it warns and does nothing. For post-load identity changes, use `window.Tawk_API.setAttributes({ name, email, phone, hash })`{lang="ts"}: | ||
|
|
||
| ```ts | ||
| const { proxy, setVisitor } = useScriptTawkTo({ | ||
| propertyId: 'your-property-id', | ||
| widgetId: 'your-widget-id', | ||
| }) | ||
|
|
||
| setVisitor({ | ||
| name: 'Jane Doe', | ||
| email: 'jane@example.com', | ||
| // HMAC-SHA256 signature for Secure Mode, generated server-side | ||
| hash: visitorHash, | ||
| }) | ||
| proxy.setAttributes({ plan: 'pro' }) | ||
| proxy.addTags(['vip']) | ||
| ``` | ||
|
|
||
| ## Switching properties at runtime | ||
|
|
||
| ```ts | ||
| const { proxy } = useScriptTawkTo({ | ||
| propertyId: 'your-property-id', | ||
| widgetId: 'your-widget-id', | ||
| }) | ||
|
|
||
| proxy.switchWidget({ propertyId: 'other-property-id', widgetId: 'other-widget-id' }) | ||
| ``` | ||
|
|
||
| ::script-types | ||
| :: | ||
|
|
||
| ## Partytown | ||
|
|
||
| Do not run Tawk.to under Partytown. The widget renders DOM overlays (the chat bubble, prechat and full chat panels) directly, and the `window` `CustomEvent`s the reactive state and listeners depend on aren't configured for worker forwarding. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.