
  let myIframel;
  let frameStatus = false;
  window.onmessage = function (event) {
    if (event.data == 'reply') {
      frameStatus = true;
      myIframe = document.getElementsByClassName("ssInsertedFrame")[0]
      myIframe.contentWindow.postMessage(window.scope, "*");
    }
 };
  const findQuery = [
    "utm_source",
    "utm_medium",
    "utm_campaign",
    "utm_content",
    "utm_term",
  ];


  function setQuery(queryVars) {
    const queryString = window.location.search;
    const urlParams = new URLSearchParams(queryString);


    queryVars.map(function (fieldName) {
      let paraValue = urlParams.get(fieldName);
      paraValue ? (setScopeParam(fieldName, paraValue)) : null;
    });
  }


  function getIp() {
    fetch("https://api.ipify.org/?format=json")
      .then((results) => results.json())
      .then((data) => {
        setScopeParam("client_ip_address", data.ip);
      })
      .catch((error) => {
        console.error(error);
      });
  }


  function getUserClient() {
    let userClient = navigator.userAgent;
    setScopeParam("client_user_agent", userClient);
  }
  getIp();
  getUserClient();


  function getCookie(name) {
    let matches = document.cookie.match(
      new RegExp(
        "(?:^|; )" +
        name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, "\\$1") +
        "=([^;]*)"
      )
    );
    return matches ? decodeURIComponent(matches[1]) : undefined;
  }
  setQuery(findQuery);
  setFbParam("_fbp", "fbp");
  setFbParam("_fbc", "fbc");


  function setFbParam(param, name) {
    let f = getCookie(param);
    let n = 50;
    loop(param, f, name, n);
  }


  function loop(param, f, name, n) {
    setTimeout(function () {
      f = getCookie(param);


      if (f == undefined) {
        n = --n;
        if (n > 0) {
          loop(param, f, name, n);
        }
      } else {
        setScopeParam(name, f);
      }
    }, 200);
  }


  function setScopeParam(name, value) {


    let g = window.scope;
    g.push({ name: name, value: value });
    window.scope = g;
    if (frameStatus) {
      myIframe.contentWindow.postMessage(window.scope, "*");
    }
  }

