CMS Development

omaklad
Member

Collecting Session URL

Hello,

I have a chatbot created using chatflows on my website, I need to be able to capture a specific parameter from the session URL and save it to the contact record created by the bot.

I found a post talking about saving the param as a cookie in my website code then pass it to ._hsq and if the variable matches a field on the contact proprety it should update it automatically, but it didn't.

0 Upvotes
2 Replies 2
Jaycee_Lewis
Community Manager
Community Manager

Collecting Session URL

Hey, @omaklad 👋 welcome to our community. Thanks for your post. I think our community members need additional information before they can help. Can you share:

  • the post you are referring to
  • code example(s) of how you implemented this on your end

Thank you! — Jaycee


HubSpot’s AI-powered customer agent resolves up to 50% of customer queries instantly, with some customers reaching up to 90% resolution rates.
Learn More.


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !
0 Upvotes
omaklad
Member

Collecting Session URL

Hi @Jaycee_Lewis 

I can't find the exact post, but I basically 

I put this code in my browser:
first to create the cookie:

<script>
  function getCookie(name) {
    var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
    return match ? decodeURIComponent(match[2]) : null;
  }

  var aid = getCookie('aid');

  if (aid && window._hsq) {
    console.log('%c[HubSpot AID] Success:', 'color: green; font-weight: bold;', 'Found aid =', aid, '| Sending to HubSpot via _hsq');
    window._hsq.push(['identify', {
      aid: aid
    }]);
  } else if (!aid) {
    console.warn('%c[HubSpot AID] Warning:', 'color: orange; font-weight: bold;', 'No aid value found in cookie.');
  } else if (!window._hsq) {
    console.error('%c[HubSpot AID] Error:', 'color: red; font-weight: bold;', 'HubSpot tracking object (_hsq) not found.');
  }
</script>

 

Then to get the cookie and send it to hsq:

<script>
  function getCookie(name) {
    var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
    return match ? decodeURIComponent(match[2]) : null;
  }

  var aid = getCookie('aid');
  var maxAttempts = 10;
  var attempts = 0;

  var interval = setInterval(function () {
    if (typeof window._hsq !== 'undefined') {
      if (aid) {
        console.log('%c[HubSpot AID] Success:', 'color: green; font-weight: bold;', 'Found aid =', aid, '| Sending to HubSpot via _hsq');
        window._hsq.push(['identify', { aid: aid }]);
      } else {
        console.warn('%c[HubSpot AID] Warning:', 'color: orange; font-weight: bold;', 'No aid value found in cookie.');
      }
      clearInterval(interval);
    } else {
      attempts++;
      if (attempts >= maxAttempts) {
        console.error('%c[HubSpot AID] Error:', 'color: red; font-weight: bold;', 'HubSpot tracking object (_hsq) not found after 10 attempts.');
        clearInterval(interval);
      }
    }
  }, 500); // Check every 500ms
</script>

And assumed once the contact is created hubspot will be able to link that aid value to the customer contact field i created thats called aid too.

0 Upvotes