ResourcesConversation Management

Conversation Management

Control when to create new conversations for optimal ad targeting

When to Create Conversations

When to Create Conversations

Users control conversation lifecycle

const { conversationId, initializeConversation } = useEarnLayerClient();

// Create conversation on page load (recommended)
useEffect(() => {
  initializeConversation();
}, []);

// Create new conversation on "New Chat" button
const handleNewChat = async () => {
  const newConversationId = await initializeConversation();
  // This resets the ad queue for fresh contextual ads
};

Best Practices

On Page Load

Always initialize a conversation when the chat page loads

function ChatPage() {
  const { initializeConversation } = useEarnLayerClient();
  const hasInitialized = useRef(false);
  
  useEffect(() => {
    if (!hasInitialized.current) {
      hasInitialized.current = true;
      initializeConversation();
    }
  }, []);
  
  return <div>Your chat UI</div>;
}

On "New Chat" Button

Reset the conversation when user starts a new topic

const handleNewChat = async () => {
  await initializeConversation();
  setMessages([]);  // Clear message history
  // Fresh conversation = fresh contextual ads
};

Custom Options

Custom Options

Demo Mode
// Testing with demo mode
const conversation = await initializeConversation({
  demoMode: true,        // Show all demo ads
  visitorId: 'user_123',
  adTypes: ['banner', 'thinking'],
  frequency: 'low'
});
Production Mode
// Production mode
const conversation = await initializeConversation({
  demoMode: false,       // Show only real ads
  visitorId: 'user_123',
  adTypes: ['hyperlink', 'thinking', 'banner'],
  frequency: 'normal'
});

Next Steps