Hoe voegt u de Asyntai AI-chatbot toe aan Sanity

Stapsgewijze handleiding voor websites op basis van Sanity

Insluitcode ophalen

Belangrijk: Sanity is een headless CMS dat inhoud levert via API's. De chatbotcode moet worden toegevoegd aan uw frontend-applicatie (Next.js, Gatsby, Nuxt, gewone HTML, enz.), niet aan de Sanity Studio zelf.

Stap 1: Uw insluitcode ophalen

Ga eerst naar uw Asyntai Dashboard en scroll naar het gedeelte "Insluitcode". Kopieer uw unieke insluitcode die er als volgt uitziet:

<script async src="https://asyntai.com/static/js/chat-widget.js" data-asyntai-id="YOUR_WIDGET_ID"></script>

Opmerking: De bovenstaande code is slechts een voorbeeld. U moet uw eigen unieke insluitcode kopiëren vanuit uw Dashboard omdat deze uw persoonlijke widget-ID bevat.

Stap 2: Toevoegen aan Next.js-frontend (meest voorkomend)

Als uw Sanity-site Next.js gebruikt (de meest voorkomende combinatie), voeg dan de chatbot toe aan uw hoofdlayout:

App Router (Next.js 13+)

Open app/layout.js (of app/layout.tsx) en voeg het Script-component toe:

import Script from 'next/script' export default function RootLayout({ children }) { return ( <html lang="en"> <body> {children} <Script src="https://asyntai.com/static/js/chat-widget.js" data-asyntai-id="YOUR_WIDGET_ID" strategy="afterInteractive" /> </body> </html> ) }

Pages Router

Als u de Pages Router gebruikt, open pages/_app.js:

import Script from 'next/script' export default function MyApp({ Component, pageProps }) { return ( <> <Component {...pageProps} /> <Script src="https://asyntai.com/static/js/chat-widget.js" data-asyntai-id="YOUR_WIDGET_ID" strategy="afterInteractive" /> </> ) }

Tip: De optie strategy="afterInteractive" zorgt ervoor dat de chatbot wordt geladen nadat de pagina interactief wordt, wat de beste gebruikerservaring biedt zonder de laadprestaties van de pagina te beïnvloeden.

Alternatieve methode 1: Gatsby-frontend

Als uw Sanity-site Gatsby gebruikt, voeg dan het script toe via gatsby-ssr.js:

// gatsby-ssr.js import React from 'react' export const onRenderBody = ({ setPostBodyComponents }) => { setPostBodyComponents([ <script key="asyntai" async src="https://asyntai.com/static/js/chat-widget.js" data-asyntai-id="YOUR_WIDGET_ID" /> ]); };

Opmerking: U moet dezelfde code ook toevoegen aan gatsby-browser.js als u wilt dat de widget blijft bestaan tijdens navigatie aan de client-side. U kunt het ook toevoegen aan uw hoofdlayout-component.

Alternatieve methode 2: Nuxt.js-frontend

Als uw Sanity-site Nuxt.js gebruikt, voeg dan het script toe in nuxt.config.js (of nuxt.config.ts):

// nuxt.config.ts export default defineNuxtConfig({ app: { head: { script: [ { src: 'https://asyntai.com/static/js/chat-widget.js', 'data-asyntai-id': 'YOUR_WIDGET_ID', async: true } ] } } })

Alternatieve methode 3: Gewone HTML-frontend

Als uw Sanity-site gewone HTML of een statische sitegenerator gebruikt die de Sanity API consumeert:

  1. Open uw hoofd-HTML-bestand (meestal index.html)
  2. Voeg de insluitcode toe net vóór de afsluitende </body>-tag:
<script async src="https://asyntai.com/static/js/chat-widget.js" data-asyntai-id="YOUR_WIDGET_ID"></script> </body> </html>

Alternatieve methode 4: React (Vite of Create React App)

Als uw frontend React gebruikt met Vite of Create React App:

Voeg het script toe aan index.html in de map public/:

<!DOCTYPE html> <html lang="en"> <head> <!-- ... existing head content ... --> </head> <body> <div id="root"></div> <script async src="https://asyntai.com/static/js/chat-widget.js" data-asyntai-id="YOUR_WIDGET_ID"></script> </body> </html>

Of gebruik een useEffect-hook in uw rootcomponent:

// App.jsx or App.tsx import { useEffect } from 'react'; function App() { useEffect(() => { const script = document.createElement('script'); script.src = 'https://asyntai.com/static/js/chat-widget.js'; script.setAttribute('data-asyntai-id', 'YOUR_WIDGET_ID'); script.async = true; document.body.appendChild(script); return () => { document.body.removeChild(script); }; }, []); return ( // ... your app content ); }

Stap 3: Deployen en verifiëren

Na het toevoegen van de code aan uw frontend-applicatie:

  1. Implementeer uw frontend bij uw hostingprovider (Vercel, Netlify, enz.)
  2. Open uw live site in een nieuw browsertabblad
  3. U zou de chatwidgetknop in de rechteronderhoek moeten zien
  4. Klik erop om te controleren of deze correct opent en functioneert

Ziet u de widget niet? Zorg ervoor dat u YOUR_WIDGET_ID hebt vervangen door uw daadwerkelijke widget-ID uit het dashboard. Controleer of het script wordt geladen in het Netwerk-tabblad van uw browser (F12 > Netwerk). Controleer of de frontend opnieuw is geïmplementeerd na het toevoegen van de code. Zorg er voor Next.js voor dat u het Script-component van next/script gebruikt, en niet een gewone <script>-tag. Wis uw browsercache of test in incognitomodus.