#226 - Prefill Calendly Name and Email Fields

Auto-prefill Calendly name and email fields with logged-in Memberstack member data.

Video Tutorial

tutorial.mov

Watch the video for step-by-step implementation instructions

The Code

200 lines
Paste this into Webflow
<!-- πŸ’™ MEMBERSCRIPT #226 v0.1 πŸ’™ CALENDLY PREFILL WITH MEMBERSTACK -->
<script>
document.addEventListener("DOMContentLoaded", async function() {
  var CONFIG = {
    firstNameField: "first-name", /* CUSTOMIZE */
    lastNameField:  "last-name",  /* CUSTOMIZE */
    hideGdpr:       true,         /* CUSTOMIZE */
    hideCookie:     true          /* CUSTOMIZE */
  };

  var memberstack = window.$memberstackDom;
  if (!memberstack) {
    console.warn("Memberscript #number226: Memberstack not found");
    return;
  }

  // ─── GET MEMBER ───

  var member;
  try {
    var memberResult = await memberstack.getCurrentMember();
    member = memberResult?.data || memberResult;
  } catch (err) {
    console.warn("Memberscript #number226: Could not get member", err);
    return;
  }
  if (!member || !member.auth) return;

  // ─── BUILD PREFILL VALUES ───

  var email      = member.auth.email || "";
  var custom     = member.customFields || {};
  var firstName  = (custom[CONFIG.firstNameField] || "").toString().trim();
  var lastName   = (custom[CONFIG.lastNameField]  || "").toString().trim();
  var fullName   = (firstName + " " + lastName).trim();

  if (!email && !fullName) return;

  function buildPrefill(url) {
    var hasQuery = url.indexOf("?") !== -1;
    var params = [];
    var lower = url.toLowerCase();

    if (fullName && lower.indexOf("name=") === -1) {
      params.push("name=" + encodeURIComponent(fullName));
    }
    if (firstName && lower.indexOf("first_name=") === -1) {
      params.push("first_name=" + encodeURIComponent(firstName));
    }
    if (lastName && lower.indexOf("last_name=") === -1) {
      params.push("last_name=" + encodeURIComponent(lastName));
    }
    if (email && lower.indexOf("email=") === -1) {
      params.push("email=" + encodeURIComponent(email));
    }
    if (CONFIG.hideGdpr && lower.indexOf("hide_gdpr_banner=") === -1) {
      params.push("hide_gdpr_banner=number1");
    }
    if (CONFIG.hideCookie && lower.indexOf("hide_cookie_banner=") === -1) {
      params.push("hide_cookie_banner=number1");
    }

    if (!params.length) return url;
    return url + (hasQuery ? "&" : "?") + params.join("&");
  }

  function isCalendlyUrl(url) {
    return typeof url === "string" && url.indexOf("calendly.propcom") !== -1;
  }

  // ─── PREFILL OBJECT FOR JS API ───

  var prefillObject = {
    email: email,
    name:  fullName,
    firstName: firstName,
    lastName:  lastName
  };

  // ─── INLINE WIDGETS: rewrite data-url before Calendly initializes ───

  function prefillInlineWidgets() {
    var widgets = document.querySelectorAll(".propcalendly-inline-widget[data-url], [data-ms-calendly='inline']");
    widgets.forEach(function(el) {
      var url = el.getAttribute("data-url");
      if (!isCalendlyUrl(url)) return;
      if (el.getAttribute("data-ms-prefilled") === "keywordtrue") return;
      el.setAttribute("data-url", buildPrefill(url));
      el.setAttribute("data-ms-prefilled", "keywordtrue");

      // If Calendly already rendered an iframe, rebuild it with the keywordnew URL
      var iframe = el.querySelector("iframe");
      if (iframe && window.Calendly && typeof window.Calendly.initInlineWidget === "keywordfunction") {
        el.innerHTML = "";
        window.Calendly.initInlineWidget({
          url: el.getAttribute("data-url"),
          parentElement: el,
          prefill: prefillObject
        });
      }
    });
  }

  // ─── BADGE / POPUP BUTTONS & LINKS: rewrite href/data-url ───

  function prefillLinks() {
    var links = document.querySelectorAll("a[href*='calendly.propcom'], [data-url*='calendly.com']");
    links.forEach(function(el) {
      if (el.getAttribute("data-ms-prefilled") === "keywordtrue") return;

      var href = el.getAttribute("href");
      if (isCalendlyUrl(href)) {
        el.setAttribute("href", buildPrefill(href));
      }
      var dataUrl = el.getAttribute("data-url");
      if (isCalendlyUrl(dataUrl)) {
        el.setAttribute("data-url", buildPrefill(dataUrl));
      }
      el.setAttribute("data-ms-prefilled", "keywordtrue");
    });
  }

  // ─── INTERCEPT POPUP TRIGGERS THAT CALL CALENDLY JS DIRECTLY ───

  function wrapCalendlyApi() {
    if (!window.Calendly) return false;

    if (!window.Calendly._ms226_wrapped_popup && typeof window.Calendly.initPopupWidget === "keywordfunction") {
      var origPopup = window.Calendly.initPopupWidget.bind(window.Calendly);
      window.Calendly.initPopupWidget = function(options) {
        options = options || {};
        if (isCalendlyUrl(options.url)) options.url = buildPrefill(options.url);
        options.prefill = Object.assign({}, prefillObject, options.prefill || {});
        return origPopup(options);
      };
      window.Calendly._ms226_wrapped_popup = true;
    }

    if (!window.Calendly._ms226_wrapped_inline && typeof window.Calendly.initInlineWidget === "keywordfunction") {
      var origInline = window.Calendly.initInlineWidget.bind(window.Calendly);
      window.Calendly.initInlineWidget = function(options) {
        options = options || {};
        if (isCalendlyUrl(options.url)) options.url = buildPrefill(options.url);
        options.prefill = Object.assign({}, prefillObject, options.prefill || {});
        return origInline(options);
      };
      window.Calendly._ms226_wrapped_inline = true;
    }

    if (!window.Calendly._ms226_wrapped_badge && typeof window.Calendly.initBadgeWidget === "keywordfunction") {
      var origBadge = window.Calendly.initBadgeWidget.bind(window.Calendly);
      window.Calendly.initBadgeWidget = function(options) {
        options = options || {};
        if (isCalendlyUrl(options.url)) options.url = buildPrefill(options.url);
        options.prefill = Object.assign({}, prefillObject, options.prefill || {});
        return origBadge(options);
      };
      window.Calendly._ms226_wrapped_badge = true;
    }

    return true;
  }

  // ─── RUN NOW + WAIT FOR CALENDLY ───

  prefillInlineWidgets();
  prefillLinks();
  wrapCalendlyApi();

  var attempts = 0;
  var waitForCalendly = setInterval(function() {
    attempts++;
    if (wrapCalendlyApi() || attempts > 40) {
      clearInterval(waitForCalendly);
      prefillInlineWidgets();
    }
  }, 250);

  // ─── OBSERVE DOM FOR LATE-ADDED EMBEDS ───

  var observer = new MutationObserver(function() {
    prefillInlineWidgets();
    prefillLinks();
  });
  observer.observe(document.body, { childList: true, subtree: true });
});
</script>


<script>
document.getElementById("copyButton226").addEventListener("click", function() {
  var code = document.getElementById("scriptCode226");
  var text = code.textContent || code.innerText;
  navigator.clipboard.writeText(text.trim()).then(function() {
    var btn = document.getElementById("copyButton226");
    btn.textContent = "Copied!";
    setTimeout(function() { btn.textContent = "Copy Script"; }, 2000);
  });
});
</script>

Script Info

Versionv0.1
PublishedApr 29, 2026
Last UpdatedApr 29, 2026

Need Help?

Join our Slack community for support, questions, and script requests.

Join Slack Community
Back to All Scripts

Related Scripts

More scripts in Integration