Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

1.4 KB
Raw Blame
defmodule PlausibleWeb.Plugins.API.Views.Funnel do
  @moduledoc """
  View for rendering Funnels in the Plugins API
  """

  use PlausibleWeb, :plugins_api_view

  def render("index.json", %{
        pagination: %{entries: funnels, metadata: metadata},
        authorized_site: site,
        conn: conn
      }) do
    %{
      funnels:
        render_many(funnels, __MODULE__, "funnel.json", authorized_site: site, as: :funnel),
      meta:
        render_metadata_links(
          metadata,
          fn params -> url(~p"/api/plugins/v1/funnels?#{params}") end,
          conn.query_params
        )
    }
  end

  def render("index.json", %{
        funnels: funnels,
        authorized_site: site,
        conn: conn
      }) do
    %{
      funnels:
        render_many(funnels, __MODULE__, "funnel.json", authorized_site: site, as: :funnel),
      meta:
        render_metadata_links(
          %{},
          fn params -> url(~p"/api/plugins/v1/funnels?#{params}") end,
          conn.query_params
        )
    }
  end

  def render(
        "funnel.json",
        %{
          funnel: funnel,
          authorized_site: site
        }
      ) do
    goals = Plausible.Funnel.goals(funnel)

    %{
      funnel: %{
        name: funnel.name,
        id: funnel.id,
        steps:
          render_many(goals, PlausibleWeb.Plugins.API.Views.Goal, "goal.json",
            authorized_site: site,
            as: :goal
          )
      }
    }
  end
end