标准代码片段会随页面一起加载聊天窗口,所以访客一打开第一个页面,tufu 的服务器就会收到他们的地址和浏览器信息。如果你的网站要先征求同意,或者你希望在有人想聊天之前什么都不加载,可以用下面两种代码片段之一,替换 两段标准代码(script 标签和聊天元素)。每种都由聊天元素加一行脚本组成。是否需要征得同意,请咨询你自己的顾问:tufu 不会替你判断。
点击后加载#
在访客点击之前,只显示一个普通按钮,不会有任何内容发往 tufu。点击后才会加载聊天窗口并打开聊天,此后显示的是你机器人自己的外观。
<tufu-chat team="your-team" bot="your-bot" data-load="click" button-text="Chat with us"></tufu-chat>
<script>(function(){var e=!1,t=!1,n;function r(e){if(!t){t=!0;var r=document.createElement(`script`);r.type=`module`,r.src=`https://tufu.ai/widget/v1/tufu-chat.esm.js`,document.head.appendChild(r),e&&customElements.whenDefined(`tufu-chat`).then(function(){n.show()})}}function i(){var e=document.createElement(`button`);e.type=`button`,e.textContent=n.getAttribute(`button-text`)||`Chat`,e.setAttribute(`data-tufu-launcher`,``),e.style.cssText=`position:fixed;right:20px;bottom:20px;z-index:2147483000;padding:12px 18px;border:0;border-radius:999px;background:#111827;color:#fff;font:600 14px system-ui,sans-serif;cursor:pointer`,e.addEventListener(`click`,function(){e.remove(),r(!0)}),document.body.appendChild(e)}function a(){n=n||document.querySelector(`tufu-chat[data-load],tufu-chat[data-consent]`),n&&(n.getAttribute(`data-consent`)!==`wait`||e)&&(n.getAttribute(`data-load`)===`click`?!document.querySelector(`[data-tufu-launcher]`)&&!t&&i():r(!1))}window.tufu=window.tufu||{},window.tufu.consent=function(t){e=t!==!1,e&&a()},document.readyState===`loading`?document.addEventListener(`DOMContentLoaded`,a):a()})();</script>
用 button-text 修改按钮上的文字。点击之前,你在工作室里的设置不会对它生效。
同意后加载#
在你的页面调用 tufu.consent(true) 之前,不显示也不加载任何内容。每次页面加载时,只要访客已经同意,就调用它。tufu.consent(false) 不会有任何后续动作:如果访客撤回同意后要移除已加载的聊天,请重新加载页面。
<tufu-chat team="your-team" bot="your-bot" data-consent="wait"></tufu-chat>
<script>(function(){var e=!1,t=!1,n;function r(e){if(!t){t=!0;var r=document.createElement(`script`);r.type=`module`,r.src=`https://tufu.ai/widget/v1/tufu-chat.esm.js`,document.head.appendChild(r),e&&customElements.whenDefined(`tufu-chat`).then(function(){n.show()})}}function i(){var e=document.createElement(`button`);e.type=`button`,e.textContent=n.getAttribute(`button-text`)||`Chat`,e.setAttribute(`data-tufu-launcher`,``),e.style.cssText=`position:fixed;right:20px;bottom:20px;z-index:2147483000;padding:12px 18px;border:0;border-radius:999px;background:#111827;color:#fff;font:600 14px system-ui,sans-serif;cursor:pointer`,e.addEventListener(`click`,function(){e.remove(),r(!0)}),document.body.appendChild(e)}function a(){n=n||document.querySelector(`tufu-chat[data-load],tufu-chat[data-consent]`),n&&(n.getAttribute(`data-consent`)!==`wait`||e)&&(n.getAttribute(`data-load`)===`click`?!document.querySelector(`[data-tufu-launcher]`)&&!t&&i():r(!1))}window.tufu=window.tufu||{},window.tufu.consent=function(t){e=t!==!1,e&&a()},document.readyState===`loading`?document.addEventListener(`DOMContentLoaded`,a):a()})();</script>
请把这段代码放在你的同意管理工具的脚本之前,这样工具调用时 tufu.consent 已经存在。两者也可以一起用:data-consent="wait" data-load="click" 只会在同意之后才显示按钮。
Cookiebot#
<script>
window.addEventListener('CookiebotOnConsentReady', function () {
if (Cookiebot.consent.preferences) tufu.consent(true);
});
</script>
OneTrust#
你的类别 ID 在 OneTrust 设置里;C0003(功能性)通常是默认值。
<script>
function OptanonWrapper() {
if (window.OnetrustActiveGroups && OnetrustActiveGroups.indexOf('C0003') !== -1) tufu.consent(true);
}
</script>
Google Consent Mode#
Consent Mode 没有自己的事件。在你的同意管理工具更新它的地方,同时调用 tufu:
gtag('consent', 'update', { functionality_storage: 'granted' });
tufu.consent(true);
为什么脚本要放在你的页面里#
浏览器会在页面上任何内容能读取属性之前就抓取 <script src>,所以加载器必须放在你自己的页面里:指向 tufu 的 src 本身就已经是一次发往 tufu 的请求。这行代码是一段简短的压缩加载器,只会在点击或同意之后才加入 tufu 的脚本。如果你的网站有内容安全策略(Content Security Policy),内联加载器需要你的 nonce,而且你的策略必须允许加载器加入的 tufu 脚本。
聊天会在浏览器里保存什么#
聊天打开之前,聊天窗口不会保存任何标识符。如果你的机器人有问候气泡,标准代码片段会保存一条简短的记录,说明它已经显示过(访客关闭后,还会记录已关闭),这样它就不会在每个页面重复出现;使用上面的代码片段时,这些在点击或同意之前都不会运行。聊天打开后,它会在聊天自己的框架中保存一个随机的访客 ID,这样回访的访客可以接着之前的会话继续聊。