Chat widget JavaScript API reference
The install code defines window.Click2Reply. Until the widget has loaded, that global is a stub
that queues on, once, off and call; once it loads, the queued calls run in order and every
method below is available. So call("<name>", ...) is the form that works at any time, and the
direct methods are for code that runs after the ready event.
// Safe anywhere, even before the widget has loaded.Click2Reply.call("set_customer_email", "ana@example.com");
// The direct form, once the widget is ready.Click2Reply.on("ready", () => Click2Reply.maximize());Configuration
Section titled “Configuration”window.__c2r is read once, when the widget loads:
| Field | Type | What it does |
|---|---|---|
license | number | Your license number. Required. |
group | string | The group new chats from this page go to, by its id. |
customer | { name?, email? } | The customer’s name and email, prefilled. |
params | object | Session variables, { "plan": "pro" }: agents see them next to the chat. |
identity_token | string | A token your server signed for the signed-in user; see identity tokens. |
async_init | boolean | Load nothing until the page calls Click2Reply.init(). |
Methods
Section titled “Methods”| Method | call name | What it does |
|---|---|---|
maximize() | maximize | Open the chat window. |
minimize() | minimize | Close the window back to the chat button. |
hide() | hide | Hide the widget, chat button included. |
destroy() | destroy | Remove the widget from the page for this page view. |
setCustomerName(name) | set_customer_name | Set the customer’s name, up to 120 characters. |
setCustomerEmail(email) | set_customer_email | Set the customer’s email. |
setSessionVariables(vars) | set_session_variables | Replace the session variables. |
updateSessionVariables(vars) | update_session_variables | Add to or change the session variables, keeping the rest. |
setCart({ value, currency, items }) | set_cart | The cart agents see: value in the currency’s main unit, currency an ISO 4217 code, items a count. |
track(event) | track | Report a sale or a goal; see tracking sales and goals. |
setSounds(on) | set_sounds | Turn the widget’s sounds on or off. |
setCookieConsent(granted) | set_cookie_consent | With the organization’s cookie-consent setting on, the widget forgets the visitor between pages unless the page calls this with true, on every page load once the visitor has consented. false withdraws consent and removes what the widget stored. |
get(what) | - | Read "state", "customer_data" or "chat_data"; only after ready. |
on(event, fn), once(event, fn), off(event, fn) | - | Listen to the events below. |
init() | - | Load the widget when the config says async_init. |
Session variables are up to 50 pairs, names up to 64 characters and values up to 512; longer ones are cut.
What get returns
Section titled “What get returns”Click2Reply.get("state");// { availability: "online" | "offline", visibility: "maximized" | "minimized" | "hidden" }
Click2Reply.get("customer_data");// { id, name, email, isReturning, status: "browsing" | "invited" | "queued" | "chatting", sessionVariables }
Click2Reply.get("chat_data");// { chatId, threadId } while a chat is open, otherwise nullTracking sales and goals
Section titled “Tracking sales and goals”track credits a sale or a goal to the chat that came before it, within 7 days:
Click2Reply.call("track", { type: "order_created", order_id: "A-1001", total_cents: 4990, currency: "USD" });Click2Reply.call("track", { type: "product_added", product: { id: "sku-42", name: "Mug", quantity: 1, price_cents: 1290 }, currency: "USD" });Click2Reply.call("track", { type: "goal", name: "signup" });Amounts are integer cents. product_removed takes the same fields as product_added, and
order_created takes an optional products list. A server can report the same events with
POST /v1/track.
Events
Section titled “Events”| Event | Its argument |
|---|---|
ready | { state, customerData }, as get returns them. |
availability_changed | { availability: "online" | "offline" } |
visibility_changed | { visibility: "maximized" | "minimized" | "hidden" } |
customer_status_changed | { status: "browsing" | "invited" | "queued" | "chatting" } |
new_event | { type, author: { id, type: "agent" } } when an agent’s message or file arrives. |
greeting_displayed, greeting_hidden | { id, uniqueId } of the greeting. |
rich_message_button_clicked | { postbackId } of the button. |
form_submitted | { type: "prechat" | "postchat" | "ask_for_email" | "custom" } |
rating_submitted | "good", "bad", or "none" when a rating is taken back. |
Click2Reply.on("new_event", (e) => { if (document.hidden) document.title = "New message";});
Click2Reply.on("availability_changed", ({ availability }) => { document.querySelector("#chat-button").hidden = availability === "offline";});Identity tokens
Section titled “Identity tokens”customer names the visitor; an identity token proves who they are, so a signed-in user sees their
chat history on every device. Sign a JWT on your server for the signed-in user and put it in
window.__c2r.identity_token:
- Algorithm: ES256 or RS256. Add the public key, as a JWK, in the agent app or with
POST /v1/customer-token/keys. Click2Reply never holds a key that can sign. - Claims:
subis your user id (1 to 128 characters),audis your license number as a string,expis at most 24 hours ahead.nbfis honored when present. - Header:
kidmay name the key; without it every key of that algorithm is tried.
Never sign the token in the browser: anyone could then claim to be anyone.