BentoSdk Events API
Mix.install([
{:bento_sdk, "~> 0.1.0"},
{:jason, "~> 1.2"}
])
Introduction
This notebook demonstrates how to use the BentoSdk to work with events in the Bento marketing platform. The Events API allows you to:
- Track individual events
- Import events in bulk
Events are a powerful way to track user behavior and trigger automations in Bento.
Configuration
We’ll use Livebook’s secrets feature to securely store and access your Bento credentials:
# Configure BentoSdk with the secrets
BentoSdk.configure(
site_uuid: System.fetch_env!("LB_BENTO_SITE_UUID"),
username: System.fetch_env!("LB_BENTO_USERNAME"),
password: System.fetch_env!("LB_BENTO_PASSWORD")
)
Tracking Events
Let’s track an event for a subscriber:
# Example of tracking a basic event
email = "example@example.com"
type = "page_viewed"
case BentoSdk.Events.track(email, type) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end
Tracking Events with Fields
You can include additional fields with your events:
# Example of tracking an event with fields
email = "example@example.com"
type = "page_viewed"
fields = %{
"page" => "/products/123",
"referrer" => "https://google.com"
}
case BentoSdk.Events.track(email, type, fields) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end
Tracking Events with Fields and Details
For more complex events, you can include both fields and details:
# Example of tracking a complex event
email = "example@example.com"
type = "$purchase"
fields = %{
"first_name" => "Jesse"
}
details = %{
"unique" => %{
"key" => "order_123"
},
"value" => %{
"currency" => "USD",
"amount" => 8000
},
"cart" => %{
"items" => [
%{
"product_sku" => "SKU123",
"product_name" => "Test Product",
"quantity" => 1
}
],
"abandoned_checkout_url" => "https://example.com/cart"
}
}
case BentoSdk.Events.track(email, type, fields, details) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end
Common Event Types
Here are some common event types you might want to track:
-
page_viewed
- User viewed a page -
$purchase
- User completed a purchase -
$completed_onboarding
- User completed onboarding -
$cart_updated
- User updated their cart -
$form_submitted
- User submitted a form
Importing Events in Bulk
You can import multiple events at once:
# Example of importing events in bulk
events = [
%{
email: "user1@example.com",
type: "page_viewed",
fields: %{
"page" => "/products/123",
"referrer" => "https://google.com"
}
},
%{
email: "user2@example.com",
type: "$purchase",
fields: %{
"first_name" => "Jane"
},
details: %{
"value" => %{
"currency" => "USD",
"amount" => 4999
}
}
}
]
case BentoSdk.Events.import_events(events) do
{:ok, result} ->
result
{:error, reason} ->
"Error: #{reason}"
end