All checks were successful
deploy / deploy (push) Successful in 1m37s
Event call sites (product_view, add_to_cart, checkout_start, purchase) were only passing visitor_hash and pathname, leaving browser, OS, screen size and country nil. Add AnalyticsHook.attrs/1 helper to extract common analytics fields from socket assigns, and use it in all LiveView event call sites. Checkout controller reads the same fields from the session. Also fix plug analytics test to clear stale events before assertions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
56 lines
1.4 KiB
Elixir
56 lines
1.4 KiB
Elixir
defmodule BerrypodWeb.Shop.CheckoutSuccess do
|
|
use BerrypodWeb, :live_view
|
|
|
|
alias Berrypod.{Analytics, Orders}
|
|
|
|
@impl true
|
|
def mount(%{"session_id" => session_id}, _session, socket) do
|
|
order = Orders.get_order_by_stripe_session(session_id)
|
|
|
|
# Subscribe to order status updates (webhook may arrive after redirect)
|
|
if order && connected?(socket) do
|
|
Phoenix.PubSub.subscribe(Berrypod.PubSub, "order:#{order.id}:status")
|
|
end
|
|
|
|
# Track purchase event
|
|
if order && connected?(socket) && socket.assigns[:analytics_visitor_hash] do
|
|
attrs =
|
|
BerrypodWeb.AnalyticsHook.attrs(socket)
|
|
|> Map.merge(%{pathname: "/checkout/success", revenue: order.total})
|
|
|
|
Analytics.track_event("purchase", attrs)
|
|
end
|
|
|
|
# Clear the cart after successful checkout
|
|
socket =
|
|
if order && connected?(socket) do
|
|
BerrypodWeb.CartHook.broadcast_and_update(socket, [])
|
|
else
|
|
socket
|
|
end
|
|
|
|
socket =
|
|
socket
|
|
|> assign(:page_title, "Order confirmed")
|
|
|> assign(:order, order)
|
|
|
|
{:ok, socket}
|
|
end
|
|
|
|
def mount(_params, _session, socket) do
|
|
{:ok, redirect(socket, to: ~p"/")}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({:order_paid, order}, socket) do
|
|
{:noreply, assign(socket, :order, order)}
|
|
end
|
|
|
|
@impl true
|
|
def render(assigns) do
|
|
~H"""
|
|
<BerrypodWeb.PageTemplates.checkout_success {assigns} />
|
|
"""
|
|
end
|
|
end
|