Posthog.Event (posthog v1.1.0)

View Source

Represents a PostHog event with all its properties and metadata.

This struct encapsulates all the information needed to send an event to PostHog, including the event name, distinct ID, properties, timestamp, and UUID.

Examples

# Create a basic event
iex> event = Posthog.Event.new("page_view", "user_123")
iex> event.event
"page_view"
iex> event.distinct_id
"user_123"
iex> event.properties
%{}
iex> is_binary(event.uuid)
true
iex> is_binary(event.timestamp)
true

# Create an event with properties
iex> event = Posthog.Event.new("purchase", "user_123", %{price: 99.99})
iex> event.properties
%{price: 99.99}

# Create an event with custom timestamp
iex> timestamp = "2023-01-01T00:00:00Z"
iex> event = Posthog.Event.new("login", "user_123", %{}, timestamp: timestamp)
iex> event.timestamp
"2023-01-01T00:00:00Z"

# Create an event with custom UUID
iex> uuid = "123e4567-e89b-12d3-a456-426614174000"
iex> event = Posthog.Event.new("signup", "user_123", %{}, uuid: uuid)
iex> event.uuid
"123e4567-e89b-12d3-a456-426614174000"

# Convert event to API payload
iex> event = Posthog.Event.new("page_view", "user_123", %{page: "home"})
iex> payload = Posthog.Event.to_api_payload(event)
iex> payload.event
"page_view"
iex> payload.distinct_id
"user_123"
iex> payload.properties["page"]
"home"
iex> payload.properties["$lib"]
"posthog-elixir"
iex> is_binary(payload.uuid)
true
iex> is_binary(payload.timestamp)
true

# Create batch payload
iex> events = [
...>   Posthog.Event.new("page_view", "user_123", %{page: "home"}),
...>   Posthog.Event.new("click", "user_123", %{button: "signup"})
...> ]
iex> batch = Posthog.Event.batch_payload(events)
iex> length(batch.batch)
2
iex> [first, second] = batch.batch
iex> first.event
"page_view"
iex> second.event
"click"

Summary

Functions

Creates a batch payload from a list of events.

Converts the event struct to a map suitable for sending to the PostHog API.

Types

distinct_id()

@type distinct_id() :: String.t()

event_name()

@type event_name() :: atom() | String.t()

properties()

@type properties() :: map()

t()

@type t() :: %Posthog.Event{
  distinct_id: String.t(),
  event: String.t(),
  properties: map(),
  timestamp: String.t(),
  uuid: String.t()
}

timestamp()

@type timestamp() :: String.t() | DateTime.t() | NaiveDateTime.t()

Functions

batch_payload(events)

@spec batch_payload([t()]) :: map()

Creates a batch payload from a list of events.

new(event, distinct_id, properties \\ %{}, opts \\ [])

@spec new(event_name(), distinct_id(), properties(), keyword()) :: t()

Creates a new PostHog event.

Parameters

  • event - The name of the event (string or atom)
  • distinct_id - The distinct ID for the person or group
  • properties - Event properties (optional, defaults to empty map)
  • timestamp - Optional timestamp for the event (defaults to current UTC time)
  • uuid - Optional UUID for the event (defaults to a new UUID7)

Examples

# Basic event
Posthog.Event.new("page_view", "user_123")

# Event with properties
Posthog.Event.new("purchase", "user_123", %{price: 99.99})

# Event with custom timestamp
Posthog.Event.new("login", "user_123", %{}, timestamp: DateTime.utc_now())

to_api_payload(event)

@spec to_api_payload(t()) :: map()

Converts the event struct to a map suitable for sending to the PostHog API.