# `PhoenixKit.Admin.Presence`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.87/lib/phoenix_kit/admin/presence.ex#L1)

Phoenix.Presence implementation for tracking anonymous and authenticated sessions.

This module provides real-time tracking of:
- Anonymous visitors (WebSocket connections without user_uuid)
- Authenticated users (with user_uuid)
- Session details like IP, User-Agent, current page, connection time

## Usage

Track anonymous session:
```elixir
PhoenixKit.Admin.Presence.track_anonymous(session_id, %{
  connected_at: DateTime.utc_now(),
  ip_address: get_connect_info(socket, :peer_data).address,
  user_agent: get_connect_info(socket, :user_agent),
  current_page: socket.assigns.current_path
})
```

Track authenticated session:
```elixir
PhoenixKit.Admin.Presence.track_user(user, %{
  connected_at: DateTime.utc_now(),
  session_id: session_id,
  ip_address: get_connect_info(socket, :peer_data).address
})
```

## Events Generated

- `{:anonymous_session_connected, session_id, session_info}`
- `{:anonymous_session_disconnected, session_id}`
- `{:user_session_connected, user_uuid, session_info}`
- `{:user_session_disconnected, user_uuid, session_id}`
- `{:presence_stats_updated, stats}`

# `child_spec`

# `fetch`

# `fetchers_pids`

# `get_by_key`

# `get_presence_stats`

Gets presence statistics.

Returns statistics about current active sessions:
- Total sessions
- Anonymous sessions count
- Authenticated sessions count
- Unique anonymous visitors
- Active authenticated users
- Top pages by activity

# `get_topic`

Gets the presence topic name.

# `list`

# `list_active_sessions`

Gets all currently active sessions (anonymous + authenticated).

Returns a list of session maps with type, metadata, and connection info.

# `list_anonymous_sessions`

Gets all anonymous sessions currently tracked.

# `list_authenticated_sessions`

Gets all authenticated user sessions currently tracked.

# `start_link`

Starts the Presence process.

# `subscribe`

Subscribes to presence events for real-time updates.

# `track`

# `track`

# `track_anonymous`

Tracks an anonymous session in Presence.

## Parameters
- `session_id` - Unique session identifier
- `metadata` - Session metadata map

## Metadata Fields
- `:connected_at` - Connection timestamp
- `:ip_address` - Client IP address
- `:user_agent` - Browser User-Agent string
- `:current_page` - Current page path

# `track_user`

Tracks an authenticated user session in Presence.

## Parameters
- `user` - User struct with id and email
- `metadata` - Session metadata map

## Metadata Fields
- `:connected_at` - Connection timestamp
- `:session_id` - Session identifier
- `:ip_address` - Client IP address
- `:user_agent` - Browser User-Agent string
- `:current_page` - Current page path

# `untrack`

# `untrack`

# `update`

# `update`

# `update_metadata`

Updates metadata for an existing presence.

Useful for updating current page or other session details.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
