Getting Started
Install What If? SDK and build your first feedback widget in 5 minutes đ
Setup in seconds
Let's build a beautiful feedback widget from scratch. This comprehensive guide covers installation, setup, and your first implementation.
Step 1: Install the Package
đŻ React / Next.js
Perfect for React applications with hooks and context.
npm install @twenty1tools/whatif⥠Vanilla JavaScript
Works everywhere - HTML, Vue, Angular, Svelte.
npm install @twenty1tools/whatif# React/Next.js
npm install @twenty1tools/whatif
# Vanilla JS
npm install @twenty1tools/whatif# React/Next.js
yarn add @twenty1tools/whatif
# Vanilla JS
yarn add @twenty1tools/whatif# React/Next.js
pnpm add @twenty1tools/whatif
# Vanilla JS
pnpm add @twenty1tools/whatif# React/Next.js
bun add @twenty1tools/whatif
# Vanilla JS
bun add @twenty1tools/whatifStep 2: Get Your Project ID
Create a project in your dashboard to get your unique Project ID.
Dashboard â New Project â Copy Project IDStep 3: Choose Your Implementation
Setup Provider
Wrap your app with the WhatIfProvider:
import { WhatIfProvider } from '@twenty1tools/whatif';
function App() {
return (
<WhatIfProvider
projectId="your-project-id"
user={{
id: 'user_123', // Required for smart filtering
name: 'John Doe', // Optional display data
}}
org={{
name: 'Acme Corp' // Optional organization tracking
}}
>
<YourApp />
</WhatIfProvider>
);
}Add a Trigger
Use the hook anywhere in your app:
import { useWhatIf } from '@twenty1tools/whatif';
export function FeedbackButton() {
const { ask } = useWhatIf();
// Simple call - user/org data already configured in provider
return (
<button
onClick={() => ask('your-question-id')}
className="btn btn-primary"
>
Give Feedback
</button>
);
}That's it! đ The widget will automatically appear when users click the button.
Create Provider Component
'use client';
import { WhatIfProvider } from '@twenty1tools/whatif';
export function Providers({ children }: { children: React.ReactNode }) {
return (
<WhatIfProvider projectId="your-project-id">
{children}
</WhatIfProvider>
);
}Add to Root Layout
import { Providers } from './providers';
export default function RootLayout({ children }) {
return (
<html>
<body>
<Providers>{children}</Providers>
</body>
</html>
);
}Add a Trigger
'use client';
import { useWhatIf } from '@twenty1tools/whatif';
export function FeedbackButton() {
const { ask } = useWhatIf();
return <button onClick={() => ask('question-id')}>Feedback</button>;
}Perfect! đ Your Next.js app now has feedback widgets.
Simplified Usage (Recommended)
With global initialization, you can now use the ultra-simple API:
import { useWhatIf } from '@twenty1tools/whatif';
function FeedbackButton() {
const { ask } = useWhatIf();
return (
<button onClick={() => ask('your-question-id')}>
Give Feedback
</button>
);
}No need to pass user/org data every time - it's already configured!
Initialize SDK
import { WhatIf } from '@twenty1tools/whatif';
const whatif = new WhatIf('your-app-id');Add a Trigger
document.getElementById('feedback-btn').addEventListener('click', () => {
whatif.ask('your-question-id');
});<button id="feedback-btn">Give Feedback</button>Done! ⨠Click the button to see your widget.
Step 4: Create Your First Question
Head to your dashboard and create a question:
-
Go to your project â Questions tab
-
Click "New Question"
-
Choose a type:
- đ Rating - Star ratings (1-5)
- â Single Choice - Multiple choice questions
- đŹ Text - Open-ended feedback
-
Write your question:
Example: "How would you rate your experience?" -
Copy the Question ID from the dashboard
-
Use it in your code:
whatif.ask('question-id-from-dashboard');
Step 5: Add User Context (Important!)
â ď¸ Important: Always include user context with a unique id to prevent questions from showing repeatedly to the same user.
Provider-Level Setup (Recommended)
Set user data once in your provider - it will be automatically used for all questions:
<WhatIfProvider
projectId="your-project-id"
user={{
id: 'user_unique_id', // â ď¸ REQUIRED - prevents repeated questions
name: 'Sarah Chen', // Optional - for dashboard display
email: 'sarah@example.com', // Optional - for dashboard display
avatar: 'https://example.com/avatar.jpg' // Optional - for dashboard display
}}
org={{
name: 'Acme Corp', // Optional - for organization display
logo: 'https://example.com/logo.png' // Optional - for dashboard display
}}
>
<YourApp />
</WhatIfProvider>Usage - Automatic Data Pickup
const { ask } = useWhatIf();
// â
Uses provider data automatically
ask('checkout-question');
// â
Override for specific cases
ask('admin-feedback', {
user: { id: 'admin_user', name: 'Admin' },
context: { page: 'admin-panel' }
});Manual Setup (Without Provider)
If not using the provider, pass user data with each question:
whatif.ask('question-id', {
user: {
id: 'user_unique_id', // â ď¸ REQUIRED - prevents repeated questions
name: 'Sarah Chen', // Optional - enhances dashboard display
email: 'sarah@example.com', // Optional - enhances dashboard display
avatar: 'https://example.com/avatar.jpg' // Optional - enhances dashboard display
},
context: {
page: 'checkout',
feature: 'new-payment'
}
});Why user.id matters: Without a unique identifier, WhatIf can't track which users have seen questions, causing the same question to appear repeatedly.
Automatic Data: WhatIf automatically captures timestamp, browser, device, page info, and previous URL with every response - no code needed! See the Context Guide for details.
Testing
Test your widget locally:
// Trigger the widget programmatically
whatif.ask('question-id');
// Or test with user/org/context data
whatif.ask('question-id', {
user: {
id: 'test_user_123',
name: 'Test User',
email: 'test@example.com',
avatar: 'https://example.com/avatar.jpg'
},
org: {
name: 'Test Corp',
logo: 'https://example.com/logo.png'
},
context: {
page: 'checkout',
environment: 'development'
}
});Common Use Cases
đ After Checkout
// When order completes
whatif.ask('checkout-satisfaction');â Feature Launch
// When user tries new feature
whatif.ask('new-feature-feedback');đŻ Exit Intent
// Before user leaves
document.addEventListener('mouseleave', () => {
whatif.ask('exit-survey');
});âąď¸ After Time
// After 30 seconds on page
setTimeout(() => {
whatif.ask('page-experience');
}, 30000);Product Use-cases: Looking to find ways to best leverage What If for your product? See our Product Use-cases list for ideas on how our growing number of customers are using WhatIf for everyday.
What's Happening?
When a user clicks your trigger:
- đŻ Question loads from your dashboard
- đ¨ Widget appears with smooth animation
- âď¸ User responds through the widget
- â Response saves automatically
- đ Data appears in your dashboard
Step 6: Customize (Optional)
Make the widget match your brand:
const whatif = new WhatIf('your-app-id', '/api', {
darkMode: 'auto',
colors: {
primary: '#3b82f6', // Your brand color
},
position: 'bottom-right',
});Want more customization? Check out the Theming Guide for 50+ customization options.
Next Steps
đ Context & Data
Automatic data + user/org display
đ¨ Theming
Match your brand perfectly
đ Dark Mode
Auto-detect user preference
đ Product Use Cases
Industry-specific implementation examples
Requirements
- Node.js 16+ (for npm installation)
- React 18+ (for React SDK)
- Modern browser with ES6 support
Need Help?
- đ Full API Reference
- đŹ Join Discord
- đ§ Email Support