Restricting the free users in using the application Answered

Post author
Jeroen Kwaad

Desperately need some help as I'm planning to launch with MS 🫡

I'm trying to prevent free-users from generating more than 10 prompts (form submissions). Currently I have this script in place from the AI free template.

<script> document.addEventListener("DOMContentLoaded", () => { // Wait for Memberstack to be fully loaded before running your code const intervalId = setInterval(async () => { if (window.$memberstackDom) { clearInterval(intervalId); const memberstack = window.$memberstackDom; console.log("Memberstack loaded!"); try { const member = await memberstack.getMemberJSON(); console.log("Member data:", member); const count = member.data.formSubmissions.count; console.log("formSubmissions count:", count); const memData = JSON.parse(localStorage.getItem("_ms-mem")); console.log("_ms-mem data:", memData); const hasPlanConnections = memData && memData.planConnections && memData.planConnections.length > 0; console.log("Has plan connections:", hasPlanConnections); if (count >= 10 && !hasPlanConnections) { console.log("Redirecting to /app/upgrade..."); window.location.href = "/app/upgrade"; } else { console.log("No need to upgrade."); } } catch (error) { console.error(error); } } }, 100); }); </script>

How can I modify this script so that paid users can have unlimited submissions, and free-plan users can only have 10 submissions? After 10 submissions I want them to get redirected to the /app/upgrade page if they click on the submit button for the 11th time.

I've been trying this for a good hour with the help of the friendly Memberstack AI chatbot with no luck 😞

Comments

5 comments

  • Comment author
    Julian Galluzzo

    Hey Jeroen!

    Is the problem that its not redirecting users? As in, on the page. If you have over 10 and you refresh, will it redirect?

    0
  • Comment author
    Jeroen Kwaad

    Hey! The problem is that now I think it only checks for any plan connection, but I want it only applied for 'free-users' and paid users should not be restricted.

    0
  • Comment author
    Julian Galluzzo

    I would recommend having the AI chatbot have it check based on plan ID rather than having any plan connections - that way it will be specific with that plan!

    Just find the plan ID for your paid plan and feed it to Rey 🙂

    0
  • Comment author
    Chukwudi Onyekwere

    Hi Jeroen Kwaad

    Here's the modified code, please keep in mind you'd be working with the Plan IDs, not Price IDs:

    <script> document.addEventListener("DOMContentLoaded", () => { // Wait for Memberstack to be fully loaded before running your code const intervalId = setInterval(async () => { if (window.$memberstackDom) { clearInterval(intervalId); const memberstack = window.$memberstackDom; console.log("Memberstack loaded!"); try { const member = await memberstack.getMemberJSON(); console.log("Member data:", member); const count = member.data.formSubmissions.count; console.log("formSubmissions count:", count); const memData = JSON.parse(localStorage.getItem("_ms-mem")); console.log("_ms-mem data:", memData); const planConnections = memData && memData.planConnections; console.log("Plan connections:", planConnections); const planA = "pln_IDForFreePlan"; // Replace with the Plan ID of Free Plan const planB = "pln_IDForPaidPlan"; // Replace with the Plan ID of Paid Plan if (planConnections && planConnections.length > 0) { const planIds = planConnections.map(connection => connection.planId); if (planIds.includes(planA)) { // Free plan user if (count >= 10) { console.log("Redirecting to /app/upgrade..."); window.location.href = "/app/upgrade"; } else { console.log("No need to upgrade."); } } else if (planIds.includes(planB)) { // Paid plan user console.log("Paid plan user. No restrictions."); } else { // Unknown plan console.log("Unknown plan. Redirecting to pricing page..."); } } else { // No plan connections console.log("No plan connections. Redirecting to pricing page..."); } } catch (error) { console.error(error); } } }, 100); }); </script>
    0
  • Comment author
    Jeroen Kwaad

    Thank you so much! 🙏

    0

Please sign in to leave a comment.