SIX|Dynamic Visitor ContextCross-Cutting EnhancementCurrent
Context SystemCross-Cutting Enhancement

Dynamic Visitor Context: Real-World Intelligence

This enhancement adds automatic visitor context detection across all versions (v1-v4), including location, weather, time of day, device type, and browsing behavior. These are cross-cutting improvements that enable personalized shopping experiences regardless of which architectural version a user experiences.

Key Principle

The dynamic visitor context system is a cross-cutting enhancement that applies to all architectural versions (v1, v2, v3, v4). Location, weather, time, and behavior detection enhance the shopping experience regardless of which version a user experiences. This is not a standalone version—it's an intelligent context layer that makes every version more personalized.

What It Does

Instead of static mock context (like "Chicago, IL, rainy weather"), the system now automatically detects:

  • Location - IP-based city/state detection (no permission needed)
  • Weather - Real-time weather conditions for visitor's location
  • Time of Day - Timezone-aware morning/afternoon/evening/night
  • Device Type - Mobile, tablet, or desktop detection
  • Browsing Behavior - Session tracking to infer urgency and interests
  • Optional Precise Location - Browser geolocation with user permission

Why It Matters

Context transforms generic product recommendations into personalized experiences. A visitor in rainy San Francisco at 6 PM gets different suggestions than someone in sunny Miami at 9 AM. The system works automatically—no user configuration required.

Context Detection Features

Phase 1: Browser Context Detection

Client-side detection without any APIs: timezone-aware time of day, device type (mobile/tablet/desktop), referrer tracking, and session behavior analysis. Works immediately on page load with zero external dependencies.

Detects: Time of day, device type, referrer (search/social/direct), session urgency
Privacy: Uses sessionStorage only, no external calls

Phase 2: IP-based Location

Automatic location detection from IP address using free geolocation services (ipapi.co, ip-api.com). No user permission required—works server-side on first request. Returns city/state format and timezone.

API: /api/context/location
Services: ipapi.co (primary), ip-api.com (fallback)
Privacy: Server-side only, cached per session

Phase 3: Weather Integration

Real-time weather detection for visitor's location using OpenWeatherMap API. Maps conditions to shopping-relevant states: sunny, cloudy, rain, snow. Cached for 30 minutes per location.

API: /api/context/weather
Service: OpenWeatherMap (free tier: 60 calls/min, 1M/month)
Optional: Works without API key (returns default sunny weather)

Phase 4: Opt-in Geolocation

Precise location detection using browser geolocation API (requires user permission). Reverse geocodes coordinates to city/state. Privacy-first: opt-in only, cached in sessionStorage for 1 hour.

Requires: User permission (HTTPS or localhost)
Service: Nominatim/OpenStreetMap (free, reverse geocoding)
Privacy: Opt-in only, sessionStorage cache

Phase 5: Behavior-based Context

Session behavior tracking infers urgency and category interests. Tracks view counts, time on site, and category preferences. Privacy-first: uses sessionStorage only (clears on browser close).

Infers: Urgency (high/medium/low), category interests
Privacy: sessionStorage only, no persistence
Helper: trackProductView(category)

Visual Design Enhancements

The context system includes visual components that display detected context in an elegant, informative way:

ContextBanner Component

A contextual information bar that displays weather, location, time of day, and urgency status. Uses icons from Phosphor Icons and color-coded badges for visual hierarchy.

Shopping context:RainSan Francisco, CAEveningHigh urgency
Usage: <ContextBanner context={context} />

Visual Features

  • Weather Icons - Dynamic icons for sunny, cloudy, rain, snow conditions
  • Time Icons - Sun, moon, and horizon icons for time of day
  • Urgency Badges - Color-coded badges (red=high, amber=medium, green=low)
  • Location Display - Map pin icon with city/state format
  • Responsive Design - Adapts to mobile, tablet, and desktop layouts

Privacy & Security

Privacy-First Design

The visitor context system is designed with privacy as a core principle, not an afterthought.

  • No persistent storage - All data cleared when browser closes
  • No cookies or cross-site tracking - Zero tracking cookies
  • Opt-in geolocation - Precise location requires explicit permission
  • SessionStorage only - Data never persists to localStorage or database
  • GDPR/CCPA compliant - No PII collected without permission

Data Storage

  • SessionStorage - Location and behavior data (clears on close)
  • No Database - Context never stored in Supabase or any database
  • No Analytics - No tracking pixels, no analytics events
  • IP Location - Server-side only, not stored on client

User Control

  • URL Parameters - Override context for testing: ?weather=rain&location=Chicago,IL
  • Geolocation Permission - User can deny and system still works with IP location
  • Session-Based - New session = fresh context (no memory across visits)

API Reference

React Hook: useVisitorContext

import { useVisitorContext } from '@/lib/context/useVisitorContext';

function MyComponent() {
  const { context, loading, error } = useVisitorContext({
    useIPLocation: true,    // Enable IP-based location
    useWeather: true,       // Enable weather detection
    useGeolocation: false,  // Opt-in geolocation (default: false)
  });

  if (loading) return <div>Loading...</div>;
  
  return (
    <div>
      <p>Location: {context.location}</p>
      <p>Weather: {context.weather}</p>
      <p>Time: {context.timeOfDay}</p>
    </div>
  );
}

Server-Side: buildServerContext

import { buildServerContext } from '@/lib/context/visitorContext';
import { NextRequest } from 'next/server';

export async function GET(request: NextRequest) {
  const context = await buildServerContext(request, {
    useIPLocation: true,
    useWeather: true,
  });
  
  return Response.json({ context });
}

API Routes

  • GET /api/context/location - Returns visitor location from IP
  • GET /api/context/weather?lat={lat}&lon={lon} - Returns weather for coordinates
  • GET /api/context/weather?location={city,state} - Returns weather for location name

Implementation Status by Version

V1Complete

Full dynamic context enabled: location, weather, time, device, behavior tracking.

V2Complete

Full dynamic context enabled: location, weather, time, device, behavior tracking.

V3Complete

Full dynamic context enabled: location, weather, time, device, behavior tracking.

V4Complete

Full dynamic context enabled: location, weather, time, device, behavior tracking.

What Each Version Gets

  • All Versions - Full context detection (IP location, weather, time, device, behavior)
  • All Versions - ContextBanner component for visual context display
  • All Versions - Automatic context integration in layout generation
  • All Versions - Privacy-first session-based tracking

Get Started

Full Technical Documentation

Complete implementation details, API reference, and troubleshooting guide

docs/DYNAMIC_CONTEXT.md