Lädt...


🔧 Supabase + Next.js: How to Run a Targeted In-app Survey for Free Using Formbricks


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Feedback is the bedrock of any forward-thinking organization. Feedback helps you as an organization figure out your customers' or users' reactions to your product or service.
However, while feedback is good, how you collect the feedback from your customers or users matters as much. Thanks to Formbricks, you now have targeted in-app surveys for generating consumer feedback or opinions!
In this tutorial, we will be building a simple webpage that allows for signup and login features using Supabase and Next.Js. We will then integrate an in-app survey using Formbricks.

Prerequisites

Before we start, make sure you have:

The complete code for this application can be found here.

Building A Simple Authentication With Supabase, Next.Js And Formbricks

Step 1

Let's create a new Next.js project:

npx create-next-app@latest my-survey-app --typescript --tailwind --app
cd my-survey-app

Step 2

Install Supabase in your project directory:

npm install @supabase/supabase-js

Step 3:

Let us set up Supabase in the app.

First, create a .env.local file in your project root:

NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your_supabase_anon_key

To fetch the Supabse URL and Anon Key, you must be logged in to your Supabase dashboard. You will see them displayed on the dashboard.

Supabase dashboard showing anon key and URL

Step 4:

Next is to create a Supabase client. In your project root directory, create a folder lib. In the folder, create a file supabaseClient.ts.

import { createClient } from "@supabase/supabase-js";

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!;

export const supabase = createClient(supabaseUrl, supabaseAnonKey);

Step 5

Let's create the sign-up and login components.
In the project directory. there is an app folder. In that folder, you will create two files. One for the signup and one for the login.

Let's create and populate signup.tsx

import { useState } from "react";
import { supabase } from "../lib/supabase";
import { useRouter } from "next/router";

export default function SignUp() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const router = useRouter();

  const handleSignUp = async (e: React.FormEvent) => {
    e.preventDefault();
    const { error } = await supabase.auth.signUp({
      email,
      password,
    });

    if (error) {
      setError(error.message);
    } else {
      router.push("/login"); // Redirect to login after signup
    }
  };

  return (
    <div>
      <h1>Sign Up</h1>
      <form onSubmit={handleSignUp}>
        <label>Email:</label>
        <input
          type="email"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
          required
        />
        <br />
        <label>Password:</label>
        <input
          type="password"
          value={password}
          onChange={(e) => setPassword(e.target.value)}
          required
        />
        <br />
        <button type="submit">Sign Up</button>
      </form>
      {error && <p style={{ color: "red" }}>{error}</p>}
    </div>
  );
}

Next, let's create and populate login.tsx

import { useState } from "react";
import { supabase } from "../lib/supabase";
import { useRouter } from "next/router";

export default function Login() {
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [error, setError] = useState("");
  const router = useRouter();

  const handleSignIn = async (e: React.FormEvent) => {
    e.preventDefault();
    const { error } = await supabase.auth.signInWithPassword({
      email,
      password,
    });

    if (error) {
      setError(error.message);
    } else {
      router.push("/dashboard"); // Redirect to dashboard after login
    }
  };

  return (
    <div>
      <h1>Login</h1>
      <form onSubmit={handleSignIn}>
        <label>Email:</label>
        <input
          type="email"
          value={email}
          onChange={(e) => setEmail(e.target.value)}
          required
        />
        <br />
        <label>Password:</label>
        <input
          type="password"
          value={password}
          onChange={(e) => setPassword(e.target.value)}
          required
        />
        <br />
        <button type="submit">Login</button>
      </form>
      {error && <p style={{ color: "red" }}>{error}</p>}
    </div>
  );
}

Step 6

After testing our app above, and we are satisfied, it is time to move on to integrating Formbricks.
More information can be found on the Formbricks documentation page for Next.Js.

In our terminal, we will install Formbricks using NPM.

npm install @formbricks/js zod

Step 7

In this next step, we will initialize Formbricks in our app.
In the app folder in your project directory, create a new file named formbricks.tsx and populate with this,

"use client";

import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";
import formbricks from "@formbricks/js/website";

export default function FormbricksProvider() {
  const pathname = usePathname();
  const searchParams = useSearchParams();

  if (typeof window !== "undefined") {
    formbricks.init({
      environmentId: "<your-environment-id>",
      apiHost: "<api-host-url>",
    });
  }
}

The environmentId and apiHost are on your Formbricks account dashboard. When logged in, go to the "Configuration" page, and click on the "Website and App Connection" button. You should see your environmentId and apiHost below.

formbricks dashboard

After you have done this, check your dashboard to see if the website has been connected to Formbricks successfully.
Your dashboard should look like this:

Formbricks website completed

Step 8

You can now get your app running. Go to your terminal and run

npm run dev

On your console, you should see this message to indicate that Formbricks is up and running.

Formbricks initializing

Setting Up Survey Targeting in Formbricks

The beautiful thing about Formbricks is that it lets you do the customizations from your dashboard!On the Formbricks dashboard, you will see a list of pre-customized surveys to get you started, or you can create your own from scratch.
Even for the pre-customized surveys, you are free to edit as much as you like. Play around with the survey questions or change the appearance and feel of the survey pop-up!
What's more is that you can customize survey triggers to either show to users with certain attributes, show at the click of a button, or show depending on what the user is doing on the page.
For the purposes of this tutorial, we will be displaying the survey or feedback form when a user clicks on the "Leave A Feedback!" button on the webpage.

On trigger the survey by clicking the button, the user gets shown the Formbricks feedback box.

Formbricks survey popup

As an extra measure of customization, I put all my favorite colors and specifications! 🤣

Best Practices for In-app Surveys

  1. Timing is Everything

    • Don't show surveys immediately after a user visits your app or website
    • Set a delay or wait for specific user actions
    • Consider the user's journey and context
  2. Targeting Strategy

    • Start with broad segments
    • Gradually refine based on user behavior
    • Use multiple conditions to ensure relevance
  3. Survey Design

    • Keep surveys short and focused
    • Use clear, concise questions
    • Provide an easy way to dismiss the survey
  4. Data Collection

    • Track survey completion rates
    • Monitor user feedback
    • Iterate based on response patterns

Conclusion

In this tutorial, we've successfully built a system that:

  • Handles user authentication with Supabase
  • Shows targeted surveys using Formbricks
  • Provides a seamless user experience

The best part? It's completely free to get started, and you can scale as your needs grow.

Formbricks is entirely open-source and value added for the community! Support them by giving a star on Github! ⭐

Further Reading

...

🔧 Creating a Pokémon guessing game using Supabase, Drizzle, and Next.js in just 2 hours!


📈 25.01 Punkte
🔧 Programmierung

🔧 Add password-less OTP based authentication to your Next.js apps using Supabase & Twilio


📈 25.01 Punkte
🔧 Programmierung

🔧 Building a Full Stack project with Supabase and Next Js


📈 20.94 Punkte
🔧 Programmierung

🔧 How to build: an AI-powered blogging platform (Next.js, Langchain, & Supabase)


📈 20.94 Punkte
🔧 Programmierung

🔧 Implementing vector search with OpenAI, Next.js, and Supabase


📈 20.94 Punkte
🔧 Programmierung

🔧 Get Started with Next.js 14 & Supabase


📈 20.94 Punkte
🔧 Programmierung

🔧 [Client] Auth with Next.js 14 & Supabase


📈 20.94 Punkte
🔧 Programmierung

🔧 Create a blog with Supabase and Next.js - part 4 - SSR


📈 20.94 Punkte
🔧 Programmierung

🔧 Building a Modern Authentication System with Supabase, Ballerina, and Next.js


📈 20.94 Punkte
🔧 Programmierung

🔧 Next JS & Supabase CRUD🚀


📈 20.94 Punkte
🔧 Programmierung

🔧 Next.js-Supabase Stripe Subscriptions Integration


📈 20.94 Punkte
🔧 Programmierung

🔧 Building a Custom Chatbot with Next.js, Langchain, OpenAI, and Supabase.


📈 20.94 Punkte
🔧 Programmierung

🔧 I'm Building an AI-Powered Blog (Next.js, LangChain, Supabase)


📈 20.94 Punkte
🔧 Programmierung

🔧 Next.js 14 SSO Github (Supabase Auth)


📈 20.94 Punkte
🔧 Programmierung

🪟 Future EA games can be 'upgraded' to run on next-gen consoles for free


📈 16.8 Punkte
🪟 Windows Tipps

🔧 Unlock Next-Level Authentication in Next.js with Next Auth and TypeScript Module Augmentation


📈 16.02 Punkte
🔧 Programmierung

🔧 Web Design Survey Findings and Next Steps


📈 15.24 Punkte
🔧 Programmierung

📰 Cert Magazine Salary Survey Says: CCSP is the Next Big Thing


📈 15.24 Punkte
📰 IT Security Nachrichten

📰 Survey: How Well Will Organizations Respond To The Next Data Breach?


📈 15.24 Punkte
📰 IT Security Nachrichten

📰 Survey: Over Half of Medical Device Makers Expect an Attack on Their Devices In the Next Year


📈 15.24 Punkte
📰 IT Security Nachrichten

📰 Only 28% of Utilities To Implement Major Security Projects in Next 2 Years, Finds Survey


📈 15.24 Punkte
📰 IT Security Nachrichten

🔧 An example of using searchParams, useSearchParams and Next router in Next 15


📈 14.75 Punkte
🔧 Programmierung

🔧 E-Book Share Using Next.js 14, Prisma, TailwindCSS & Next Auth 🤩


📈 14.75 Punkte
🔧 Programmierung

🔧 Travel Booking Using Next.js 14, Prisma, TailwindCSS & Next Auth


📈 14.75 Punkte
🔧 Programmierung

🔧 Build a website with Next.js using next/images


📈 14.75 Punkte
🔧 Programmierung

🔧 Fixing Disqus 'Auto' theme switching when using Next.js + next-themes


📈 14.75 Punkte
🔧 Programmierung

🔧 Enhancing Next.js Projects with CSS Support Using '@zeit/next-css'


📈 14.75 Punkte
🔧 Programmierung

🔧 Add custom fonts to Next.js sites with Tailwind using next/font


📈 14.75 Punkte
🔧 Programmierung

🔧 Translate website content using Next.js internationalization and next-i18next


📈 14.75 Punkte
🔧 Programmierung

🔧 Data Structures and Algorithms: 500 Questions Down, 60 More Hards targeted in next 30 Days! 🚀


📈 14.28 Punkte
🔧 Programmierung

📰 Cyber Attacks now being targeted as the next state level weapons


📈 14.28 Punkte
📰 IT Security Nachrichten

📰 Remote and cloud-based systems to be ruthlessly targeted next year


📈 14.28 Punkte
📰 IT Security Nachrichten

matomo