D
D
Dim2020-05-22 20:32:57
JavaScript
Dim, 2020-05-22 20:32:57

How to pass cookie value to Bitrix24 CRM form?

The site contains an online chat widget and a CRM form.
The documentation has an example of how to pass a parameter to the Bitrix24 form by modifying the open lines widget code:

<script data-skip-moving="true"> (function(w,d,u,b){ s=d.createElement('script');
r=1*new Date();s.async=1;s.src=u+'?'+r; h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(s,h); })
(window,document,'http://cp.bx/upload/crm/site_button/loader_14_i4bdl2.js'); 
window.Bitrix24WidgetObject = window.Bitrix24WidgetObject || {};
 window.Bitrix24WidgetObject.handlers = { 'form-init': function(form){ form.presets = { 'my_cookie1': 'test_value', }; } };


I tried to just insert the widget code before the form, and inside the form I added the personalization tag %my_cookie1%.
When submitting the form, test_value does not come to the form ... Here it stalled.

The second step would be to get the value of the y_clid cookie and pass it to the form instead of test_value.
The code should help, but I don't know how to tie it all together
function getCookie(name) {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(';').shift();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dim, 2020-05-22
@Dee3

Figured it out myself. The widget is not particularly needed here, it is enough to modify the form code. Works for old forms (not 2.0)
add the function code at the beginning of the page, then add a request for this function in the form code.

<script>function getCookie(name){const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(';').shift();}
</script>

Then add a line with additional parameters (presets)
b24form({"id": "4",
    "lang": "ru",
    "sec": "d143a7",
    "type": "inline",
  "presets":{"mycookie": getCookie('_ym_uid')}});
</script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question