```
/**
 * Copies GA4 client/session IDs, the tg_ft / tg_lt attribution cookies and the
 * Rewardful campaign coupon into hidden fields of the signup form, so they reach
 * the server with the AJAX POST. See "Teamgate CRM: trial and demo tracking"
 * brief, section 4.3.
 *
 * The form is submitted via jQuery (.submit() / .trigger('submit')), which
 * does not dispatch a native submit event, so scripts-old.js must call
 * window.tgFillSignupFields() right before $form.serialize().
 */
(function () {
  var MID = 'KPFTLTT70P';

  function ck(n) {
    var m = document.cookie.match('(?:^|; )' + n.replace(/[.$?*|{}()\[\]\\\/+^]/g, '\\$&') + '=([^;]*)');
    try {
      return m ? decodeURIComponent(m[1]) : null;
    } catch (e) {
      return null;
    }
  }

  // _ga = GA1.1.<random>.<first_seen_unix> -> client_id = last two parts
  function clientId() {
    var v = ck('_ga');
    if (!v) return null;
    var p = v.split('.');
    return p.length >= 4 ? p.slice(-2).join('.') : null;
  }

  // _ga_<MID> = GS2.1.s<session_id>$o... (current) or GS1.1.<session_id>.... (legacy)
  function sessionId() {
    var v = ck('_ga_' + MID);
    if (!v) return null;
    var m = v.match(/^GS2\.\d+\.s(\d+)/);
    if (m) return m[1];
    var p = v.split('.');
    return (p[0] === 'GS1' && /^\d+$/.test(p[2])) ? p[2] : null;
  }

  function setField(form, name, val) {
    var el = form.querySelector('input[name="' + name + '"]');
    if (!el) {
      el = document.createElement('input');
      el.type = 'hidden';
      el.name = name;
      form.appendChild(el);
    }
    el.value = val == null ? '' : val;
  }

  function fill() {
    var form = document.querySelector('.registration-form form');
    if (!form) return;
    var cid = clientId(), sid = sessionId();
    setField(form, 'data[ga_client_id]', cid || '');
    setField(form, 'data[ga_session_id]', sid || '');
    setField(form, 'data[ga_id_source]', cid ? 'cookie' : 'none'); // server generates an id when 'none'
    setField(form, 'data[attr_ft]', ck('tg_ft') || '');
    setField(form, 'data[attr_lt]', ck('tg_lt') || '');
    setField(form, 'data[signup_page]', location.origin + location.pathname);
    // Stripe coupon of the visitor's Rewardful campaign, null when not referred or the campaign has none
    var rw = window.Rewardful;
    setField(form, 'data[coupon]', rw && rw.coupon && rw.coupon.id ? rw.coupon.id : '');
  }

  window.tgFillSignupFields = fill;

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', fill);
  } else {
    fill();
  }
})();

```