How to pass URL parameters to a chat scenario

Hello everyone,

I frequently find myself returning to this particular use case and having to search through old code to locate it. To make it easier for me to find in the future, I will leave it here as a post.

The following JavaScript code, when placed somewhere above the chat snippet on the page, will extract URL parameters and store them in an array called “url_params”:

<script language="JavaScript">
function getQueryString() {
  var result = {};
  var queryString = location.search.slice(1);
  var re = /([^&=]+)=([^&]*)/g;
  var m;
  
  while (m = re.exec(queryString)) {
    result[decodeURIComponent(m[1])] = decodeURIComponent(m[2]);
  }
  return result;
}

var url_params = getQueryString();
</script>

Instead of explicitly specifying the parameters in the chat snippet like this:
(from here: 5.3:Tutorials-for-admins/Chat/PassJavascriptVariables - Bright Pattern Documentation)

parameters: {
variable_name: 'Value',
}

In the chat snippet, assign the previously created “url_params” array to the parameters:

parameters: url_params

By doing this, the parameter array containing the URL variables will be sent as scenario invocation parameters, accessible as $(item.externalChatData.paramname).

4 Likes

Very useful, thank you.

Just what I was looking for, thank you!