How to refactor Crisp Chat integration code from MemberStack 1.0 to 2.0? Answered

Post author
John Matias

Hey team can I get a little help refactoring this code for MS2.0?

<script>
MemberStack.onReady.then(function(member) {
    var email = member["email"]
    var name = member["name"]
    if (member.loggedIn) {
      try{
        $crisp.push(["set", "user:email", [email] ])
        $crisp.push(["set", "user:nickname", [name] ])
        profitwell('start', { 'user_email': email });
      } catch(e) {
    }
  }
})
</script>

Comments

5 comments

  • Comment author
    Josh Lopez

    try this:

    window.$memberstackDom.getCurrentMember().then((member) => {
      if (member.data) {
        // do logged in logic here
        const email = member.data.auth.email;
        const name = member.data.customFields["name"];
        try {
          $crisp.push(["set", "user:email", [email]])
          $crisp.push(["set", "user:nickname", [name]])
          profitwell('start', { 'user_email': email });
        } catch (e) {
          console.log(e);
        };
      } else {
        // do logged out logic here
      }
    })
    
    0
  • Comment author
    John Matias

    Thanks, Josh! I'll give it a try.

    0
  • Comment author
    Zack Hakim

    Josh Lopez Any chance you can change this to MS 2.0 code?

    <script>
    MemberStack.onReady.then(function(member) {
    var url = member["profile-url"]
    var els = Array.from(document.querySelectorAll("[profile-url]"))
    for (var el of els) {
    el.href = url
    }
    })
    </script>

    0
  • Comment author
    Diana Mendez

    Hii!! We have been using Memberstack 1.0 and Crisp Chat and it worked perfect... now with 2.0 we haven't being able to integrated them... Does someone know if it is supported by 2.0? thanks!

    0
  • Comment author
    Raquel Lopez

    Hi,
    Can you show what errors you're presenting? or share a link to your test site to take a look at the issue? If you are using a custom code script to send the member email to the chat, you'll need to adapt it to version 2.0, because the member object has changed...

    According to the docs, if you want to send a member email to the chat, you can use this snippet. If you're not sending any other member data, then crisp should work, because it would not depend on Memberstack to work

        function init() {
            const currentUser = JSON.parse(localStorage.getItem("_ms-mem"));
    
            if (currentUser) {
                const email = currentUser.auth.email
                $crisp.push(["set", "user:email", [email]]);
            }
        }
    
    (async function () {
        if (window.$memberstackReady) {
            await init();
        } else {
            document.addEventListener("memberstack.ready", async () => {
                await init();
            })
        }
    })()
    
    0

Please sign in to leave a comment.