Using Memberstack 2.0? There is an updated version of this article here.
Track clicks, limit member actions, etc. using Javascript
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.
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(memberstackmMember) {
// increments the clickCount number by 1
clickCount++;
console.log("current count = " + clickCount);
// updates the members profile
memberstackmMember.updateProfile({
"click-count": clickCount
}, false)
}
</script>
Now when a member clicks on the button, it will be added to their profile field and increment with every click! Congratulations! 🎉
Comments
0 comments
Please sign in to leave a comment.