Botpress v2.2 - Collecting the page URL, browser language, and query parameters

by Zannax_Botpress

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css">
<!doctype html>
<html lang="en">
  <!-- 
    Thank you to the Botpress community (https://discord.com/channels/1108396290624213082)
  -->
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
  </head>

  <body>
    <script src="https://cdn.botpress.cloud/webchat/v2.2/inject.js"></script>
    <script src="https://files.bpcontent.cloud/yyyy/mm/dd/hh/yyyymmddhhnnss-XXXXXXXX.js"></script>
    <script defer>
      (function () {
        function getUrlData() {
          return {
            origin: window.location.href, // URL della pagina
            language: navigator.language, // Prima lingua di navigazione del browser
            referrer: document.referrer, // URL della pagina che ha collegato alla pagina corrente
          }
        }

        function updateUserData(logMessage) {
          if (logMessage) {
            console.log(logMessage)
          }

          if (typeof window.botpress === "undefined") {
            console.log("botpress is undefined, retrying...");
            setTimeout(updateUserData, 5);
            return;
          }

					// Read the webchat user data.
          // If it is undefined or the data is different, update the user and send the event zx:urlDataChanged
          window.botpress
            .getUser()
            .then((botpressUser) => {
              const urlData = getUrlData()
              const userData = botpressUser.data || {}

							// are origin or referrer changed?
              if (
                userData.origin !== urlData.origin ||
                userData.referrer !== urlData.referrer
              ) {
                window.botpress.updateUser({ data: urlData })
                window.botpress.sendEvent({ type: "zx:urlDataChanged", urlData })

               ...

JavaScript

/**
* To try your theme comment lines below and at line 11 replace <code>https://files.bpcontent.cloud/yyyy/mm/dd/hh/yyyymmddhhnnss-XXXXXXXX.js 
*/
window.botpress.init({
  "configuration": {
    "botAvatar":...