How to install and use the Quotient SDK in your application
This guide walks you through setting up the Quotient SDK from start to finish, including API key creation, SDK installation, and implementation best practices.
Before starting, ensure you have:
For Development:
Name: Development Web App
Description: Local development environment
Allowed Origins:
- localhost:3000
- localhost:3001
- 127.0.0.1:3000
For Production:
Name: Production Web App
Description: Main production application
Allowed Origins:
- yourapp.com
- www.yourapp.com
- app.yourapp.com
Choose the appropriate package for your framework:
npm install @quotientjs/client
# or
yarn add @quotientjs/client
# or
pnpm add @quotientjs/client
npm install @quotientjs/react
# or
yarn add @quotientjs/react
# or
pnpm add @quotientjs/react
It is safe, but not recommended to hard-code API keys. Use environment variables:
.env.local (Next.js)
NEXT_PUBLIC_QUOTIENT_API_KEY=pk_test_your_api_key_here
.env (Vite/Create React App)
VITE_QUOTIENT_API_KEY=pk_test_your_api_key_here
# or for Create React App
REACT_APP_QUOTIENT_API_KEY=pk_test_your_api_key_here
We allow you to source the sdk in a few different ways.
Basic Setup:
// get-quotient-client.js
import { QuotientClient } from '@quotientjs/client';
let client: QuotientClient | null = null;
export async function getQuotientClient() {
if (!client) {
client = await QuotientClient.init({
apiKey: process.env.NEXT_PUBLIC_QUOTIENT_API_KEY,
});
}
return client;
}
App.jsx / App.tsx:
import { QuotientProvider } from '@quotientjs/react';
function App() {
// Handle missing API key
const apiKey = import.meta.env.VITE_QUOTIENT_API_KEY;
if (!apiKey) {
return (
<div className="error">
Missing Quotient API key. Please set VITE_QUOTIENT_API_KEY.
</div>
);
}
return (
<QuotientProvider
clientOptions={{
apiKey,
}}
autoTrackPageViews={true}
>
<YourApp />
</QuotientProvider>
);
}
Once your SDK is initialized, you can start tracking events and managing customer data. Here are the essential features to implement:
For React apps with autoTrackPageViews={true}
, page views are tracked automatically. For manual tracking or vanilla JavaScript:
// Track initial page view
await client.analytics.event({ eventType: 'pageView' });
Identify users when they sign up, log in, or submit forms:
const { personId } = await client.people.upsert({
emailAddress: 'user@example.com',
firstName: 'Jane',
lastName: 'Doe'
});
Track important user interactions:
// Track search
await client.analytics.event({
eventType: 'search',
searchQuery: 'winter boots'
});
For detailed implementation examples and the full API reference, see the SDK Usage Guide.
// test-quotient.js
async function testQuotientConnection() {
try {
const client = await getQuotientClient();
const info = await client.auth.whoami({});
console.log('✅ Connection successful');
console.log('Business ID:', info.businessId);
console.log('Key Type:', info.keyType);
console.log('Device ID:', info.deviceId);
return true;
} catch (error) {
console.error('❌ Connection failed:', error);
return false;
}
}
// Enable debug mode
const client = await QuotientClient.init({
apiKey: process.env.NEXT_PUBLIC_QUOTIENT_API_KEY,
debug: true // Logs all API calls
});
// Test event tracking
async function testTracking() {
const client = await getQuotientClient();
console.log('Testing page view...');
await client.analytics.event({ eventType: 'pageView' });
console.log('Testing search...');
await client.analytics.event({
eventType: 'search',
searchQuery: 'test query'
});
console.log('✅ Events tracked successfully');
}