Blog

How to add a custom AI chatbot to your Drupal website

· 7 min read

Drupal is built for sites that need care: government services, universities, membership organisations. It's also careful about scripts, so unlike most platforms there's no box in the admin where you paste one. Adding an AI chat assistant still takes about ten minutes, either with a small module or from your theme.

This guide uses Pixexl's assistant as the example, but the steps are the same for most chat tools that give you a script tag.

Before you start

You'll need three things:

  • An administrator account, with permission to install modules (option 1) or access to the site's code (option 2).
  • Your chat's embed code. In Pixexl, it's on your project's Settings tab, under the integration code. It looks something like this:
<script
  src="https://…/pixe-widget.js"
  data-project-id="your-project-id"
  data-api="https://…"
  async
></script>

Keep it open: you'll need its three values, the script's address (src), your project ID and the API address.

  • Your domain added to the project. For security, the chat only runs on domains you've allowed. If the site also runs on a staging or development address, add that too.

It's also worth giving the assistant something to answer from first: add your site's pages as its knowledge (see What to give your AI assistant so it answers well).

Why not a block?

The obvious idea is a custom block with the script in its body. Drupal's text formats remove <script> tags, on purpose, and the editor often removes them too. You could create a text format that allows them, but every editor who can use that format could then add scripts to the site. The two options below avoid that.

Option 1: the Asset Injector module (no code)

Asset Injector is a widely used contributed module that adds JavaScript or CSS to your pages from the admin.

  1. Install the module, with Composer (composer require drupal/asset_injector) or the way your site installs modules, and enable Asset Injector and its JS Injector part.
  2. Go to Configuration → Development → Asset Injector → JS Injector and choose Add JS Injector.
  3. Give it a label, like "Pixe chat", and paste this into the code box, with the three values from your embed code:
(function () {
  var s = document.createElement('script');
  s.src = 'https://…/pixe-widget.js';
  s.async = true;
  s.dataset.projectId = 'your-project-id';
  s.dataset.api = 'https://…';
  (document.body || document.head).appendChild(s);
})();
  1. Leave the conditions empty so it runs on every page, or limit it to your front-end theme so it doesn't show in the admin.
  2. Save, and clear Drupal's cache (Configuration → Development → Performance → Clear all caches).

The injector takes JavaScript rather than HTML, which is why the snippet builds the script tag itself. It loads exactly the same chat.

Option 2: from your theme (for developers)

If you maintain the theme, load the script as a library. In your theme's <theme>.libraries.yml:

pixe-chat:
  js:
    https://…/pixe-widget.js:
      type: external
      attributes:
        async: true
        data-project-id: your-project-id
        data-api: 'https://…'

Then add it to every page in <theme>.info.yml:

libraries:
  - <theme>/pixe-chat

Clear the cache with drush cr. Do this in your own theme or a subtheme, never in a contributed theme like Olivero, which an update would overwrite.

Will it clash with my theme?

It shouldn't. The chat draws itself in its own sealed-off area of the page, so your theme's styles can't change how it looks, and its styles can't leak into your site. Its colours, greeting and position come from the Widget tab in your Pixexl dashboard, so you can change them later without a deploy.

Cookie consent

The chat remembers a visitor's conversation in their browser, so it's covered by your cookie rules. Either keep the chat's own cookie notice (on by default, linked to your privacy policy from the Widget tab), or, if you run a consent module such as EU Cookie Compliance, turn the chat's notice off, add the consent setting to the script (s.dataset.consent = 'pending' in option 1, data-consent: pending in option 2) and have your consent tool call PixeWidget.grantConsent() when the visitor agrees. Pick one, not both.

It's not showing up: what to check

  • Caching. Clear Drupal's cache, and your CDN's or reverse proxy's (Varnish, Cloudflare) if you have one.
  • JavaScript aggregation. If the chat appears only with aggregation off (Performance → Aggregate JavaScript files), rebuild the cache with it on; the script itself is external and isn't aggregated.
  • The injector's conditions. Check it's not limited to some pages or roles, and that the JS Injector module is enabled.
  • The domain. A staging or development address needs to be in the project's allowed domains.
  • A Content Security Policy. If your site sends one (the Security Kit or CSP modules), allow the chat's script and API addresses.
  • The chat is switched off. Check that "Show the chat on my site" is on in the Widget tab.

If it still won't appear, open your browser's developer console (right-click, Inspect, then Console). The chat leaves a short message there explaining why it didn't load.

After it's live

Ask it the questions your visitors ask, read the real conversations once a week, and add forms so a question can turn into an enquiry or an application without leaving the chat (see How to turn website chats into bookings and enquiries).

Every Pixexl plan includes the assistant, and your first 2 weeks are free, so you can try it on your Drupal site with real visitors before you decide. Meet Pixe AI to see what it can do.

Drupal chatbot questions

Do I need a Drupal chatbot module?

Not a chatbot module. The chat is one script, so a general module for adding JavaScript (such as Asset Injector) or a few lines in your theme is all it takes.

Can I paste the script into a block?

Usually not reliably. Drupal's text formats strip <script> tags for security, and loosening a format to allow them lets every editor who uses it add scripts too. A module or the theme is safer.

Does it work on Drupal 10 and 11?

Yes. Both options in this guide work the same on Drupal 10 and Drupal 11.

Will it slow down my site?

Not noticeably. The script loads asynchronously, so the browser fetches it without holding up the rest of your page, and nothing is added to Drupal's database apart from the module's own setting.

Is there a free trial?

Yes. Every Pixexl plan includes the AI chat assistant, and your first 2 weeks are free, so you can try it on your Drupal site with real visitors. See pricing for the plans.

More articles

· 7 min read

How to add a custom AI chatbot to your Shopify store

Shopify shoppers ask the same questions every day, about sizes, shipping and returns. Here's how to add an AI chat assistant that answers them from your own store's pages, with one line of code in your theme.

AI chatShopify

· 7 min read

How to add a custom AI chatbot to your WooCommerce store

WooCommerce runs on WordPress, so adding an AI chat assistant takes one line of code. Here's how to do it, what a store's assistant should know, and the caching settings that trip stores up.

AI chatWooCommerceWordPress

· 6 min read

How to add a custom AI chatbot to your Wix website

Wix lets you add a custom AI chat assistant to every page with its Custom Code setting, in about ten minutes. Here's how, which plan you need, and what to check if the chat doesn't appear.

AI chatWix

Let your website answer for you.

We set up your assistant and site with you, usually within a day. Then your first 2 weeks are free, with real visitors.