How to track dashboard button clicks and save them to Memberstack custom fields? Answered

Post author
Mubassir Shaikh

I have one button in my dashboard, when user click on that button, i want to record that button click in this custom field on memberstack, has anyone tried this before?

Comments

1 comment

  • Comment author
    Chukwudi

    Hi Mubassir,

    Yes, it's definitely possible to record a button click and store that data in a custom field on Memberstack.

    Here’s a sample script you can use. It updates the custom field button-click-1 when a user clicks a specific button on your dashboard. Remember to replace my-special-button in the code with the button ID on your dashboard.:

    <script>
      document.getElementById("my-special-button").addEventListener("click", async function () {
        try {
          const memberData = await window.$memberstackDom.getCurrentMember();
          const member = memberData.data;
    
          if (member) {
            await window.$memberstackDom.updateMember({
              customFields: {
                "button-click-1": "true" // You can also store a timestamp or counter value
              }
            });
    
            console.log("Custom field 'button-click-1' updated successfully!");
          }
        } catch (error) {
          console.error("Error updating custom field 'button-click-1':", error);
        }
      });
    </script>
    

    I hope this helps.

    0

Please sign in to leave a comment.