Examples
Copy ready-made code for the most common Screeb tasks, from setup and identity to events, surveys, hooks and logout.
Each example below covers one common task. They assume the SDK is installed as shown in Install. Replace the placeholders with your own values.
For a complete working app, open the example for your SDK in the examples folder of the Screeb SDK repository.
Initialize with a logged-in user
Section titled “Initialize with a logged-in user”When the user is already logged in at startup, pass their id and properties to the initialization. Screeb then ties the session to that user from the start, with no anonymous user in between.
import type { ReactNode } from "react";import { ScreebProvider } from "@screeb/sdk-react";
export function AppProviders({ children }: { children: ReactNode }) { return ( <ScreebProvider websiteId="<YOUR-CHANNEL-ID>" autoInit userId="<USER-ID>" userProperties={{ email: "jane@example.com", plan: "pro" }} > {children} </ScreebProvider> );}Identify the user after login
Section titled “Identify the user after login”When the app starts before the user logs in, initialize Screeb without a user, then identify them once they log in. Screeb keeps the activity of the anonymous session and attaches it to that user. See Identity for the details.
const { identity } = useScreeb();
async function onLogin(userId: string, email: string) { await identity(userId, { email, plan: "pro" });}Track an event
Section titled “Track an event”Track what the user does, with properties that describe it. You can then target surveys at users who did it. See Event tracking.
const { eventTrack } = useScreeb();
await eventTrack("plan_upgraded", { plan: "pro", seats: 5,});Assign the user to a group
Section titled “Assign the user to a group”Put the user in a group, such as their company, with properties that describe the group. See Group assignation.
const { identityGroupAssign } = useScreeb();
await identityGroupAssign("Acme", "company", { plan: "enterprise",});Start a survey with hidden fields
Section titled “Start a survey with hidden fields”Start a survey from your code, for example once a checkout completes, and pass hidden fields that Screeb saves with the response. See Start a survey programmatically for the other options.
const { surveyStart } = useScreeb();
await surveyStart("<SURVEY-ID>", undefined, true, { source: "checkout", cart_value: 149.99,});Run code when a survey is completed
Section titled “Run code when a survey is completed”Pass hooks to the initialization to run your code on Screeb events. This one logs each completed survey. Hooks always need a version. See Hooks for every hook and its payload.
import type { ReactNode } from "react";import { ScreebProvider } from "@screeb/sdk-react";
export function AppProviders({ children }: { children: ReactNode }) { return ( <ScreebProvider websiteId="<YOUR-CHANNEL-ID>" autoInit hooks={{ version: "1.0.0", onSurveyCompleted: (payload) => { console.log("Survey completed", payload.survey.id); }, }} > {children} </ScreebProvider> );}Reset the identity on logout
Section titled “Reset the identity on logout”When the user logs out, reset the identity. Screeb starts a new anonymous session, so the next person on the device is not mixed with the previous user.
const { identityReset } = useScreeb();
await identityReset();