Limit Member Actions

Article author
Josh Lopez

How to limit how many times a member can perform an action. (Example: Only allowing a customer to book certain appointments.)

 

Here is how to count the number of clicks on an element & save that number to a member's profile. This approach can be used on many different platforms.

 

Step 1:

Add a New Field on the Forms & Fields page as "click count". We recommend hiding the field so members can't see or edit the count in their profile.

 

Add_a_custom_field.png

Step 2:
Add a button on a member page with an id of "addCount". The HTML would look similar to this.
<a id="addCount" href="#">Count Click</a>

 

Step 3:
Add the following code before the closing </body> tag in your HTML. This code may not function properly if placed in the header.

<script>
// global variable that will be increased on click.
var clickCount;
// Memberstack code that gets the member information.
MemberStack.onReady.then(function (member) {
// gets the click count field info from memberstack or starts with 0 if not found.
clickCount = member["click-count"] || 0;
console.log("memberstack count: " + clickCount);
// adds event listener to button with the addCount id.
document.getElementById("addCount").addEventListener("click", function () {
clickCounter(member)
});
});
function clickCounter(memberstackMember) {
// increments the clickCount number by 1.
clickCount++;
console.log("current count = " + clickCount);
// updates the members profile if clickCount is equal to or less than 5.
if (clickCount <= 5) {
memberstackMember.updateProfile({ "click-count": clickCount }, false)
} else {
document.getElementById("addCount").removeEventListener("click", function () {
clickCounterOff();
});
}

}
function clickCounterOff() {
console.log("Clicks turned off for clickCounter");
}
</script>

 

Now when a member clicks on the button, it will be added to their profile field and increment with every click and limit it to a certain amount! Congratulations! 🎉

Was this article helpful?

Comments

1 comment

  • Comment author
    Cameron P

    Hi I love this idea! However I'm having troubles getting it to work. Looks like the biggest error the JS is throwing is "MemberStack is not defined". Do you have any ideas on how to fix this? I'm using Memberstack 2.0

    0

Please sign in to leave a comment.