https://discord.gg/botpress logo
I’m having an issue capturing my fbclid and gclid ...
# 🤝help
b
I am reaching out specifically because I just finished a nearly identical implementation for a client tracking UTM parameters, and the process is fresh in my mind. If you can share your current Webflow code, I can review it and provide a more tailored version of the code above to ensure it fits perfectly with your existing cookie logic, avoiding any conflicts. Let me know how it goes https://cdn.discordapp.com/attachments/1416058747046072400/1416061066105978962/SPOILER_FitsWork?ex=68c57923&is=68c427a3&hm=60603e4fcbcafd40c83c21e2fabd5ac6c04dac462c6bd290bdc42d09eb2f061a&
l
Thank you for your message. Here is the code I have on Webflow in my page settings to get fbclid and gclid in the url :
Copy code
<script>
  function getParameterByName(name) {
    const url = window.location.href;
    name = name.replace(/[\[\]]/g, '\\$&');
    const regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
    const results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
  }

  const gclid = getParameterByName('gclid');
  const fbclid = getParameterByName('fbclid');

  if (gclid) {
    document.cookie = 'gclid=' + gclid + '; path=/; max-age=' + 60 * 60 * 24 * 30;
  }
  if (fbclid) {
    document.cookie = 'fbclid=' + fbclid + '; path=/; max-age=' + 60 * 60 * 24 * 30;
  }

  function getCookie(name) {
    const value = '; ' + document.cookie;
    const parts = value.split('; ' + name + '=');
    if (parts.length === 2) return parts.pop().split(';').shift();
  }

  document.addEventListener('DOMContentLoaded', function () {
    const gclidCookie = getCookie('gclid');
    const fbclidCookie = getCookie('fbclid');

    if (gclidCookie) {
      const gclidInput = document.querySelector('input[name="gclid"]');
      if (gclidInput) gclidInput.value = gclidCookie;
    }
    if (fbclidCookie) {
      const fbclidInput = document.querySelector('input[name="fbclid"]');
      if (fbclidInput) fbclidInput.value = fbclidCookie;
    }
  });
</script>
And here is my Botpress code in the settings project :
Copy code
<script>
  function getCookie(name) {
    const value = '; ' + document.cookie;
    const parts = value.split('; ' + name + '=');
    if (parts.length === 2) return parts.pop().split(';').shift();
    return null;
  }

  window.botpress.on('webchat:initialized', () => {
    const gclid = getCookie('gclid') || null;
    const fbclid = getCookie('fbclid') || null;

    function sendClidsPayload(attempt = 0) {
      try {
        window.botpress.sendEvent({
          type: 'custom',              // nom générique d’évènement custom
          payload: { gclid, fbclid }   // ce que l’on veut faire remonter au bot
        });
      } catch (e) {
        if (attempt < 20) {
          setTimeout(() => sendClidsPayload(attempt + 1), 100);
        }
      }
    }

    setTimeout(() => {
      window.botpress.open();
      sendClidsPayload();
    }, 2500);
  });
</script>
@big-lifeguard-56833