Session tracking in Netra provides powerful capabilities for monitoring user interactions and application behavior. This section covers how to track sessions, add custom context, and record events within your application.

Setting Up Session Tracking

Before you can start tracking sessions, initialize the Netra SDK:
from netra import Netra
from netra.instrumentation.instruments import InstrumentSet

# Initialize with default settings
Netra.init(app_name="Your application name")

# Or with custom configuration
api_key = "Your API key"
headers = f"x-api-key={api_key}"
Netra.init(
    app_name="Your application name",
    headers=headers,
    trace_content=True,
    environment="Your Application environment",
	instruments={InstrumentSet.OPENAI}
)

Session Identification

Set unique identifiers for your sessions:
# Set session identification
Netra.set_session_id("unique-session-id")
Netra.set_user_id("user-123")
Netra.set_tenant_id("tenant-456")

Custom Context Attributes

Add custom attributes to enrich your session data:
# Add custom context attributes
Netra.set_custom_attributes(key="customer_tier", value="premium")
Netra.set_custom_attributes(key="region", value="us-east")
Netra.set_custom_attributes(key="plan_type", value="enterprise")

Custom Events

Record important events in your application:
# Record user feedback event
Netra.set_custom_event(event_name="user_feedback", attributes={
    "rating": 5,
    "comment": "Great response!",
    "timestamp": "2024-01-15T10:30:00Z",
    "category": "positive"
})

# Record conversion event
Netra.set_custom_event(event_name="conversion", attributes={
    "type": "subscription",
    "plan": "premium",
    "value": 99.99,
    "currency": "USD",
    "source": "website",
    "campaign": "spring_sale"
})

# Record error event
Netra.set_custom_event(event_name="error_occurred", attributes={
    "error_type": "authentication",
    "error_message": "Invalid credentials",
    "severity": "high",
    "user_action": "login"
})