Add the chat widget to React or Next.js
The widget keeps one chat across route changes on its own, so load it once for the whole app, never per page or per component render.
The HTML shell
Section titled “The HTML shell”Paste the install code from Settings > Website widget into the HTML every route shares:
index.html for a Vite project, the root layout for Next.js.
<!-- Click2Reply chat widget --><script>window.__c2r = {"license":12345678};;(function(w,d,n){if(w[n])return;var q=[],f=function(m){return function(){q.push([m,[].slice.call(arguments)])}};w[n]={_q:q,on:f("on"),once:f("once"),off:f("off"),call:f("call"),get:function(){throw new Error("[Click2Reply] get() is available after the widget loads")},init:function(){var s=d.createElement("script");s.async=!0;s.src="https://w.click2reply.com/w.js";d.head.appendChild(s)}};!(w.__c2r&&w.__c2r.async_init)&&w[n].init()})(window,document,"Click2Reply");</script><noscript><a href="https://w.click2reply.com/page?license=12345678" rel="nofollow">Chat with our support team</a></noscript>In a Next.js root layout, put the two script lines in a <Script id="c2r" strategy="afterInteractive">
from next/script, so the code runs once for the app rather than on every navigation.
Loading it when your app decides
Section titled “Loading it when your app decides”To load the widget only once your code says so, for example after the visitor accepts cookies, add
async_init: true to the config. The shell then holds a small stub, and nothing loads until you
call Click2Reply.init():
<script>window.__c2r = {"license":12345678,"async_init":true};;(function(w,d,n){if(w[n])return;var q=[],f=function(m){return function(){q.push([m,[].slice.call(arguments)])}};w[n]={_q:q,on:f("on"),once:f("once"),off:f("off"),call:f("call"),get:function(){throw new Error("[Click2Reply] get() is available after the widget loads")},init:function(){var s=d.createElement("script");s.async=!0;s.src="https://w.click2reply.com/w.js";d.head.appendChild(s)}};!(w.__c2r&&w.__c2r.async_init)&&w[n].init()})(window,document,"Click2Reply");</script>import { useEffect } from "react";
export function ChatWidget({ consented }: { consented: boolean }) { useEffect(() => { if (consented) window.Click2Reply?.init(); }, [consented]); return null;}The widget boots once however often init() runs, so a component that mounts twice is safe.
If your organization’s cookie-consent setting is on, the widget remembers the visitor across pages
only on a page load that calls Click2Reply.call("set_cookie_consent", true). Call it at start-up
whenever your stored consent says yes, and again the moment the visitor consents, whether or not you
also delay init().
The signed-in user
Section titled “The signed-in user”The stub queues call(), so it works before the widget has loaded and runs once it has. Pass the
user when they sign in:
window.Click2Reply?.call("set_customer_name", user.name);window.Click2Reply?.call("set_customer_email", user.email);To prove who the user is, rather than only name them, sign an identity token on your server and
pass it as identity_token in window.__c2r; see the widget API.
In TypeScript, declare the globals once:
declare global { interface Window { __c2r?: { license: number; group?: string; customer?: { name?: string; email?: string }; params?: Record<string, string>; identity_token?: string; async_init?: boolean }; Click2Reply?: { init(): void; maximize(): void; minimize(): void; call(name: string, ...args: unknown[]): void }; }}