PhoenixKit.Modules.Legal.ConsentLog (phoenix_kit v1.7.43)

Copy Markdown View Source

Schema for consent log entries.

Tracks user consent for cookies and data processing, supporting GDPR, CCPA, and other privacy regulations.

Fields

  • user_id - The ID of the logged-in user (nil for anonymous)
  • session_id - Session identifier for anonymous tracking
  • consent_type - Type of consent (necessary, analytics, marketing, preferences)
  • consent_given - Whether consent was given
  • consent_version - Version of privacy/cookie policy
  • ip_address - IP address when consent was recorded
  • user_agent_hash - SHA256 hash of user agent for fingerprinting
  • metadata - Additional metadata about the consent
  • necessary - Essential cookies (always required)
  • analytics - Performance and analytics cookies
  • marketing - Advertising and targeting cookies
  • preferences - Functionality and preference cookies

Usage

# Log consent for anonymous user
ConsentLog.create(%{
  session_id: "abc123",
  consent_type: "analytics",
  consent_given: true,
  consent_version: "1.0",
  ip_address: "192.168.1.1"
})

# Log consent for logged-in user
ConsentLog.create(%{
  user_id: 123,
  consent_type: "marketing",
  consent_given: false
})

# Get current consent status
ConsentLog.get_consent_status(user_id: 123)
ConsentLog.get_consent_status(session_id: "abc123")

Summary

Functions

Creates a changeset for consent log entry.

Returns valid consent types.

Create a new consent log entry.

Gets a single consent log by ID or UUID.

Same as get_consent_log/1, but raises Ecto.NoResultsError if not found.

Get consent status for a user or session.

Hash user agent string for privacy-focused storage.

Log consent for multiple types at once.

Types

t()

@type t() :: %PhoenixKit.Modules.Legal.ConsentLog{
  __meta__: term(),
  consent_given: boolean(),
  consent_type: String.t(),
  consent_version: String.t() | nil,
  id: integer() | nil,
  inserted_at: DateTime.t() | nil,
  ip_address: String.t() | nil,
  metadata: map() | nil,
  session_id: String.t() | nil,
  updated_at: DateTime.t() | nil,
  user_agent_hash: String.t() | nil,
  user_id: integer() | nil,
  user_uuid: Ecto.UUID.t() | nil,
  uuid: Ecto.UUID.t() | nil
}

Functions

changeset(consent_log, attrs)

@spec changeset(t(), map()) :: Ecto.Changeset.t()

Creates a changeset for consent log entry.

Required Fields

  • :consent_type - Type of consent

Optional Fields

  • :user_id - User ID (for logged-in users)
  • :session_id - Session ID (for anonymous users)
  • :consent_given - Whether consent was given (default: false)
  • :consent_version - Version of policy
  • :ip_address - IP address
  • :user_agent_hash - Hashed user agent
  • :metadata - Additional metadata

create(attrs)

@spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Create a new consent log entry.

hash_user_agent(user_agent)

@spec hash_user_agent(String.t()) :: String.t()

Hash user agent string for privacy-focused storage.

log_consents(consents, opts)

@spec log_consents(
  map(),
  keyword()
) :: {:ok, [t()]} | {:error, term()}

Log consent for multiple types at once.

Parameters

  • consents - Map of consent_type => consent_given
  • opts - Options including :user_id, :session_id, :ip_address, etc.

Example

ConsentLog.log_consents(
  %{"analytics" => true, "marketing" => false},
  user_id: 123,
  consent_version: "1.0"
)