Implementation of Social Proof Elements (Purchase Notifications, Counters) on Website
Social proof works on a simple psychological mechanism: people orient by actions of others when making decisions. Technically this is a set of components — pop-up purchase notifications, online user counters, "X bought today" badges. Main — don't overdo it: aggressive social proof turns into dark patterns and drops trust.
Purchase notification pop-ups
// purchase-notifications.ts
interface PurchaseEvent {
customerName: string; // "John from New York"
productName: string;
productUrl?: string;
timeAgo: string; // "2 minutes ago"
avatarUrl?: string;
}
interface NotificationConfig {
position?: 'bottom-left' | 'bottom-right';
displayMs?: number; // how long notification hangs
intervalMs?: number; // pause between notifications
maxQueue?: number; // max in queue
}
export class PurchaseNotifier {
private queue: PurchaseEvent[] = [];
private isShowing = false;
private container: HTMLElement;
private config: Required<NotificationConfig>;
constructor(config: NotificationConfig = {}) {
this.config = {
position: 'bottom-left',
displayMs: 5000,
intervalMs: 8000,
maxQueue: 5,
...config,
};
this.container = this.createContainer();
document.body.appendChild(this.container);
}
private createContainer(): HTMLElement {
const el = document.createElement('div');
const pos = this.config.position;
el.style.cssText = `
position: fixed;
${pos === 'bottom-left' ? 'left: 20px' : 'right: 20px'};
bottom: 20px;
z-index: 9998;
pointer-events: none;
`;
return el;
}
push(events: PurchaseEvent[]) {
const toAdd = events.slice(0, this.config.maxQueue - this.queue.length);
this.queue.push(...toAdd);
if (!this.isShowing) this.showNext();
}
private async showNext() {
if (this.queue.length === 0) { this.isShowing = false; return; }
this.isShowing = true;
const event = this.queue.shift()!;
const toast = this.createToast(event);
this.container.appendChild(toast);
// appearance animation
requestAnimationFrame(() => {
toast.style.opacity = '1';
toast.style.transform = 'translateY(0)';
});
await new Promise(r => setTimeout(r, this.config.displayMs));
// disappearance animation
toast.style.opacity = '0';
toast.style.transform = 'translateY(10px)';
await new Promise(r => setTimeout(r, 300));
toast.remove();
await new Promise(r => setTimeout(r, this.config.intervalMs));
this.showNext();
}
private createToast(event: PurchaseEvent): HTMLElement {
const toast = document.createElement('div');
toast.style.cssText = `
display: flex;
align-items: center;
gap: 12px;
background: #fff;
border: 1px solid #e5e7eb;
border-radius: 12px;
padding: 12px 16px;
box-shadow: 0 4px 16px rgba(0,0,0,.1);
max-width: 300px;
pointer-events: auto;
cursor: default;
opacity: 0;
transform: translateY(16px);
transition: opacity .3s, transform .3s;
font-family: system-ui, sans-serif;
font-size: 13px;
`;
const avatar = event.avatarUrl
? `<img src="${event.avatarUrl}" alt="" width="36" height="36" style="border-radius:50%;flex-shrink:0">`
: `<div style="width:36px;height:36px;border-radius:50%;background:#dbeafe;display:flex;align-items:center;justify-content:center;font-size:16px;flex-shrink:0">🛒</div>`;
const productLink = event.productUrl
? `<a href="${event.productUrl}" style="color:#1d4ed8;text-decoration:none;font-weight:500">${event.productName}</a>`
: `<strong>${event.productName}</strong>`;
toast.innerHTML = `
${avatar}
<div>
<div style="color:#111827">
<strong>${event.customerName}</strong> bought ${productLink}
</div>
<div style="color:#9ca3af;margin-top:2px">${event.timeAgo}</div>
</div>
<button style="margin-left:auto;background:none;border:none;cursor:pointer;color:#9ca3af;font-size:16px;padding:0;line-height:1" aria-label="Close">×</button>
`;
toast.querySelector('button')?.addEventListener('click', () => {
toast.remove();
});
return toast;
}
destroy() {
this.container.remove();
}
}
Loading real data from API
// Use real purchase data
async function loadRecentPurchases(productId?: string): Promise<PurchaseEvent[]> {
const url = new URL('/api/social-proof/purchases', location.origin);
if (productId) url.searchParams.set('product_id', productId);
url.searchParams.set('limit', '10');
const res = await fetch(url.toString());
const data: Array<{
buyer_city: string;
product_name: string;
product_url: string;
purchased_at: string;
}> = await res.json();
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
return data.map(item => {
const secondsAgo = (Date.now() - new Date(item.purchased_at).getTime()) / 1000;
let timeAgo: string;
if (secondsAgo < 3600) {
timeAgo = rtf.format(-Math.floor(secondsAgo / 60), 'minute');
} else if (secondsAgo < 86400) {
timeAgo = rtf.format(-Math.floor(secondsAgo / 3600), 'hour');
} else {
timeAgo = rtf.format(-Math.floor(secondsAgo / 86400), 'day');
}
return {
customerName: item.buyer_city,
productName: item.product_name,
productUrl: item.product_url,
timeAgo,
};
});
}
// Initialization
const notifier = new PurchaseNotifier({ position: 'bottom-left', displayMs: 6000 });
loadRecentPurchases().then(events => notifier.push(events));
Online user counter
Real counter via WebSocket or Server-Sent Events. For simple cases SSE is enough:
// online-counter.ts
export function initOnlineCounter(selector: string) {
const el = document.querySelector(selector);
if (!el) return;
const sse = new EventSource('/api/online-count');
sse.addEventListener('count', (e: MessageEvent) => {
const count = parseInt(e.data, 10);
el.textContent = formatCount(count);
// pulse on change
el.classList.remove('pulse');
void (el as HTMLElement).offsetWidth; // reflow trick
el.classList.add('pulse');
});
sse.addEventListener('error', () => {
setTimeout(() => initOnlineCounter(selector), 5000);
});
return () => sse.close();
}
function formatCount(n: number): string {
if (n >= 1000) return `${(n / 1000).toFixed(1)}k`;
return String(n);
}
// Server side (Node.js/Express)
import { Router } from 'express';
const onlineCountRouter = Router();
const clients = new Set<NodeJS.WritableStream>();
// Update counter from Redis or in-memory
setInterval(async () => {
const count = await redis.get('online_users_count') ?? '0';
const message = `event: count\ndata: ${count}\n\n`;
clients.forEach(client => {
try { client.write(message); }
catch { clients.delete(client); }
});
}, 5000);
onlineCountRouter.get('/', (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache');
res.setHeader('Connection', 'keep-alive');
res.flushHeaders();
clients.add(res);
req.on('close', () => clients.delete(res));
});
Sales badges "X bought in 24 hours"
Static or semi-static elements that can be cached:
// SalesBadge.tsx
interface SalesBadgeProps {
count: number;
period?: '24h' | '7d' | '30d';
threshold?: number; // don't show if less
}
const PERIOD_LABELS = {
'24h': 'per day',
'7d': 'per week',
'30d': 'per month',
};
export function SalesBadge({ count, period = '24h', threshold = 5 }: SalesBadgeProps) {
if (count < threshold) return null;
return (
<div className="inline-flex items-center gap-1.5 bg-orange-50 border border-orange-200 text-orange-700 text-xs font-medium px-2.5 py-1 rounded-full">
<span className="w-1.5 h-1.5 bg-orange-400 rounded-full animate-pulse" />
{count} {pluralize(count, ['sale', 'sales', 'sales'])} {PERIOD_LABELS[period]}
</div>
);
}
function pluralize(n: number, forms: [string, string, string]): string {
const abs = Math.abs(n) % 100;
const mod = abs % 10;
if (mod === 1 && abs !== 11) return forms[0];
if (mod >= 2 && mod <= 4 && (abs < 10 || abs >= 20)) return forms[1];
return forms[2];
}
Important from UX perspective
All data should be real or at least based on real aggregates. Completely fake notifications — breach of trust and in some jurisdictions advertising requirements violation. If there are no real purchases — don't show notifications at all.
Frequency should not annoy: one notification every 8–15 seconds — upper limit. User came to read content, not watch blinking banners.
Mandatory close button and ability to disable via cookie/localStorage.
Timeline
Purchase notification component with real API — two days. SSE online counter with Redis — another day. Sales badges — half a day. Total three to four days for full set.







