@@ -60,18 +60,17 @@ defmodule Posthog do
60
60
Posthog.capture("page_view", distinct_id: "user_123")
61
61
62
62
# Event with properties
63
- Posthog.capture("purchase", [
63
+ Posthog.capture("purchase", %{
64
64
distinct_id: "user_123",
65
65
product_id: "prod_123",
66
66
price: 99.99
67
- ] )
67
+ } )
68
68
69
69
# Event with custom timestamp
70
- Posthog.capture("signup", [distinct_id: "user_123"], DateTime.utc_now())
70
+ Posthog.capture("signup", "user_123", %{}, timestamp: DateTime.utc_now())
71
71
72
72
# Event with custom headers (e.g., for IP forwarding)
73
- Posthog.capture("login", [distinct_id: "user_123"],
74
- [headers: [{"x-forwarded-for", "127.0.0.1"}]])
73
+ Posthog.capture("login", "user_123", %{}, headers: [{"x-forwarded-for", "127.0.0.1"}])
75
74
76
75
## Feature Flags
77
76
@@ -129,24 +128,21 @@ defmodule Posthog do
129
128
## Examples
130
129
131
130
# Basic event
132
- Posthog.capture("page_view", distinct_id: "user_123")
131
+ Posthog.capture("page_view", "user_123")
133
132
134
133
# Event with properties
135
- Posthog.capture("purchase", [
136
- distinct_id: "user_123",
134
+ Posthog.capture("purchase", "user_123", %{
137
135
product_id: "prod_123",
138
136
price: 99.99
139
- ] )
137
+ } )
140
138
141
139
# Event with timestamp
142
- Posthog.capture("signup", [distinct_id: "user_123"], DateTime.utc_now())
140
+ Posthog.capture("signup", "user_123", %{}, timestamp: DateTime.utc_now())
143
141
144
142
# Event with custom headers
145
- Posthog.capture("login", [distinct_id: "user_123"],
146
- [headers: [{"x-forwarded-for", "127.0.0.1"}]])
143
+ Posthog.capture("login", "user_123", %{}, headers: [{"x-forwarded-for", "127.0.0.1"}])
147
144
"""
148
145
@ typep result ( ) :: { :ok , term ( ) } | { :error , term ( ) }
149
- @ typep timestamp ( ) :: DateTime . t ( ) | NaiveDateTime . t ( ) | String . t ( ) | nil
150
146
@ typep cache_key ( ) :: { :feature_flag_called , binary ( ) , binary ( ) }
151
147
@ typep feature_flag_called_event_properties_key ( ) ::
152
148
:"$feature_flag"
@@ -162,22 +158,23 @@ defmodule Posthog do
162
158
163
159
alias Posthog . { Client , FeatureFlag }
164
160
165
- @ spec capture ( atom ( ) | String . t ( ) , keyword ( ) | map ( ) , keyword ( ) | timestamp ( ) ) :: result ( )
166
- defdelegate capture ( event , params , opts \\ [ ] ) , to: Client
161
+ @ spec capture ( Client . event ( ) , Client . distinct_id ( ) , Client . properties ( ) , Client . opts ( ) ) ::
162
+ result ( )
163
+ defdelegate capture ( event , distinct_id , properties , opts \\ [ ] ) , to: Client
167
164
168
165
@ doc """
169
166
Sends multiple events to PostHog in a single request.
170
167
171
168
## Parameters
172
169
173
- * `events` - List of event tuples in the format `{event_name, properties, timestamp }`
170
+ * `events` - List of event tuples in the format `{event_name, distinct_id, properties }`
174
171
* `opts` - Optional parameters for the batch request
175
172
176
173
## Examples
177
174
178
175
events = [
179
- {"page_view", [distinct_id: "user_123"], nil },
180
- {"button_click", [distinct_id: "user_123", button: "signup"], nil }
176
+ {"page_view", "user_123", %{} },
177
+ {"button_click", "user_123", %{ button: "signup"} }
181
178
]
182
179
183
180
Posthog.batch(events)
@@ -244,8 +241,8 @@ defmodule Posthog do
244
241
if Keyword . get ( opts , :send_feature_flag_event , true ) ,
245
242
do:
246
243
capture_feature_flag_called_event (
244
+ distinct_id ,
247
245
% {
248
- "distinct_id" => distinct_id ,
249
246
"$feature_flag" => flag ,
250
247
"$feature_flag_response" => enabled
251
248
} ,
@@ -259,21 +256,25 @@ defmodule Posthog do
259
256
end
260
257
end
261
258
262
- @ spec capture_feature_flag_called_event ( feature_flag_called_event_properties ( ) , map ( ) ) ::
259
+ @ spec capture_feature_flag_called_event (
260
+ Client . distinct_id ( ) ,
261
+ feature_flag_called_event_properties ( ) ,
262
+ map ( )
263
+ ) ::
263
264
:ok
264
- defp capture_feature_flag_called_event ( properties , response ) do
265
+ defp capture_feature_flag_called_event ( distinct_id , properties , response ) do
265
266
# Create a unique key for this distinct_id and flag combination
266
- cache_key = { :feature_flag_called , properties [ " distinct_id" ] , properties [ "$feature_flag" ] }
267
+ cache_key = { :feature_flag_called , distinct_id , properties [ "$feature_flag" ] }
267
268
268
269
# Check if we've seen this combination before using Cachex
269
270
case Cachex . exists? ( Posthog.Application . cache_name ( ) , cache_key ) do
270
271
{ :ok , false } ->
271
- do_capture_feature_flag_called_event ( cache_key , properties , response )
272
+ do_capture_feature_flag_called_event ( cache_key , distinct_id , properties , response )
272
273
273
274
# Should be `{:error, :no_cache}` but Dyalixir is wrongly assuming that doesn't exist
274
275
{ :error , _ } ->
275
276
# Cache doesn't exist, let's capture the event PLUS notify user they should be initing it
276
- do_capture_feature_flag_called_event ( cache_key , properties , response )
277
+ do_capture_feature_flag_called_event ( cache_key , distinct_id , properties , response )
277
278
278
279
Logger . error ( """
279
280
[posthog] Cachex process `#{ inspect ( Posthog.Application . cache_name ( ) ) } ` is not running.
@@ -299,10 +300,11 @@ defmodule Posthog do
299
300
300
301
@ spec do_capture_feature_flag_called_event (
301
302
cache_key ( ) ,
303
+ Client . distinct_id ( ) ,
302
304
feature_flag_called_event_properties ( ) ,
303
305
map ( )
304
306
) :: :ok
305
- defp do_capture_feature_flag_called_event ( cache_key , properties , response ) do
307
+ defp do_capture_feature_flag_called_event ( cache_key , distinct_id , properties , response ) do
306
308
flag = properties [ "$feature_flag" ]
307
309
308
310
properties =
@@ -324,7 +326,7 @@ defmodule Posthog do
324
326
end
325
327
326
328
# Send the event to our server
327
- Client . capture ( "$feature_flag_called" , properties , [ ] )
329
+ Client . capture ( "$feature_flag_called" , distinct_id , properties , [ ] )
328
330
329
331
# Add new entry to cache using Cachex
330
332
Cachex . put ( Posthog.Application . cache_name ( ) , cache_key , true )
0 commit comments