Skip to content

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

Implement dynamic funnels (#6741)

Adrian Gruntkowski050d4138ff59
diff --git a/assets/js/liveview/live_socket.js b/assets/js/liveview/live_socket.js
index 2b9a393da7..61e47ca4db 100644
--- a/assets/js/liveview/live_socket.js
+++ b/assets/js/liveview/live_socket.js
@@ -7,7 +7,7 @@
import 'phoenix_html'
import { Socket } from 'phoenix'
import { LiveSocket } from 'phoenix_live_view'
-import { Modal, Dropdown, Listbox } from 'prima'
+import { Modal, Dropdown, Listbox, Combobox } from 'prima'
import topbar from 'topbar'
/* eslint-enable import/no-unresolved */
@@ -20,7 +20,15 @@ import MemberRows from './member-rows'
let csrfToken = document.querySelector("meta[name='csrf-token']")
let websocketUrl = document.querySelector("meta[name='websocket-url']")
if (csrfToken && websocketUrl) {
- let Hooks = { Modal, Dropdown, Listbox, Confirm, CopySnippet, MemberRows }
+ let Hooks = {
+ Modal,
+ Dropdown,
+ Listbox,
+ Confirm,
+ CopySnippet,
+ MemberRows,
+ Combobox
+ }
Hooks.VerificationLifecycle = {
mounted() {
diff --git a/e2e/tests/fixtures.ts b/e2e/tests/fixtures.ts
index 2f53f71713..c195501ae0 100644
--- a/e2e/tests/fixtures.ts
+++ b/e2e/tests/fixtures.ts
@@ -62,6 +62,11 @@ type ImportedVisitors = {
date?: EventDate
}
+type FunnelInputStep =
+ | string
+ | { event_name: string; currency?: string }
+ | { page_path: string; scroll_threshold?: string }
+
export type StatsEntry = Event | ImportedVisitors
export async function register({
@@ -504,7 +509,7 @@ export async function addFunnel({
request: APIRequestContext
domain: string
name: string
- steps: string[]
+ steps: FunnelInputStep[]
}) {
const response = await request.post('/e2e-tests/funnel', {
headers: {
@@ -517,6 +522,28 @@ export async function addFunnel({
expect(response.ok()).toBeTruthy()
}
+export async function getFunnel({
+ request,
+ domain,
+ name
+}: {
+ request: APIRequestContext
+ domain: string
+ name: string
+}) {
+ const response = await request.get('/e2e-tests/funnel', {
+ headers: {
+ 'Content-Type': 'application/json',
+ Accept: 'application/json'
+ },
+ params: { domain: domain, name: name }
+ })
+
+ expect(response.ok()).toBeTruthy()
+
+ return await response.json()
+}
+
export async function subscribeToPlan({
page,
planId
diff --git a/e2e/tests/site-settings/funnel-settings.spec.ts b/e2e/tests/site-settings/funnel-settings.spec.ts
new file mode 100644
index 0000000000..b731967b75
--- /dev/null
+++ b/e2e/tests/site-settings/funnel-settings.spec.ts
@@ -0,0 +1,443 @@
+import { test, expect } from '@playwright/test'
+import {
+ setupSite,
+ populateStats,
+ getFunnel,
+ addGoal,
+ addFunnel
+} from '../fixtures'
+import { expectLiveViewConnected } from '../test-utils'
+
+test.describe('funnel settings', () => {
+ test('form integration tests', async ({ page, request }) => {
+ const { domain } = await setupSite({ page, request })
+
+ await populateStats({
+ request,
+ domain,
+ events: [
+ { user_id: 123, name: 'Hello World', timestamp: { hoursAgo: 60 } },
+ { user_id: 123, name: 'Plausible', timestamp: { hoursAgo: 50 } },
+ { user_id: 123, name: 'Another World', timestamp: { hoursAgo: 45 } }
+ ]
+ })
+
+ await page.goto(`/${domain}/settings/funnels`, { waitUntil: 'commit' })
+ await expectLiveViewConnected(page)
+
+ await test.step('search reacts to the input the user types in', async () => {
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('#step-1 input').fill('hello')
+
+ await expect(
+ page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Hello World')
+ ).toBeVisible()
+
+ await page.locator('#step-1 input').fill('hello')
+
+ await expect(
+ page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Plausible')
+ ).toBeVisible()
+
+ // First escape press cancels focus on combobox input;
+ // next one closes the modal.
+ await page.keyboard.press('Escape')
+ await page.keyboard.press('Escape')
+
+ await expect(page.locator('#funnel-form')).toBeHidden()
+ })
+
+ await test.step('selecting option prefills input values', async () => {
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('#step-1 input').fill('another')
+
+ await page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Another World')
+ .click()
+
+ await expect(page.locator('#step-1 input')).toHaveValue('Another World')
+
+ await page.keyboard.press('Escape')
+ await page.keyboard.press('Escape')
+
+ await expect(page.locator('#funnel-form')).toBeHidden()
+ })
+
+ await test.step('selecting one option reduces suggestions in the other', async () => {
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('#step-1 input').fill('another')
+
+ await page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Another World')
+ .click()
+
+ await page.locator('#step-2 input').fill('a')
+
+ await expect(
+ page
+ .locator('#step-2 .input-picker-dropdown-option')
+ .getByText('Another World')
+ ).toBeHidden()
+
+ await page.keyboard.press('Escape')
+ await page.keyboard.press('Escape')
+
+ await expect(page.locator('#funnel-form')).toBeHidden()
+ })
+
+ await test.step('removing one option alters suggestions for other', async () => {
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('a[phx-click=add-step]').click()
+
+ await page.locator('#step-2 input').fill('hello')
+
+ await page
+ .locator('#step-2 .input-picker-dropdown-option')
+ .getByText('Hello World')
+ .click()
+
+ await page.locator('#step-1 input').fill('hello')
+
+ await expect(
+ page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Hello World')
+ ).toBeHidden()
+
+ await page.locator('#remove-step-2').click()
+
+ await page.locator('#step-1 input').fill('hello')
+
+ await expect(
+ page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Hello World')
+ ).toBeVisible()
+
+ await page.keyboard.press('Escape')
+ await page.keyboard.press('Escape')
+
+ await expect(page.locator('#funnel-form')).toBeHidden()
+ })
+ })
+
+ test('save button saves a new funnel', async ({ page, request }) => {
+ const { domain } = await setupSite({ page, request })
+
+ await populateStats({
+ request,
+ domain,
+ events: [
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page1',
+ timestamp: { hoursAgo: 60 }
+ },
+ {
+ user_id: 123,
+ name: 'Form: Submission',
+ 'meta.key': ['path'],
+ 'meta.value': ['/form-action/path'],
+ timestamp: { hoursAgo: 50 }
+ },
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page2',
+ timestamp: { hoursAgo: 45 }
+ }
+ ]
+ })
+
+ await page.goto(`/${domain}/settings/funnels`, { waitUntil: 'commit' })
+ await expectLiveViewConnected(page)
+
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await expect(page.locator('button#save')).toBeDisabled()
+
+ await page.locator('#step-1 input').fill('page')
+
+ await page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Visit /page1')
+ .click()
+
+ await expect(page.locator('#step-1 input')).toHaveValue('Visit /page1')
+
+ await page.locator('#step-2 input').click()
+
+ await page
+ .locator('#step-2 .input-picker-dropdown-option')
+ .getByText('Form: Submission')
+ .click()
+
+ await page.locator('input#funnel_name').fill('First funnel')
+
+ await expect(page.locator('button#save')).toBeEnabled()
+
+ await page.locator('button#save').click()
+
+ await expect(page.locator('#funnel-form')).toBeHidden()
+
+ await expect(page.locator('#funnel-settings-main table')).toContainText(
+ 'First funnel'
+ )
+
+ const {
+ name: funnel_name,
+ steps: [step1, step2]
+ } = await getFunnel({ request, domain, name: 'First funnel' })
+
+ expect(funnel_name).toBe('First funnel')
+ expect(step1.id).toBe(0) // dynamic step
+ expect(step1.page_path).toBe('/page1')
+ expect(step2.id).toBeGreaterThan(0) // static step
+ expect(step2.event_name).toBe('Form: Submission')
+ })
+
+ test('clicking save after editing the funnel updates it', async ({
+ page,
+ request
+ }) => {
+ const { domain } = await setupSite({ page, request })
+
+ await addGoal({
+ request,
+ domain,
+ params: { page_path: '/page1' }
+ })
+
+ await addGoal({
+ request,
+ domain,
+ params: { event_name: 'Form: Submission' }
+ })
+
+ await populateStats({
+ request,
+ domain,
+ events: [
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page1',
+ timestamp: { hoursAgo: 60 }
+ },
+ {
+ user_id: 123,
+ name: 'Form: Submission',
+ 'meta.key': ['path'],
+ 'meta.value': ['/form-action/path'],
+ timestamp: { hoursAgo: 50 }
+ },
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page2',
+ timestamp: { hoursAgo: 45 }
+ },
+ {
+ user_id: 124,
+ name: 'pageview',
+ pathname: '/page1',
+ timestamp: { hoursAgo: 60 }
+ },
+ {
+ user_id: 124,
+ name: 'pageview',
+ pathname: '/page2',
+ timestamp: { hoursAgo: 45 }
+ }
+ ]
+ })
+
+ await addFunnel({
+ request,
+ domain,
+ name: 'Existing funnel',
+ steps: ['Visit /page1', { page_path: '/page2' }]
+ })
+
+ await page.goto(`/${domain}/settings/funnels`, { waitUntil: 'commit' })
+ await expectLiveViewConnected(page)
+
+ await page.locator('button[phx-click=edit-funnel]').click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('#step-2 input').fill('form')
+
+ await page
+ .locator('#step-2 .input-picker-dropdown-option')
+ .getByText('Form: Submission')
+ .click()
+
+ await expect(page.locator('#step-2 input')).toHaveValue('Form: Submission')
+
+ await page.locator('input#funnel_name').fill('Updated funnel')
+
+ await expect(page.locator('button#save')).toBeEnabled()
+
+ await page.locator('button#save').click()
+
+ await expect(page.locator('#funnel-form')).toBeHidden()
+
+ const {
+ name: funnel_name,
+ steps: [step1, step2]
+ } = await getFunnel({ request, domain, name: 'Updated funnel' })
+
+ expect(funnel_name).toBe('Updated funnel')
+ expect(step1.id).toBeGreaterThan(0) // static step
+ expect(step1.page_path).toBe('/page1')
+ expect(step2.id).toBeGreaterThan(0) // static step
+ expect(step2.event_name).toBe('Form: Submission')
+ })
+
+ test('funnel gets evaluated on every select, assuming a second has passed between selections', async ({
+ page,
+ request
+ }) => {
+ const { domain } = await setupSite({ page, request })
+
+ await populateStats({
+ request,
+ domain,
+ events: [
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page1',
+ timestamp: { hoursAgo: 60 }
+ },
+ {
+ user_id: 123,
+ name: 'Form: Submission',
+ 'meta.key': ['path'],
+ 'meta.value': ['/form-action/path'],
+ timestamp: { hoursAgo: 50 }
+ },
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page2',
+ timestamp: { hoursAgo: 45 }
+ }
+ ]
+ })
+
+ await page.goto(`/${domain}/settings/funnels`, { waitUntil: 'commit' })
+ await expectLiveViewConnected(page)
+
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('#step-1 input').click()
+
+ await page.locator('#step-1 .input-picker-dropdown-option').first().click()
+
+ await expect(page.locator('#step-1 input')).not.toHaveValue('')
+
+ await page.locator('#step-2 input').click()
+
+ await page.locator('#step-2 .input-picker-dropdown-option').first().click()
+
+ await expect(page.locator('#step-2 input')).not.toHaveValue('')
+
+ await expect(page.locator('#step-eval-0')).toHaveText('Visitors: 1')
+ await expect(page.locator('#step-eval-1')).toHaveText('Dropoff: 100%')
+ await expect(page.locator('#funnel-eval')).toHaveText(
+ 'Last month conversion rate: 0%'
+ )
+ })
+
+ test('scroll goal can be picked for a matching pageview event from the previous step', async ({
+ page,
+ request
+ }) => {
+ const { domain } = await setupSite({ page, request })
+
+ await addGoal({
+ request,
+ domain,
+ params: {
+ page_path: '/page1',
+ scroll_threshold: 90,
+ display_name: 'Scroll 90% on /page1'
+ }
+ })
+
+ await populateStats({
+ request,
+ domain,
+ events: [
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page1',
+ timestamp: { minutesAgo: 1500 }
+ },
+ {
+ user_id: 123,
+ name: 'engagement',
+ pathname: '/page1',
+ scroll_depth: 90,
+ timestamp: { minutesAgo: 1499 }
+ },
+ {
+ user_id: 123,
+ name: 'pageview',
+ pathname: '/page2',
+ timestamp: { minutesAgo: 1498 }
+ }
+ ]
+ })
+
+ await page.goto(`/${domain}/settings/funnels`, { waitUntil: 'commit' })
+ await expectLiveViewConnected(page)
+
+ await page.getByRole('button', { name: 'Add funnel' }).click()
+
+ await expect(page.locator('#funnel-form')).toBeVisible()
+
+ await page.locator('#step-1 input').click()
+
+ await page
+ .locator('#step-1 .input-picker-dropdown-option')
+ .getByText('Visit /page1')
+ .click()
+
+ await expect(page.locator('#step-1 input')).toHaveValue('Visit /page1')
+
+ await page.locator('#step-2 input').click()
+
+ await expect(
+ page
+ .locator('#step-2 .input-picker-dropdown-option')
+ .getByText('Scroll 90% on /page1')
+ ).toBeVisible()
+ })
+})
diff --git a/extra/lib/plausible/funnel.ex b/extra/lib/plausible/funnel.ex
index d6803274af..c4e862053a 100644
--- a/extra/lib/plausible/funnel.ex
+++ b/extra/lib/plausible/funnel.ex
@@ -90,12 +90,15 @@ defmodule Plausible.Funnel do
def as_goal(%Step{} = step), do: Step.as_goal(step)
def as_goal(%DynamicStep{} = step), do: DynamicStep.as_goal(step)
- def changeset(funnel \\ %__MODULE__{}, attrs \\ %{}) do
+ def changeset(funnel, attrs, opts \\ []) do
+ allowed_goal_ids = Keyword.get(opts, :allowed_goal_ids, [])
+
funnel
|> cast(attrs, [:name, :funnel_type])
|> validate_required([:name])
|> set_funnel_type()
|> put_steps(attrs[:steps] || attrs["steps"])
+ |> validate_steps(allowed_goal_ids)
|> validate_steps_length()
|> unique_constraint(:name,
name: :funnels_name_site_id_index
@@ -145,10 +148,33 @@ defmodule Plausible.Funnel do
|> Ecto.Changeset.put_embed(:dynamic_steps, dynamic_steps)
end
+ defp validate_steps(changeset, allowed_goal_ids) do
+ rogue_goals? =
+ changeset
+ |> get_assoc(:steps)
+ # NOTE: `Ecto.Changeset.Relation` is part of an internal Ecto API.
+ # However, there's no other way to filter out empty entries
+ # from changes to carry out validations.
+ |> Ecto.Changeset.Relation.filter_empty()
+ |> Enum.any?(&(get_change(&1, :goal_id) not in allowed_goal_ids))
+
+ if rogue_goals? do
+ add_error(changeset, :steps, "contain invalid goals")
+ else
+ changeset
+ end
+ end
+
defp validate_steps_length(changeset) do
+ # NOTE: `Ecto.Changeset.Relation` is part of an internal Ecto API.
+ # It's used by `validate_length` when using it against relations,
+ # but it's not exposed in any way directly to use it to validate
+ # multiple fields at once.
steps_count =
- length(Ecto.Changeset.get_assoc(changeset, :steps)) +
- length(Ecto.Changeset.get_embed(changeset, :dynamic_steps))
+ [get_assoc(changeset, :steps), get_embed(changeset, :dynamic_steps)]
+ |> Enum.map(&Ecto.Changeset.Relation.filter_empty/1)
+ |> Enum.map(&length/1)
+ |> Enum.sum()
cond do
steps_count < @min_steps ->
diff --git a/extra/lib/plausible/funnel/dynamic_step.ex b/extra/lib/plausible/funnel/dynamic_step.ex
index 7dfd61d8cd..1e95f9916c 100644
--- a/extra/lib/plausible/funnel/dynamic_step.ex
+++ b/extra/lib/plausible/funnel/dynamic_step.ex
@@ -44,7 +44,7 @@ defmodule Plausible.Funnel.DynamicStep do
def as_goal(step) do
%Plausible.Goal{
id: 0,
- display_name: display_name(step),
+ display_name: Plausible.Goal.display_name(step.event_name, step.page_path),
event_name: step.event_name,
page_path: step.page_path,
scroll_threshold: step.scroll_threshold,
@@ -60,14 +60,4 @@ defmodule Plausible.Funnel.DynamicStep do
changeset
end
end
-
- defp display_name(step) do
- case step do
- %{page_path: page_path} when is_binary(page_path) ->
- "Visit " <> page_path
-
- %{event_name: event_name} when is_binary(event_name) ->
- event_name
- end
- end
end
diff --git a/extra/lib/plausible/funnels.ex b/extra/lib/plausible/funnels.ex
index fd5d896751..26c7e6a829 100644
--- a/extra/lib/plausible/funnels.ex
+++ b/extra/lib/plausible/funnels.ex
@@ -21,6 +21,8 @@ defmodule Plausible.Funnels do
def create(site, name, steps, opts)
when is_list(steps) and length(steps) in Funnel.min_steps()..Funnel.max_steps() do
site = Plausible.Repo.preload(site, :team)
+ goals = Plausible.Goals.for_site(site)
+ opts = Keyword.put(opts, :allowed_goal_ids, Enum.map(goals, & &1.id))
case Plausible.Billing.Feature.Funnels.check_availability(site.team) do
{:error, _} = error ->
@@ -42,6 +44,8 @@ defmodule Plausible.Funnels do
| {:error, Ecto.Changeset.t() | :invalid_funnel_size | :upgrade_required}
def update(funnel, name, steps, opts \\ []) do
site = Plausible.Repo.preload(funnel, site: :team).site
+ goals = Plausible.Goals.for_site(site)
+ opts = Keyword.put(opts, :allowed_goal_ids, Enum.map(goals, & &1.id))
case Plausible.Billing.Feature.Funnels.check_availability(site.team) do
{:error, _} = error ->
@@ -59,11 +63,15 @@ defmodule Plausible.Funnels do
def create_changeset(site, name, steps, opts \\ []) do
funnel_type = Keyword.get(opts, :funnel_type, Funnel.default_funnel_type())
- Funnel.changeset(%Funnel{site_id: site.id}, %{
- name: name,
- steps: steps,
- funnel_type: funnel_type
- })
+ Funnel.changeset(
+ %Funnel{site_id: site.id},
+ %{
+ name: name,
+ steps: steps,
+ funnel_type: funnel_type
+ },
+ opts
+ )
end
@spec edit_changeset(Plausible.Funnel.t(), String.t(), [map()], Keyword.t()) ::
@@ -78,7 +86,7 @@ defmodule Plausible.Funnels do
params
end
- Funnel.changeset(funnel, params)
+ Funnel.changeset(funnel, params, opts)
end
@spec ephemeral_definition(Plausible.Site.t(), String.t(), [map()], Keyword.t()) :: Funnel.t()
diff --git a/extra/lib/plausible/stats/funnel.ex b/extra/lib/plausible/stats/funnel.ex
index ba18393754..ea8b989138 100644
--- a/extra/lib/plausible/stats/funnel.ex
+++ b/extra/lib/plausible/stats/funnel.ex
@@ -15,8 +15,60 @@ defmodule Plausible.Stats.Funnel do
alias Plausible.ClickhouseRepo
alias Plausible.Stats.{Base, Comparisons, DateTimeRange, Query}
+ alias Plausible.Stats.Exploration
alias Plausible.Stats.Goal.Revenue
+ @spec suggest(Plausible.Site.t(), Plausible.Stats.Query.t(), Keyword.t()) ::
+ {[Plausible.Goal.t()], [Plausible.Goal.t()]}
+ def suggest(site, query, opts \\ []) do
+ goals = Keyword.get(opts, :goals, [])
+ input = Keyword.get(opts, :input, "")
+
+ goals_map =
+ Map.new(goals, fn g -> {{g.event_name || "pageview", g.page_path || ""}, g} end)
+
+ {:ok, suggestions} =
+ Exploration.next_steps(site, query, [], search_term: input, include_wildcard?: false)
+
+ {suggestions, unmatched_goals} =
+ suggestions
+ |> Enum.reject(&(&1.step.name == Exploration.Journey.Step.journey_end_event()))
+ |> Enum.reduce({[], goals_map}, fn suggestion, {suggestions, goals_map} ->
+ case step_to_goal(Map.fetch!(suggestion, :step), goals_map) do
+ {goal, :static, key} ->
+ {[goal | suggestions], Map.delete(goals_map, key)}
+
+ {goal, :dynamic} ->
+ {[goal | suggestions], goals_map}
+ end
+ end)
+
+ {Enum.reverse(suggestions), Map.values(unmatched_goals)}
+ end
+
+ @spec step_to_goal(Exploration.Journey.Step.t(), %{
+ {String.t(), String.t()} => Plausible.Goal.t()
+ }) ::
+ {Plausible.Goal.t(), :static, {binary(), binary()}} | {Plausible.Goal.t(), :dynamic}
+ def step_to_goal(step, goals_map) do
+ goal = goals_map[{step.name, step.pathname}]
+
+ if not is_nil(goal) and step.is_goal and
+ Plausible.Goal.display_name(step.name, step.pathname) == goal.display_name do
+ {goal, :static, {step.name, step.pathname}}
+ else
+ goal =
+ %Plausible.Goal{}
+ |> Plausible.Goal.changeset(%{
+ event_name: if(step.name != "pageview", do: step.name),
+ page_path: step.pathname
+ })
+ |> Ecto.Changeset.apply_changes()
+
+ {goal, :dynamic}
+ end
+ end
+
@spec funnel(Plausible.Site.t(), Plausible.Stats.Query.t(), Funnel.t() | pos_integer()) ::
{:ok, map()} | {:error, :funnel_not_found}
def funnel(site, query, funnel_id) when is_integer(funnel_id) do
diff --git a/extra/lib/plausible/stats/journey/step.ex b/extra/lib/plausible/stats/journey/step.ex
index 0b205ea401..a0bae45c4e 100644
--- a/extra/lib/plausible/stats/journey/step.ex
+++ b/extra/lib/plausible/stats/journey/step.ex
@@ -21,7 +21,11 @@ defmodule Plausible.Stats.Exploration.Journey.Step do
@spec journey_end_label() :: String.t()
def journey_end_label, do: @journey_end_label
- @spec from(map()) :: t()
+ @spec from(map() | Plausible.Goal.t()) :: t()
+ def from(%Plausible.Goal{} = goal) do
+ new(goal.event_name || "pageview", goal.page_path || "", false, 0, true)
+ end
+
def from(step) do
new(step.name, step.pathname, step.includes_subpaths, step.subpaths_count, step.is_goal)
end
diff --git a/extra/lib/plausible_web/live/funnel_settings.ex b/extra/lib/plausible_web/live/funnel_settings.ex
index 8255cd75cc..71b8c6b4a4 100644
--- a/extra/lib/plausible_web/live/funnel_settings.ex
+++ b/extra/lib/plausible_web/live/funnel_settings.ex
@@ -47,7 +47,7 @@ defmodule PlausibleWeb.Live.FunnelSettings do
# Normally, we'd have to use live_patch which we can't do with views unmounted at the router it seems.
def render(assigns) do
~H"""
- <div id="funnel-settings-main">
+ <div id="funnel-settings-main" class="group">
<.flash_messages flash={@flash} />
<.tile
@@ -65,6 +65,18 @@ defmodule PlausibleWeb.Live.FunnelSettings do
<:subtitle :if={Enum.count(@all_funnels) > 0}>
Compose goals into funnels to track user flows and conversion rates.
</:subtitle>
+ <div
+ :if={@setup_funnel?}
+ class="fixed flex inset-0 bg-gray-500/75 transition-opacity z-50"
+ phx-window-keydown="cancel-setup-funnel"
+ phx-key="Escape"
+ >
+ <div class="w-full self-center block group-has-[#funnel-form]:hidden">
+ <div class="text-center">
+ <.spinner class="inline-block h-8 w-8" />
+ </div>
+ </div>
+ </div>
<%= if @setup_funnel? do %>
{live_render(
@socket,
@@ -128,6 +140,11 @@ defmodule PlausibleWeb.Live.FunnelSettings do
{:noreply, assign(socket, setup_funnel?: true)}
end
+ def handle_event("cancel-setup-funnel", _value, socket) do
+ send(self(), :cancel_setup_funnel)
+ {:noreply, socket}
+ end
+
def handle_event("edit-funnel", %{"funnel-id" => id}, socket) do
{:noreply, assign(socket, setup_funnel?: true, funnel_id: String.to_integer(id))}
end
diff --git a/extra/lib/plausible_web/live/funnel_settings/form.ex b/extra/lib/plausible_web/live/funnel_settings/form.ex
index faa6c1d895..ac02043fec 100644
--- a/extra/lib/plausible_web/live/funnel_settings/form.ex
+++ b/extra/lib/plausible_web/live/funnel_settings/form.ex
@@ -1,15 +1,14 @@
defmodule PlausibleWeb.Live.FunnelSettings.Form do
@moduledoc """
- Phoenix LiveComponent that renders a form used for setting up funnels.
- Makes use of dynamically placed `PlausibleWeb.Live.FunnelSettings.ComboBox` components
- to allow building searchable funnel definitions out of list of goals available.
+ Phoenix LiveComponent that renders a form used for setting up dynamic funnels.
"""
use PlausibleWeb, :live_view
use Plausible.Funnel
import PlausibleWeb.Live.Components.Form
- alias Plausible.{Goals, Funnels}
+
+ alias Plausible.Funnels
alias Plausible.Stats.QueryBuilder
def mount(_params, %{"domain" => domain} = session, socket) do
@@ -23,32 +22,27 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
]
)
- # We'll have the options trimmed to only the data we care about, to keep
- # it minimal at the socket assigns, yet, we want to retain specific %Goal{}
- # fields, so that `String.Chars` protocol and `Funnels.ephemeral_definition/3`
- # are applicable downstream.
goals =
site
- |> Goals.for_site()
+ |> Plausible.Goals.for_site()
|> Enum.map(fn goal ->
- {goal.id,
- struct!(
- Plausible.Goal,
- Map.take(goal, [
- :id,
- :display_name,
- :event_name,
- :page_path,
- :currency,
- :scroll_threshold,
- :custom_props
- ])
- )}
+ struct!(
+ Plausible.Goal,
+ Map.take(goal, [
+ :id,
+ :display_name,
+ :event_name,
+ :page_path,
+ :scroll_threshold,
+ :currency,
+ :custom_props
+ ])
+ )
end)
socket =
socket
- |> assign(goals: goals, site: site, evaluation_result: nil)
+ |> assign(site: site, goals: goals, evaluation_result: nil)
|> prepare_socket(site, session["funnel_id"])
{:ok, socket}
@@ -56,22 +50,24 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
def render(assigns) do
~H"""
- <div
- class="fixed inset-0 bg-gray-500/75 transition-opacity z-50"
- phx-window-keydown="cancel-add-funnel"
- phx-key="Escape"
- >
- </div>
<div class="fixed inset-0 flex items-center justify-center mt-16 z-50 overlofw-y-auto overflow-x-hidden">
<div class="md:w-2/3 max-w-xl h-full">
- <div id="funnel-form">
+ <div
+ id="funnel-form"
+ class="group relative"
+ phx-click-away="cancel-add-funnel"
+ phx-target="#funnel-form"
+ >
+ <div class="absolute inset-0 z-10 hidden items-center justify-center rounded-lg bg-white/50 dark:bg-gray-900/50 group-has-[.phx-click-loading]:flex">
+ <.spinner class="h-8 w-8" />
+ </div>
+
<.form
:let={f}
for={@form}
phx-change="validate"
phx-submit="save"
phx-target="#funnel-form"
- phx-click-away="cancel-add-funnel"
onkeydown="return event.key != 'Enter';"
class="bg-white dark:bg-gray-900 shadow-2xl rounded-lg px-8 pt-6 pb-8 mb-4 mt-8"
>
@@ -152,17 +148,14 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
<div :for={step_idx <- @step_ids} class="flex items-center my-3">
<div class="w-2/5 flex-1">
<.live_component
- selected={find_preselected(@funnel, @funnel_modified?, step_idx)}
- submit_name={"funnel[steps][#{step_idx}][goal_id]"}
- module={PlausibleWeb.Live.Components.ComboBox}
- suggest_fun={&PlausibleWeb.Live.Components.ComboBox.StaticSearch.suggest/2}
- on_selection_made={
- fn value, by_id ->
- send(self(), {:selection_made, %{submit_value: value, by: by_id}})
- end
- }
id={"step-#{step_idx}"}
- options={reject_already_selected(@goals, @selections_made)}
+ module={PlausibleWeb.Live.Components.PrimaCombobox}
+ submit_name={"funnel[steps][#{step_idx}][step_data]"}
+ selected={selected_option(@steps, step_idx)}
+ suggest_trigger={@steps}
+ suggest_fun={
+ fn input, _suggestions -> suggest(input, @site, @goals, @steps, step_idx) end
+ }
/>
</div>
@@ -183,10 +176,9 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
</div>
<div class="flex flex-col gap-y-2 mt-2">
- <.add_step_button :if={
- length(@step_ids) < Funnel.max_steps() and
- map_size(@selections_made) < length(@goals)
- } />
+ <p>
+ <.add_step_button :if={length(@step_ids) < Funnel.max_steps()} />
+ </p>
<p id="funnel-eval" class="text-gray-800 dark:text-gray-200 text-sm">
<%= if @evaluation_result do %>
Last month conversion rate: <strong><%= List.last(@evaluation_result.steps).conversion_rate %></strong>%
@@ -197,10 +189,10 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
<.button
id="save"
type="submit"
- class="w-full mt-6"
+ class="w-full mt-4"
disabled={
- has_steps_errors?(f) or map_size(@selections_made) < Funnel.min_steps() or
- length(@step_ids) > map_size(@selections_made)
+ has_steps_errors?(f) or map_size(@steps) < Funnel.min_steps() or
+ length(@step_ids) > map_size(@steps)
}
>
<span>{if @funnel, do: "Update", else: "Add"} funnel</span>
@@ -274,8 +266,6 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
def handle_event("add-step", _value, socket) do
step_ids = socket.assigns.step_ids
- socket = assign(socket, funnel_modified?: true)
-
if length(step_ids) < Funnel.max_steps() do
first_free_idx = find_sequence_break(step_ids)
new_ids = step_ids ++ [first_free_idx]
@@ -288,12 +278,11 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
def handle_event("remove-step", %{"step-idx" => idx}, socket) do
idx = String.to_integer(idx)
step_ids = List.delete(socket.assigns.step_ids, idx)
- selections_made = drop_selection(socket.assigns.selections_made, idx)
+ steps = drop_step(socket.assigns.steps, idx)
send(self(), :evaluate_funnel)
- {:noreply,
- assign(socket, step_ids: step_ids, selections_made: selections_made, funnel_modified?: true)}
+ {:noreply, assign(socket, step_ids: step_ids, steps: steps)}
end
def handle_event("switch-type", %{"type" => funnel_type_str}, socket) do
@@ -305,25 +294,29 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
def handle_event("validate", %{"funnel" => params}, socket) do
funnel_type = socket.assigns.funnel_type
+ allowed_goal_ids = Enum.map(socket.assigns.goals, & &1.id)
- steps_from_assigns =
- socket.assigns.step_ids
- |> Enum.reduce([], fn step_id, acc ->
- goal = Map.get(socket.assigns.selections_made, "step-#{step_id}")
- if goal, do: [%{"goal_id" => goal.id} | acc], else: acc
- end)
- |> Enum.reverse()
+ steps_with_idx =
+ params
+ |> Map.get("steps", [])
+ |> Enum.sort_by(&elem(&1, 0))
+ |> Enum.map(fn {idx, payload} -> {idx, JSON.decode!(payload["step_data"])} end)
changeset =
socket.assigns.site
|> Funnels.create_changeset(
params["name"],
- steps_from_assigns,
- funnel_type: funnel_type
+ Enum.map(steps_with_idx, &elem(&1, 1)),
+ funnel_type: funnel_type,
+ allowed_goal_ids: allowed_goal_ids
)
|> Map.put(:action, :validate)
- {:noreply, assign(socket, form: to_form(changeset))}
+ steps = store_steps(socket.assigns, steps_with_idx)
+
+ send(self(), :evaluate_funnel)
+
+ {:noreply, assign(socket, form: to_form(changeset), steps: steps)}
end
def handle_event(
@@ -337,7 +330,8 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
}
} = socket
) do
- steps = Enum.map(params["steps"], fn {_idx, payload} -> payload end)
+ steps =
+ Enum.map(params["steps"], fn {_idx, payload} -> JSON.decode!(payload["step_data"]) end)
save_fn =
case funnel do
@@ -374,23 +368,197 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
{:noreply, socket}
end
- def handle_info({:selection_made, %{submit_value: goal_id, by: combo_box}}, socket) do
- selections_made = store_selection(socket.assigns, combo_box, goal_id)
+ def handle_info(:evaluate_funnel, socket) do
+ {:noreply, evaluate_funnel(socket)}
+ end
- send(self(), :evaluate_funnel)
+ defp suggest(input, site, goals, steps, step_idx) do
+ steps =
+ steps
+ |> Enum.filter(fn {idx, _} -> idx != "step-#{step_idx}" end)
+ |> Enum.map(&elem(&1, 1))
+
+ query =
+ QueryBuilder.build!(site,
+ metrics: [:pageviews],
+ input_date_range: {:last_n_days, 30}
+ )
+
+ {goals_for_query, other_goals} =
+ Enum.split_with(goals, fn g ->
+ g.scroll_threshold == -1 and map_size(g.custom_props) == 0 and is_nil(g.currency)
+ end)
+
+ {suggestions, unmatched_query_goals} =
+ Plausible.Stats.Funnel.suggest(site, query, goals: goals_for_query, input: input)
- {:noreply,
- assign(socket,
- selections_made: selections_made
- )}
+ unmatched_goal_entries =
+ (unmatched_query_goals ++ other_goals)
+ |> static_suggest(input)
+ |> to_suggestion_entries(steps)
+
+ suggestions
+ |> to_suggestion_entries(steps)
+ |> Enum.concat(unmatched_goal_entries)
+ |> segment()
end
- def handle_info(:evaluate_funnel, socket) do
- {:noreply, evaluate_funnel(socket)}
+ defp static_suggest(goals, input) do
+ # NOTE: static search for goals logic should be in sync with
+ # `Plausible.Stats.Exploration.maybe_search/2`.
+
+ case String.trim(input) do
+ term when byte_size(term) > 2 ->
+ term = String.downcase(term)
+
+ Enum.filter(goals, fn g ->
+ display_name = String.downcase(g.display_name)
+ event_name = String.downcase(g.event_name || "")
+ page_path = String.downcase(g.page_path || "")
+
+ String.contains?(display_name, term) or String.contains?(event_name, term) or
+ String.contains?(page_path, term)
+ end)
+
+ _ ->
+ goals
+ end
+ end
+
+ defp to_suggestion_entries(suggestions, steps) do
+ suggestions
+ |> exclude_existing_steps(steps)
+ |> Enum.map(
+ &{to_step_data(&1), to_string(&1),
+ [goal: &1, type: if(&1.event_name, do: :custom_event, else: :pageview)]}
+ )
+ |> set_icons()
+ end
+
+ defp set_icons(suggestions) do
+ Enum.map(suggestions, fn {value, display_name, opts} ->
+ icon =
+ case opts[:type] do
+ :custom_event -> :cursor
+ :pageview -> :eye
+ end
+
+ {value, display_name, Keyword.put(opts, :icon, icon)}
+ end)
+ end
+
+ defp segment(suggestions) do
+ suggestions
+ |> Enum.group_by(fn {_, _, opts} -> opts[:type] end)
+ |> Enum.sort_by(fn {k, _} ->
+ case k do
+ :custom_event -> 0
+ :pageview -> 1
+ end
+ end)
+ |> Enum.reduce([], fn {_, segment}, acc ->
+ [{value, display_name, opts} | rest] = segment
+
+ separator? = acc != []
+
+ title =
+ case opts[:type] do
+ :custom_event -> "Custom events"
+ :pageview -> "Pageviews"
+ end
+
+ opts =
+ opts
+ |> Keyword.put(:title, title)
+ |> Keyword.put(:separator?, separator?)
+
+ acc ++ [{value, display_name, opts} | rest]
+ end)
+ end
+
+ defp exclude_existing_steps(goals, steps) do
+ existing_steps =
+ MapSet.new(steps, &{&1.event_name, &1.page_path, &1.scroll_threshold, &1.custom_props})
+
+ Enum.reject(
+ goals,
+ &MapSet.member?(
+ existing_steps,
+ {&1.event_name, &1.page_path, &1.scroll_threshold, &1.custom_props}
+ )
+ )
+ end
+
+ defp has_steps_errors?(f) do
+ not f.source.valid?
+ end
+
+ defp store_steps(assigns, steps_with_idx) do
+ Enum.reduce(steps_with_idx, assigns.steps, fn {idx, step_data}, steps ->
+ Map.put(steps, "step-#{idx}", to_goal(step_data))
+ end)
+ end
+
+ defp drop_step(steps, idx) do
+ step_input_id = "step-#{idx}"
+ Map.delete(steps, step_input_id)
end
- defp evaluate_funnel(%{assigns: %{selections_made: selections_made}} = socket)
- when map_size(selections_made) < Funnel.min_steps() do
+ defp selected_option(steps, idx) do
+ if goal = steps["step-#{idx}"] do
+ {to_step_data(goal), to_string(goal)}
+ end
+ end
+
+ defp to_goal(%{"goal_id" => goal_id} = step_data) do
+ step_data
+ |> Map.delete("goal_id")
+ |> then(&Plausible.Goal.changeset(%Plausible.Goal{}, &1))
+ |> Ecto.Changeset.apply_changes()
+ |> Map.put(:id, goal_id)
+ end
+
+ defp to_goal(step_data) do
+ step_data
+ |> Funnel.DynamicStep.changeset()
+ |> Ecto.Changeset.apply_changes()
+ |> Funnel.DynamicStep.as_goal()
+ end
+
+ defp to_step_data(%Plausible.Goal{} = goal) do
+ data = %{
+ event_name: goal.event_name,
+ page_path: goal.page_path,
+ display_name: goal.display_name,
+ scroll_threshold: goal.scroll_threshold,
+ currency: goal.currency
+ }
+
+ data =
+ if is_integer(goal.id) and goal.id > 0 do
+ Map.put(data, :goal_id, goal.id)
+ else
+ data
+ end
+
+ JSON.encode!(data)
+ end
+
+ defp find_sequence_break(input) do
+ input
+ |> Enum.sort()
+ |> Enum.with_index(1)
+ |> Enum.reduce_while(nil, fn {x, order}, _ ->
+ if x != order do
+ {:halt, order}
+ else
+ {:cont, order + 1}
+ end
+ end)
+ end
+
+ defp evaluate_funnel(%{assigns: %{steps: steps}} = socket)
+ when map_size(steps) < Funnel.min_steps() do
socket
end
@@ -398,13 +566,14 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
%{
assigns: %{
site: site,
- selections_made: selections_made,
- funnel_type: funnel_type
+ steps: steps,
+ funnel_type: funnel_type,
+ goals: goals
}
} = socket
) do
with {:ok, {definition, query}} <-
- build_ephemeral_funnel(site, selections_made, funnel_type: funnel_type),
+ build_ephemeral_funnel(site, steps, funnel_type: funnel_type, goals: goals),
{:ok, funnel} <- Plausible.Stats.funnel(site, query, definition) do
assign(socket, evaluation_result: funnel)
else
@@ -413,22 +582,30 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
end
end
- defp build_ephemeral_funnel(site, selections_made, opts) do
+ defp build_ephemeral_funnel(site, steps, opts) do
+ {goals, opts} = Keyword.pop(opts, :goals, [])
+ opts = Keyword.put(opts, :allowed_goal_ids, Enum.map(goals, & &1.id))
+
steps =
- selections_made
+ steps
|> Enum.sort_by(&elem(&1, 0))
|> Enum.map(fn {_, goal} ->
- %{
- "goal_id" => goal.id,
- "goal" => %{
- "id" => goal.id,
- "event_name" => goal.event_name,
- "page_path" => goal.page_path,
- "scroll_threshold" => goal.scroll_threshold,
- "currency" => goal.currency,
- "custom_props" => goal.custom_props
- }
+ step_data = %{
+ "event_name" => goal.event_name,
+ "page_path" => goal.page_path,
+ "currency" => goal.currency,
+ "scroll_threshold" => goal.scroll_threshold,
+ "custom_props" => goal.custom_props
}
+
+ if goal.id > 0 do
+ %{
+ "goal_id" => goal.id,
+ "goal" => Map.put(step_data, "id", goal.id)
+ }
+ else
+ step_data
+ end
end)
definition =
@@ -448,70 +625,18 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
{:ok, {definition, query}}
end
- defp find_sequence_break(input) do
- input
- |> Enum.sort()
- |> Enum.with_index(1)
- |> Enum.reduce_while(nil, fn {x, order}, _ ->
- if x != order do
- {:halt, order}
- else
- {:cont, order + 1}
- end
- end)
- end
-
- defp has_steps_errors?(f) do
- not f.source.valid?
- end
-
- defp get_goal(assigns, id) do
- assigns
- |> Map.fetch!(:goals)
- |> Enum.find_value(fn
- {goal_id, goal} when goal_id == id -> goal
- _ -> nil
- end)
- end
-
- defp store_selection(assigns, combo_box, goal_id) do
- Map.put(assigns.selections_made, combo_box, get_goal(assigns, goal_id))
- end
-
- defp drop_selection(selections_made, step_idx) do
- step_input_id = "step-#{step_idx}"
- Map.delete(selections_made, step_input_id)
- end
-
- defp reject_already_selected(goals, selections_made) do
- selection_ids =
- Enum.map(selections_made, fn
- {_, %{id: goal_id}} -> goal_id
- end)
-
- Enum.reject(goals, fn {goal_id, _} -> goal_id in selection_ids end)
- end
-
- defp find_preselected(%Funnel{} = funnel, false, idx) do
- if goal = Enum.at(Funnel.goals(funnel), idx - 1) do
- {goal.id, to_string(goal)}
- end
- end
-
- defp find_preselected(_, _, _), do: nil
-
defp prepare_socket(socket, site, funnel_id) when is_integer(funnel_id) do
funnel = Funnels.get(site.id, funnel_id)
- goals = Funnel.goals(funnel)
+ steps = Funnel.goals(funnel)
form =
funnel
- |> Funnels.edit_changeset(funnel.name, goals)
+ |> Funnels.edit_changeset(funnel.name, steps)
|> to_form()
- selections_made =
- Enum.reduce(Enum.with_index(goals, 1), %{}, fn {goal, idx}, acc ->
- Map.put(acc, "step-#{idx}", goal)
+ steps =
+ Enum.reduce(Enum.with_index(steps, 1), %{}, fn {step, idx}, acc ->
+ Map.put(acc, "step-#{idx}", step)
end)
socket =
@@ -520,9 +645,8 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
form: form,
funnel: funnel,
funnel_type: funnel.funnel_type,
- funnel_modified?: false,
- selections_made: selections_made,
- step_ids: Enum.to_list(1..Enum.count(goals))
+ steps: steps,
+ step_ids: Enum.to_list(1..Enum.count(steps))
)
evaluate_funnel(socket)
@@ -536,8 +660,7 @@ defmodule PlausibleWeb.Live.FunnelSettings.Form do
form: form,
funnel: nil,
funnel_type: :sequential,
- funnel_modified?: false,
- selections_made: Map.new(),
+ steps: Map.new(),
step_ids: Enum.to_list(1..Funnel.min_steps())
)
end
diff --git a/lib/plausible/goal.ex b/lib/plausible/goal.ex
index 202b6bf851..0c613d04cf 100644
--- a/lib/plausible/goal.ex
+++ b/lib/plausible/goal.ex
@@ -109,6 +109,14 @@ defmodule Plausible.Goal do
goal.display_name
end
+ @spec display_name(event_name :: String.t() | nil, page_path :: String.t() | nil) :: String.t()
+ def display_name(event_name, page_path) do
+ case {event_name, page_path} do
+ {event, path} when event in [nil, "", "pageview"] and is_binary(path) -> "Visit " <> path
+ {event, _} when is_binary(event) -> event
+ end
+ end
+
@spec type(t()) :: :event | :scroll | :page
def type(goal) do
cond do
@@ -217,10 +225,10 @@ defmodule Plausible.Goal do
case clause do
[nil, path, _] when is_binary(path) ->
- put_change(changeset, :display_name, "Visit " <> path)
+ put_change(changeset, :display_name, display_name(nil, path))
[nil, _, event_name] when is_binary(event_name) ->
- put_change(changeset, :display_name, event_name)
+ put_change(changeset, :display_name, display_name(event_name, nil))
_ ->
changeset
diff --git a/lib/plausible_web/components/icons.ex b/lib/plausible_web/components/icons.ex
index 551b7855ec..c726499c81 100644
--- a/lib/plausible_web/components/icons.ex
+++ b/lib/plausible_web/components/icons.ex
@@ -255,4 +255,20 @@ defmodule PlausibleWeb.Components.Icons do
</svg>
"""
end
+
+ attr :class, :any, default: []
+
+ def cursor_icon(assigns) do
+ ~H"""
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" class={@class}>
+ <path
+ stroke="currentColor"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ stroke-width="1.5"
+ d="m4.63 3.711 15.23 5.565c.641.235.623 1.148-.028 1.358l-6.97 2.23-2.232 6.971c-.208.65-1.122.67-1.357.028L3.71 4.631a.717.717 0 0 1 .92-.92"
+ />
+ </svg>
+ """
+ end
end
diff --git a/lib/plausible_web/live/components/prima_combobox.ex b/lib/plausible_web/live/components/prima_combobox.ex
new file mode 100644
index 0000000000..c87170a479
--- /dev/null
+++ b/lib/plausible_web/live/components/prima_combobox.ex
@@ -0,0 +1,166 @@
+defmodule PlausibleWeb.Live.Components.PrimaCombobox do
+ @moduledoc false
+ use PlausibleWeb, :live_component
+
+ import Prima.Combobox
+
+ def mount(socket) do
+ socket =
+ socket
+ |> assign(:suggestions, [])
+
+ {:ok, socket}
+ end
+
+ def update(assigns, socket) do
+ selected =
+ case assigns[:selected] do
+ {value, display_name} -> %{value: value, display: display_name}
+ _ -> socket.assigns[:selected]
+ end
+
+ socket =
+ socket
+ |> assign(assigns)
+ |> assign(:selected, selected)
+
+ suggest_trigger = assigns[:suggest_trigger] && hash(assigns[:suggest_trigger])
+
+ socket =
+ if suggest_trigger && suggest_trigger != socket.assigns[:suggest_trigger] do
+ suggestions = handle_suggest(socket, "")
+ assign(socket, :suggestions, suggestions)
+ else
+ socket
+ end
+
+ {:ok, assign(socket, :suggest_trigger, suggest_trigger)}
+ end
+
+ attr(:id, :string, required: true)
+ attr(:selected, :any)
+ attr(:placeholder, :string, default: "Select option or search by typing")
+ attr(:class, :string, default: "")
+ attr(:input_class, :string, default: "")
+ attr(:dropdown_class, :string, default: "")
+ attr(:submit_name, :string, required: true)
+
+ def render(assigns) do
+ ~H"""
+ <div
+ id={"combobox-container-#{@id}"}
+ class="relative [&_[data-prima-ref=options-wrapper]]:w-full"
+ >
+ <.combobox :let={input_value} name={@submit_name} id={@id} class={@class} selections={@selected}>
+ <.combobox_input
+ id={"combobox-search-input-#{@id}"}
+ value={input_value}
+ class={
+ Enum.join(
+ [
+ "peer input-base input-md w-full pr-10",
+ @input_class
+ ],
+ " "
+ )
+ }
+ on_search="async_combobox_search"
+ phx-target={@myself}
+ placeholder={@placeholder}
+ />
+
+ <.spinner class="absolute inset-y-3 right-3.5 invisible phx-hook-loading:peer-focus:visible" />
+
+ <.combobox_options
+ id={"input-picker-dropdown-#{@id}"}
+ phx-update="replace"
+ offset={4}
+ class={
+ Enum.join(
+ [
+ "relative z-50 max-h-60 w-full overflow-y-auto overflow-x-hidden p-1 rounded-md shadow-lg bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-800 focus:outline-hidden",
+ @dropdown_class
+ ],
+ " "
+ )
+ }
+ >
+ <div class="flex flex-col gap-0.5">
+ <%= for {idx, {value, display_name, opts}} <- @suggestions do %>
+ <div
+ :if={opts[:separator?]}
+ class="my-0.5 -mx-1 border-b border-gray-200 dark:border-gray-700"
+ />
+
+ <div
+ :if={opts[:title]}
+ class="px-4 py-2.5 truncate text-xs uppercase font-medium text-gray-500 dark:text-gray-400"
+ >
+ {opts[:title]}
+ </div>
+
+ <.combobox_option
+ id={"input-picker-dropdown-#{@id}-option-#{idx}"}
+ class="input-picker-dropdown-option px-4 py-2.5 leading-5 rounded-md text-sm text-gray-800 dark:text-gray-200 hover:text-gray-900 dark:hover:text-gray-100 cursor-pointer select-none data-focus:bg-gray-100 data-focus:text-gray-900 dark:data-focus:bg-gray-700 dark:data-focus:text-gray-100 data-selected:bg-gray-100 data-selected:text-gray-900 dark:data-selected:bg-gray-700 dark:data-selected:text-gray-100"
+ value={value}
+ display={display_name}
+ >
+ <span class="flex items-center gap-2">
+ <.icon :if={opts[:icon]} name={opts[:icon]} />
+ <span class="truncate">{display_name}</span>
+ </span>
+ </.combobox_option>
+ <% end %>
+
+ <div
+ :if={@suggestions == []}
+ class="px-4 py-2.5 text-sm text-gray-500 dark:text-gray-400 cursor-default select-none"
+ >
+ No matches found. Try searching for something different.
+ </div>
+ </div>
+ </.combobox_options>
+ </.combobox>
+ </div>
+ """
+ end
+
+ @icons %{
+ cursor: &PlausibleWeb.Components.Icons.cursor_icon/1,
+ eye: &Heroicons.eye/1
+ }
+
+ defp icon(assigns) do
+ {name, assigns} = Map.pop(assigns, :name)
+
+ assigns = assign(assigns, :class, "size-4 shrink-0")
+
+ if icon_component = @icons[name] do
+ icon_component.(assigns)
+ else
+ ~H""
+ end
+ end
+
+ def handle_event("async_combobox_search", %{"query" => query}, socket) do
+ suggestions = handle_suggest(socket, query)
+
+ {:noreply, assign(socket, suggestions: suggestions)}
+ end
+
+ defp handle_suggest(socket, query) do
+ existing_suggestions = Enum.map(socket.assigns.suggestions, &elem(&1, 1))
+
+ suggestions = socket.assigns.suggest_fun.(query, existing_suggestions)
+
+ Enum.map(suggestions, &{hash(&1), &1})
+ end
+
+ defp hash(value) do
+ value
+ |> :erlang.term_to_binary()
+ |> then(&:crypto.hash(:md5, &1))
+ |> Base.encode16()
+ |> String.downcase()
+ end
+end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 9250703ec2..f59bbc911a 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -191,6 +191,7 @@ defmodule PlausibleWeb.Router do
post "/stats", E2EController, :populate_stats
post "/funnel", E2EController, :create_funnel
+ get "/funnel", E2EController, :get_funnel
post "/goal", E2EController, :create_goal
put "/verification", E2EController, :put_verification_scenario
end
diff --git a/test/plausible/funnel_test.exs b/test/plausible/funnel_test.exs
index 1b94d5e894..9a12d9c39a 100644
--- a/test/plausible/funnel_test.exs
+++ b/test/plausible/funnel_test.exs
@@ -51,7 +51,8 @@ defmodule Plausible.FunnelTest do
]
}
- changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)
+ changeset =
+ Funnel.changeset(%Funnel{site_id: site.id}, params, allowed_goal_ids: [g1.id, g2.id])
assert changeset.valid?
@@ -134,7 +135,10 @@ defmodule Plausible.FunnelTest do
]
}
- changeset = Funnel.changeset(%Funnel{site_id: site.id}, params)
+ changeset =
+ Funnel.changeset(%Funnel{site_id: site.id}, params,
+ allowed_goal_ids: [g1.id, g2.id, g3.id]
+ )
assert changeset.valid?
diff --git a/test/plausible/funnels_test.exs b/test/plausible/funnels_test.exs
index 20f11ec269..f4c7586b04 100644
--- a/test/plausible/funnels_test.exs
+++ b/test/plausible/funnels_test.exs
@@ -6,8 +6,6 @@ defmodule Plausible.FunnelsTest do
alias Plausible.Funnel
alias Plausible.Funnels
alias Plausible.Goals
- alias Plausible.Stats
- alias Plausible.Stats.QueryBuilder
setup do
site = new_site()
@@ -270,6 +268,49 @@ defmodule Plausible.FunnelsTest do
refute Funnels.get(site, funnel.id)
end
+ test "a created funnel cannot include goals from another site", %{
+ site: site,
+ steps: [g1, g2 | _]
+ } do
+ other_site = new_site()
+ {:ok, g3} = Goals.create(other_site, %{"page_path" => "/somewhere/else"})
+
+ {:error, changeset} =
+ Funnels.create(
+ site,
+ "From blog to signup and further",
+ [g1, g2, g3]
+ )
+
+ refute changeset.valid?
+ assert changeset.errors[:steps]
+ end
+
+ test "an updated funnel cannot include goals from another site", %{
+ site: site,
+ steps: [g1, g2 | _]
+ } do
+ other_site = new_site()
+ {:ok, g3} = Goals.create(other_site, %{"page_path" => "/somewhere/else"})
+
+ {:ok, funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup",
+ [g1, g2]
+ )
+
+ {:error, changeset} =
+ Funnels.update(
+ funnel,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ refute changeset.valid?
+ assert changeset.errors[:steps]
+ end
+
test "a funnel cannot be made of < 2 steps", %{site: site, steps: [g1 | _]} do
assert {:error, :invalid_funnel_size} =
Funnels.create(
@@ -330,464 +371,5 @@ defmodule Plausible.FunnelsTest do
funnels_list
end
end
-
- describe "Plausible.Stats.Funnel" do
- test "funnels can be evaluated per site within a time range against an ephemeral definition",
- %{
- site: site,
- goals: [g1, g2, g3 | _]
- } do
- funnel_definition =
- Funnels.ephemeral_definition(
- site,
- "From blog to signup and purchase",
- [
- %{
- "goal_id" => "#{g1.id}",
- "goal" => %{
- "id" => "#{g1.id}",
- "event_name" => g1.event_name,
- "page_path" => g1.page_path
- }
- },
- %{
- "goal_id" => g2.id,
- "goal" => %{
- "id" => g2.id,
- "event_name" => g2.event_name,
- "page_path" => g2.page_path
- }
- },
- %{
- "goal_id" => g3.id,
- "goal" => %{
- "id" => g3.id,
- "event_name" => g3.event_name,
- "page_path" => g3.page_path
- }
- }
- ]
- )
-
- populate_stats(site, [
- build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
- build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
- build(:event, name: "Signup", user_id: 123),
- build(:pageview, pathname: "/checkout", user_id: 123),
- build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
- build(:event, name: "Signup", user_id: 666)
- ])
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- funnel_data = Stats.funnel(site, query, funnel_definition)
-
- assert {:ok,
- %{
- all_visitors: 3,
- entering_visitors: 2,
- entering_visitors_percentage: "66.67",
- never_entering_visitors: 1,
- never_entering_visitors_percentage: "33.33",
- steps: [
- %{
- label: "Visit /go/to/blog/**",
- visitors: 2,
- conversion_rate: "100",
- conversion_rate_step: "0",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Signup",
- visitors: 2,
- conversion_rate: "100",
- conversion_rate_step: "100",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Visit /checkout",
- visitors: 1,
- conversion_rate: "50",
- conversion_rate_step: "50",
- dropoff: 1,
- dropoff_percentage: "50"
- }
- ]
- }} = funnel_data
- end
-
- test "ephemeral definition creation crashes when passed data are invalid", %{site: site} do
- assert_raise MatchError, fn ->
- Funnels.ephemeral_definition(site, "Empty", [])
- end
- end
-
- test "mixed funnels can be evaluated per site within a time range against an ephemeral definition",
- %{
- site: site,
- goals: [g1, g2, g3 | _]
- } do
- funnel_definition =
- Funnels.ephemeral_definition(
- site,
- "From blog to signup and purchase",
- [
- %{
- "goal_id" => "#{g1.id}",
- "goal" => %{
- "id" => "#{g1.id}",
- "event_name" => g1.event_name,
- "page_path" => g1.page_path
- }
- },
- %{"event_name" => g2.event_name},
- %{"page_path" => g3.page_path}
- ]
- )
-
- populate_stats(site, [
- build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
- build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
- build(:event, name: "Signup", user_id: 123),
- build(:pageview, pathname: "/checkout", user_id: 123),
- build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
- build(:event, name: "Signup", user_id: 666)
- ])
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- funnel_data = Stats.funnel(site, query, funnel_definition)
-
- assert {:ok,
- %{
- all_visitors: 3,
- entering_visitors: 2,
- entering_visitors_percentage: "66.67",
- never_entering_visitors: 1,
- never_entering_visitors_percentage: "33.33",
- steps: [
- %{
- label: "Visit /go/to/blog/**",
- visitors: 2,
- conversion_rate: "100",
- conversion_rate_step: "0",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Signup",
- visitors: 2,
- conversion_rate: "100",
- conversion_rate_step: "100",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Visit /checkout",
- visitors: 1,
- conversion_rate: "50",
- conversion_rate_step: "50",
- dropoff: 1,
- dropoff_percentage: "50"
- }
- ]
- }} = funnel_data
- end
-
- test "funnels can be evaluated per site within a time range", %{
- site: site,
- steps: [g1, g2, g3 | _]
- } do
- {:ok, funnel} =
- Funnels.create(
- site,
- "From blog to signup and purchase",
- [g1, g2, g3]
- )
-
- populate_stats(site, [
- build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
- build(:event, name: "Signup", user_id: 123),
- build(:pageview, pathname: "/checkout", user_id: 123),
- build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
- build(:event, name: "Signup", user_id: 666)
- ])
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- funnel_data = Stats.funnel(site, query, funnel.id)
-
- assert {:ok,
- %{
- all_visitors: 2,
- entering_visitors: 2,
- entering_visitors_percentage: "100",
- never_entering_visitors: 0,
- never_entering_visitors_percentage: "0",
- steps: [
- %{
- label: "Visit /go/to/blog/**",
- visitors: 2,
- conversion_rate: "100",
- conversion_rate_step: "0",
- dropoff: 0
- },
- %{
- label: "Signup",
- visitors: 2,
- conversion_rate: "100",
- conversion_rate_step: "100",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Visit /checkout",
- visitors: 1,
- conversion_rate: "50",
- conversion_rate_step: "50",
- dropoff: 1,
- dropoff_percentage: "50"
- }
- ]
- }} = funnel_data
- end
-
- test "strict-order funnels stop at intervention events", %{
- site: site,
- steps: [g1, g2, g3 | _]
- } do
- {:ok, non_strict_funnel} =
- Funnels.create(
- site,
- "From blog to signup and purchase",
- [g1, g2, g3]
- )
-
- {:ok, strict_funnel} =
- Funnels.create(
- site,
- "Strict from blog to signup and purchase",
- [g1, g2, g3],
- funnel_type: :strict
- )
-
- populate_stats(site, [
- build(:pageview,
- pathname: "/go/to/blog/foo",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:00]
- ),
- build(:pageview,
- pathname: "/some/other/page",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:01]
- ),
- build(:event,
- name: "Signup",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:02]
- ),
- build(:pageview,
- pathname: "/checkout",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:03]
- )
- ])
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- {:ok, non_strict_funnel_data} = Stats.funnel(site, query, non_strict_funnel.id)
- {:ok, strict_funnel_data} = Stats.funnel(site, query, strict_funnel.id)
-
- assert Enum.at(non_strict_funnel_data.steps, 2).visitors == 1
- assert Enum.at(strict_funnel_data.steps, 2).visitors == 0
-
- assert non_strict_funnel_data.funnel_type == :sequential
- assert strict_funnel_data.funnel_type == :strict
- end
-
- test "flexible funnels count everyone who reaches first and last step in the last step", %{
- site: site,
- steps: [g1, g2, g3 | _]
- } do
- {:ok, non_flexible_funnel} =
- Funnels.create(
- site,
- "From blog to signup and purchase",
- [g1, g2, g3]
- )
-
- {:ok, flexible_funnel} =
- Funnels.create(
- site,
- "Flexible from blog to signup and purchase",
- [g1, g2, g3],
- funnel_type: :flexible
- )
-
- populate_stats(site, [
- build(:pageview,
- pathname: "/go/to/blog/foo",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:00]
- ),
- build(:pageview,
- pathname: "/checkout",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:03]
- )
- ])
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- {:ok, non_flexible_funnel_data} = Stats.funnel(site, query, non_flexible_funnel.id)
- {:ok, flexible_funnel_data} = Stats.funnel(site, query, flexible_funnel.id)
-
- assert Enum.at(non_flexible_funnel_data.steps, 0).visitors == 1
- assert Enum.at(non_flexible_funnel_data.steps, 1).visitors == 0
- assert Enum.at(non_flexible_funnel_data.steps, 2).visitors == 0
- assert Enum.at(flexible_funnel_data.steps, 0).visitors == 1
- assert Enum.at(flexible_funnel_data.steps, 1).visitors == 0
- assert Enum.at(flexible_funnel_data.steps, 2).visitors == 1
-
- assert non_flexible_funnel_data.funnel_type == :sequential
- assert flexible_funnel_data.funnel_type == :flexible
- end
-
- test "flexible mixed funnels count everyone who reaches first and last step in the last step",
- %{
- site: site,
- steps: [g1 | _]
- } do
- s2 = %{"event_name" => "Signup"}
- s3 = %{"page_path" => "/checkout"}
-
- {:ok, non_flexible_funnel} =
- Funnels.create(
- site,
- "From blog to signup and purchase",
- [g1, s2, s3]
- )
-
- {:ok, flexible_funnel} =
- Funnels.create(
- site,
- "Flexible from blog to signup and purchase",
- [g1, s2, s3],
- funnel_type: :flexible
- )
-
- populate_stats(site, [
- build(:pageview,
- pathname: "/go/to/blog/foo",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:00]
- ),
- build(:pageview,
- pathname: "/checkout",
- user_id: 123,
- timestamp: ~N[2021-01-01 00:00:03]
- )
- ])
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- {:ok, non_flexible_funnel_data} = Stats.funnel(site, query, non_flexible_funnel.id)
- {:ok, flexible_funnel_data} = Stats.funnel(site, query, flexible_funnel.id)
-
- assert Enum.at(non_flexible_funnel_data.steps, 0).visitors == 1
- assert Enum.at(non_flexible_funnel_data.steps, 1).visitors == 0
- assert Enum.at(non_flexible_funnel_data.steps, 2).visitors == 0
- assert Enum.at(flexible_funnel_data.steps, 0).visitors == 1
- assert Enum.at(flexible_funnel_data.steps, 1).visitors == 0
- assert Enum.at(flexible_funnel_data.steps, 2).visitors == 1
-
- assert non_flexible_funnel_data.funnel_type == :sequential
- assert flexible_funnel_data.funnel_type == :flexible
- end
-
- test "funnels can be evaluated even where there are no visits yet", %{
- site: site,
- steps: [g1, g2, g3 | _]
- } do
- {:ok, funnel} =
- Funnels.create(
- site,
- "From blog to signup and purchase",
- [g1, g2, g3]
- )
-
- query = QueryBuilder.build!(site, input_date_range: :all)
-
- funnel_data = Stats.funnel(site, query, funnel.id)
-
- assert {:ok,
- %{
- all_visitors: 0,
- entering_visitors: 0,
- entering_visitors_percentage: "0",
- never_entering_visitors: 0,
- never_entering_visitors_percentage: "0",
- steps: [
- %{
- label: "Visit /go/to/blog/**",
- visitors: 0,
- conversion_rate: "0",
- conversion_rate_step: "0",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Signup",
- visitors: 0,
- conversion_rate: "0",
- conversion_rate_step: "0",
- dropoff: 0,
- dropoff_percentage: "0"
- },
- %{
- label: "Visit /checkout",
- visitors: 0,
- conversion_rate: "0",
- conversion_rate_step: "0",
- dropoff: 0,
- dropoff_percentage: "0"
- }
- ]
- }} = funnel_data
- end
-
- @tag :slow
- test "sampling", %{site: site, steps: [g1, g2, g3 | _]} do
- {:ok, funnel} =
- Funnels.create(
- site,
- "From blog to signup and purchase",
- [g1, g2, g3]
- )
-
- stats =
- 1..50_000
- |> Enum.flat_map(fn _ ->
- user_id = SipHash.hash!("0123456789ABCDEF", :crypto.strong_rand_bytes(64))
-
- [
- build(:pageview, pathname: "/go/to/blog/foo", user_id: user_id),
- build(:event, name: "Signup", user_id: user_id),
- build(:pageview, pathname: "/checkout", user_id: user_id)
- ]
- end)
-
- populate_stats(site, stats)
-
- query =
- Plausible.Stats.Query.from(site, %{"period" => "all", "sample_threshold" => "10000"})
-
- {:ok, funnel_data} = Stats.funnel(site, query, funnel.id)
- assert_in_delta funnel_data[:all_visitors], 50_000, 5000
- end
- end
end
end
diff --git a/test/plausible/stats/funnel_test.exs b/test/plausible/stats/funnel_test.exs
new file mode 100644
index 0000000000..cc0e65b366
--- /dev/null
+++ b/test/plausible/stats/funnel_test.exs
@@ -0,0 +1,565 @@
+defmodule Plausible.Stats.FunnelTest do
+ use Plausible.DataCase
+
+ @moduletag :ee_only
+
+ on_ee do
+ alias Plausible.Funnels
+ alias Plausible.Goals
+ alias Plausible.Stats
+ alias Plausible.Stats.QueryBuilder
+
+ setup do
+ site = new_site()
+
+ {:ok, g1} = Goals.create(site, %{"page_path" => "/go/to/blog/**"})
+ {:ok, g2} = Goals.create(site, %{"event_name" => "Signup"})
+ {:ok, g3} = Goals.create(site, %{"page_path" => "/checkout"})
+
+ {:ok,
+ %{
+ site: site,
+ goals: [g1, g2, g3],
+ steps: [g1, g2, g3] |> Enum.map(&%{"goal_id" => &1.id})
+ }}
+ end
+
+ describe "suggest/3" do
+ test "returns empty on no goals and no stats", %{site: site} do
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ assert {[], []} = Stats.Funnel.suggest(site, query)
+ end
+
+ test "returns all goals as unmatched when there are not stats", %{
+ site: site,
+ goals: [g1, g2, g3 | _]
+ } do
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ assert {[], unmatched} = Stats.Funnel.suggest(site, query, goals: [g1, g2, g3])
+
+ unmatched_ids = Enum.map(unmatched, & &1.id)
+
+ assert length(unmatched_ids) == 3
+ assert g1.id in unmatched_ids
+ assert g2.id in unmatched_ids
+ assert g3.id in unmatched_ids
+ end
+
+ test "returns suggestions from stats when no goals provided", %{site: site} do
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
+ build(:event, name: "Signup", user_id: 123),
+ build(:pageview, pathname: "/checkout", user_id: 123),
+ build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
+ build(:event, name: "Signup", user_id: 666)
+ ])
+
+ assert {suggestions, []} = Stats.Funnel.suggest(site, query)
+ suggestions = Enum.map(suggestions, &{&1.event_name, &1.page_path})
+
+ assert length(suggestions) == 6
+ assert {"Signup", nil} in suggestions
+ assert {nil, "/go/to/blog/foo"} in suggestions
+ assert {nil, "/go/to/blog/**"} in suggestions
+ assert {nil, "/go/to/blog/bar"} in suggestions
+ assert {nil, "/checkout"} in suggestions
+ assert {nil, "/irrelevant/page/not/in/funnel"} in suggestions
+ end
+
+ test "returns only unmatched goals", %{site: site, goals: [g1, g2, g3 | _]} do
+ {:ok, g4} = Goals.create(site, %{"event_name" => "Unmatched"})
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
+ build(:event, name: "Signup", user_id: 123),
+ build(:pageview, pathname: "/checkout", user_id: 123),
+ build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
+ build(:event, name: "Signup", user_id: 666)
+ ])
+
+ assert {suggestions, [unmatched]} =
+ Stats.Funnel.suggest(site, query, goals: [g1, g2, g3, g4])
+
+ suggestions = Enum.map(suggestions, &{&1.event_name, &1.page_path})
+
+ assert length(suggestions) == 6
+ assert {"Signup", nil} in suggestions
+ assert {nil, "/go/to/blog/foo"} in suggestions
+ assert {nil, "/go/to/blog/**"} in suggestions
+ assert {nil, "/go/to/blog/bar"} in suggestions
+ assert {nil, "/checkout"} in suggestions
+ assert {nil, "/irrelevant/page/not/in/funnel"} in suggestions
+
+ assert unmatched.event_name == "Unmatched"
+ end
+ end
+
+ describe "funnel/3" do
+ test "funnels can be evaluated per site within a time range against an ephemeral definition",
+ %{
+ site: site,
+ goals: [g1, g2, g3 | _]
+ } do
+ funnel_definition =
+ Funnels.ephemeral_definition(
+ site,
+ "From blog to signup and purchase",
+ [
+ %{
+ "goal_id" => "#{g1.id}",
+ "goal" => %{
+ "id" => "#{g1.id}",
+ "event_name" => g1.event_name,
+ "page_path" => g1.page_path
+ }
+ },
+ %{
+ "goal_id" => g2.id,
+ "goal" => %{
+ "id" => g2.id,
+ "event_name" => g2.event_name,
+ "page_path" => g2.page_path
+ }
+ },
+ %{
+ "goal_id" => g3.id,
+ "goal" => %{
+ "id" => g3.id,
+ "event_name" => g3.event_name,
+ "page_path" => g3.page_path
+ }
+ }
+ ],
+ allowed_goal_ids: [g1.id, g2.id, g3.id]
+ )
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
+ build(:event, name: "Signup", user_id: 123),
+ build(:pageview, pathname: "/checkout", user_id: 123),
+ build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
+ build(:event, name: "Signup", user_id: 666)
+ ])
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ funnel_data = Stats.funnel(site, query, funnel_definition)
+
+ assert {:ok,
+ %{
+ all_visitors: 3,
+ entering_visitors: 2,
+ entering_visitors_percentage: "66.67",
+ never_entering_visitors: 1,
+ never_entering_visitors_percentage: "33.33",
+ steps: [
+ %{
+ label: "Visit /go/to/blog/**",
+ visitors: 2,
+ conversion_rate: "100",
+ conversion_rate_step: "0",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Signup",
+ visitors: 2,
+ conversion_rate: "100",
+ conversion_rate_step: "100",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Visit /checkout",
+ visitors: 1,
+ conversion_rate: "50",
+ conversion_rate_step: "50",
+ dropoff: 1,
+ dropoff_percentage: "50"
+ }
+ ]
+ }} = funnel_data
+ end
+
+ test "ephemeral definition creation crashes when passed data are invalid", %{site: site} do
+ assert_raise MatchError, fn ->
+ Funnels.ephemeral_definition(site, "Empty", [])
+ end
+ end
+
+ test "mixed funnels can be evaluated per site within a time range against an ephemeral definition",
+ %{
+ site: site,
+ goals: [g1, g2, g3 | _]
+ } do
+ funnel_definition =
+ Funnels.ephemeral_definition(
+ site,
+ "From blog to signup and purchase",
+ [
+ %{
+ "goal_id" => "#{g1.id}",
+ "goal" => %{
+ "id" => "#{g1.id}",
+ "event_name" => g1.event_name,
+ "page_path" => g1.page_path
+ }
+ },
+ %{"event_name" => g2.event_name},
+ %{"page_path" => g3.page_path}
+ ],
+ allowed_goal_ids: [g1.id, g2.id, g3.id]
+ )
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/irrelevant/page/not/in/funnel", user_id: 999),
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
+ build(:event, name: "Signup", user_id: 123),
+ build(:pageview, pathname: "/checkout", user_id: 123),
+ build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
+ build(:event, name: "Signup", user_id: 666)
+ ])
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ funnel_data = Stats.funnel(site, query, funnel_definition)
+
+ assert {:ok,
+ %{
+ all_visitors: 3,
+ entering_visitors: 2,
+ entering_visitors_percentage: "66.67",
+ never_entering_visitors: 1,
+ never_entering_visitors_percentage: "33.33",
+ steps: [
+ %{
+ label: "Visit /go/to/blog/**",
+ visitors: 2,
+ conversion_rate: "100",
+ conversion_rate_step: "0",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Signup",
+ visitors: 2,
+ conversion_rate: "100",
+ conversion_rate_step: "100",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Visit /checkout",
+ visitors: 1,
+ conversion_rate: "50",
+ conversion_rate_step: "50",
+ dropoff: 1,
+ dropoff_percentage: "50"
+ }
+ ]
+ }} = funnel_data
+ end
+
+ test "funnels can be evaluated per site within a time range", %{
+ site: site,
+ steps: [g1, g2, g3 | _]
+ } do
+ {:ok, funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ populate_stats(site, [
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: 123),
+ build(:event, name: "Signup", user_id: 123),
+ build(:pageview, pathname: "/checkout", user_id: 123),
+ build(:pageview, pathname: "/go/to/blog/bar", user_id: 666),
+ build(:event, name: "Signup", user_id: 666)
+ ])
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ funnel_data = Stats.funnel(site, query, funnel.id)
+
+ assert {:ok,
+ %{
+ all_visitors: 2,
+ entering_visitors: 2,
+ entering_visitors_percentage: "100",
+ never_entering_visitors: 0,
+ never_entering_visitors_percentage: "0",
+ steps: [
+ %{
+ label: "Visit /go/to/blog/**",
+ visitors: 2,
+ conversion_rate: "100",
+ conversion_rate_step: "0",
+ dropoff: 0
+ },
+ %{
+ label: "Signup",
+ visitors: 2,
+ conversion_rate: "100",
+ conversion_rate_step: "100",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Visit /checkout",
+ visitors: 1,
+ conversion_rate: "50",
+ conversion_rate_step: "50",
+ dropoff: 1,
+ dropoff_percentage: "50"
+ }
+ ]
+ }} = funnel_data
+ end
+
+ test "strict-order funnels stop at intervention events", %{
+ site: site,
+ steps: [g1, g2, g3 | _]
+ } do
+ {:ok, non_strict_funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ {:ok, strict_funnel} =
+ Funnels.create(
+ site,
+ "Strict from blog to signup and purchase",
+ [g1, g2, g3],
+ funnel_type: :strict
+ )
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/go/to/blog/foo",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageview,
+ pathname: "/some/other/page",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:01]
+ ),
+ build(:event,
+ name: "Signup",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:02]
+ ),
+ build(:pageview,
+ pathname: "/checkout",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:03]
+ )
+ ])
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ {:ok, non_strict_funnel_data} = Stats.funnel(site, query, non_strict_funnel.id)
+ {:ok, strict_funnel_data} = Stats.funnel(site, query, strict_funnel.id)
+
+ assert Enum.at(non_strict_funnel_data.steps, 2).visitors == 1
+ assert Enum.at(strict_funnel_data.steps, 2).visitors == 0
+
+ assert non_strict_funnel_data.funnel_type == :sequential
+ assert strict_funnel_data.funnel_type == :strict
+ end
+
+ test "flexible funnels count everyone who reaches first and last step in the last step", %{
+ site: site,
+ steps: [g1, g2, g3 | _]
+ } do
+ {:ok, non_flexible_funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ {:ok, flexible_funnel} =
+ Funnels.create(
+ site,
+ "Flexible from blog to signup and purchase",
+ [g1, g2, g3],
+ funnel_type: :flexible
+ )
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/go/to/blog/foo",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageview,
+ pathname: "/checkout",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:03]
+ )
+ ])
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ {:ok, non_flexible_funnel_data} = Stats.funnel(site, query, non_flexible_funnel.id)
+ {:ok, flexible_funnel_data} = Stats.funnel(site, query, flexible_funnel.id)
+
+ assert Enum.at(non_flexible_funnel_data.steps, 0).visitors == 1
+ assert Enum.at(non_flexible_funnel_data.steps, 1).visitors == 0
+ assert Enum.at(non_flexible_funnel_data.steps, 2).visitors == 0
+ assert Enum.at(flexible_funnel_data.steps, 0).visitors == 1
+ assert Enum.at(flexible_funnel_data.steps, 1).visitors == 0
+ assert Enum.at(flexible_funnel_data.steps, 2).visitors == 1
+
+ assert non_flexible_funnel_data.funnel_type == :sequential
+ assert flexible_funnel_data.funnel_type == :flexible
+ end
+
+ test "flexible mixed funnels count everyone who reaches first and last step in the last step",
+ %{
+ site: site,
+ steps: [g1 | _]
+ } do
+ s2 = %{"event_name" => "Signup"}
+ s3 = %{"page_path" => "/checkout"}
+
+ {:ok, non_flexible_funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, s2, s3]
+ )
+
+ {:ok, flexible_funnel} =
+ Funnels.create(
+ site,
+ "Flexible from blog to signup and purchase",
+ [g1, s2, s3],
+ funnel_type: :flexible
+ )
+
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/go/to/blog/foo",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageview,
+ pathname: "/checkout",
+ user_id: 123,
+ timestamp: ~N[2021-01-01 00:00:03]
+ )
+ ])
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ {:ok, non_flexible_funnel_data} = Stats.funnel(site, query, non_flexible_funnel.id)
+ {:ok, flexible_funnel_data} = Stats.funnel(site, query, flexible_funnel.id)
+
+ assert Enum.at(non_flexible_funnel_data.steps, 0).visitors == 1
+ assert Enum.at(non_flexible_funnel_data.steps, 1).visitors == 0
+ assert Enum.at(non_flexible_funnel_data.steps, 2).visitors == 0
+ assert Enum.at(flexible_funnel_data.steps, 0).visitors == 1
+ assert Enum.at(flexible_funnel_data.steps, 1).visitors == 0
+ assert Enum.at(flexible_funnel_data.steps, 2).visitors == 1
+
+ assert non_flexible_funnel_data.funnel_type == :sequential
+ assert flexible_funnel_data.funnel_type == :flexible
+ end
+
+ test "funnels can be evaluated even where there are no visits yet", %{
+ site: site,
+ steps: [g1, g2, g3 | _]
+ } do
+ {:ok, funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ query = QueryBuilder.build!(site, input_date_range: :all)
+
+ funnel_data = Stats.funnel(site, query, funnel.id)
+
+ assert {:ok,
+ %{
+ all_visitors: 0,
+ entering_visitors: 0,
+ entering_visitors_percentage: "0",
+ never_entering_visitors: 0,
+ never_entering_visitors_percentage: "0",
+ steps: [
+ %{
+ label: "Visit /go/to/blog/**",
+ visitors: 0,
+ conversion_rate: "0",
+ conversion_rate_step: "0",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Signup",
+ visitors: 0,
+ conversion_rate: "0",
+ conversion_rate_step: "0",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ },
+ %{
+ label: "Visit /checkout",
+ visitors: 0,
+ conversion_rate: "0",
+ conversion_rate_step: "0",
+ dropoff: 0,
+ dropoff_percentage: "0"
+ }
+ ]
+ }} = funnel_data
+ end
+
+ @tag :slow
+ test "sampling", %{site: site, steps: [g1, g2, g3 | _]} do
+ {:ok, funnel} =
+ Funnels.create(
+ site,
+ "From blog to signup and purchase",
+ [g1, g2, g3]
+ )
+
+ stats =
+ 1..50_000
+ |> Enum.flat_map(fn _ ->
+ user_id = SipHash.hash!("0123456789ABCDEF", :crypto.strong_rand_bytes(64))
+
+ [
+ build(:pageview, pathname: "/go/to/blog/foo", user_id: user_id),
+ build(:event, name: "Signup", user_id: user_id),
+ build(:pageview, pathname: "/checkout", user_id: user_id)
+ ]
+ end)
+
+ populate_stats(site, stats)
+
+ query =
+ Plausible.Stats.Query.from(site, %{"period" => "all", "sample_threshold" => "10000"})
+
+ {:ok, funnel_data} = Stats.funnel(site, query, funnel.id)
+ assert_in_delta funnel_data[:all_visitors], 50_000, 5000
+ end
+ end
+ end
+end
diff --git a/test/plausible_web/live/funnel_settings/form_test.exs b/test/plausible_web/live/funnel_settings/form_test.exs
deleted file mode 100644
index 38709ec4d4..0000000000
--- a/test/plausible_web/live/funnel_settings/form_test.exs
+++ /dev/null
@@ -1,117 +0,0 @@
-defmodule PlausibleWeb.Live.FunnelSettings.FormTest do
- use PlausibleWeb.ConnCase, async: true
- @moduletag :ee_only
-
- import Phoenix.LiveViewTest
-
- describe "integration - live rendering" do
- setup [:create_user, :log_in, :create_site]
-
- test "search reacts to the input, the user types in", %{conn: conn, site: site} do
- setup_goals(site, ["Hello World", "Plausible", "Another World"])
- lv = get_liveview(conn, site)
-
- doc = type_into_combo(lv, 1, "hello")
-
- assert text_of_element(doc, "#dropdown-step-1-option-1") == "Hello World"
-
- doc = type_into_combo(lv, 1, "plausible")
-
- assert text_of_element(doc, "#dropdown-step-1-option-1") == "Plausible"
- end
-
- test "selecting an option prefills input values", %{conn: conn, site: site} do
- {:ok, [_, _, g3]} = setup_goals(site, ["Hello World", "Plausible", "Another World"])
- lv = get_liveview(conn, site)
-
- doc = type_into_combo(lv, 1, "another")
-
- refute element_exists?(doc, ~s/input[type="hidden"][value="#{g3.id}"]/)
- refute element_exists?(doc, ~s/input[type="text"][value="Another World"]/)
-
- lv
- |> element("li#dropdown-step-1-option-1 a")
- |> render_click()
-
- assert lv
- |> element("#submit-step-1")
- |> render()
- |> element_exists?(~s/input[type="hidden"][value="#{g3.id}"]/)
-
- assert lv
- |> element("#step-1")
- |> render()
- |> element_exists?(~s/input[type="text"][value="Another World"]/)
- end
-
- test "selecting one option reduces suggestions in the other", %{conn: conn, site: site} do
- setup_goals(site, ["Hello World", "Plausible", "Another World"])
- lv = get_liveview(conn, site)
-
- type_into_combo(lv, 1, "another")
-
- lv
- |> element("li#dropdown-step-1-option-1 a")
- |> render_click()
-
- doc = type_into_combo(lv, 2, "another")
-
- refute text_of_element(doc, "ul#dropdown-step-1 li") =~ "Another World"
- refute text_of_element(doc, "ul#dropdown-step-2 li") =~ "Another World"
- end
-
- test "removing one option alters suggestions for other", %{conn: conn, site: site} do
- setup_goals(site, ["Hello World", "Plausible", "Another World"])
-
- lv = get_liveview(conn, site)
-
- lv |> element(~s/a[phx-click="add-step"]/) |> render_click()
-
- type_into_combo(lv, 2, "hello")
-
- lv
- |> element("li#dropdown-step-2-option-1 a")
- |> render_click()
-
- doc = type_into_combo(lv, 1, "hello")
-
- refute text_of_element(doc, "ul#dropdown-step-0 li") =~ "Hello World"
-
- lv |> element(~s/#remove-step-2/) |> render_click()
-
- doc = type_into_combo(lv, 1, "hello")
-
- assert text_of_element(doc, "ul#dropdown-step-1 li") =~ "Hello World"
- end
- end
-
- defp get_liveview(conn, site) do
- conn = assign(conn, :live_module, PlausibleWeb.Live.FunnelSettings)
- {:ok, lv, _html} = live(conn, "/#{site.domain}/settings/funnels")
- lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
- assert form_view = find_live_child(lv, "funnels-form")
- form_view |> element("form") |> render_change(%{funnel: %{name: "My test funnel"}})
- form_view
- end
-
- defp setup_goals(site, goal_names) when is_list(goal_names) do
- goals =
- Enum.map(goal_names, fn goal_name ->
- {:ok, g} =
- Plausible.Goals.create(site, %{"event_name" => goal_name}, max_goals_per_site: 100)
-
- g
- end)
-
- {:ok, goals}
- end
-
- defp type_into_combo(lv, idx, text) do
- lv
- |> element("input#step-#{idx}")
- |> render_change(%{
- "_target" => ["display-step-#{idx}"],
- "display-step-#{idx}" => "#{text}"
- })
- end
-end
diff --git a/test/plausible_web/live/funnel_settings_test.exs b/test/plausible_web/live/funnel_settings_test.exs
index ee70571723..a41091317a 100644
--- a/test/plausible_web/live/funnel_settings_test.exs
+++ b/test/plausible_web/live/funnel_settings_test.exs
@@ -181,21 +181,16 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert element_exists?(
doc,
- ~s/form[phx-change="validate"][phx-submit="save"][phx-click-away="cancel-add-funnel"]/
+ ~s/form[phx-change="validate"][phx-submit="save"]/
)
assert element_exists?(doc, ~s/form input[type="text"][name="funnel[name]"]/)
- assert element_exists?(
- doc,
- ~s/input[type="hidden"][name="funnel[steps][1][goal_id]"]#submit-step-1/
- )
-
step_setup_controls = [
- ~s/input[type="hidden"][name="funnel[steps][1][goal_id]"]#submit-step-1/,
- ~s/input[type="hidden"][name="funnel[steps][2][goal_id]"]#submit-step-2/,
- ~s/input[type="text"][name="display-step-1"]#step-1/,
- ~s/input[type="text"][name="display-step-2"]#step-2/,
+ ~s/#step-1 select[hidden][name="funnel[steps][1][step_data]"]/,
+ ~s/#step-2 select[hidden][name="funnel[steps][2][step_data]"]/,
+ ~s/#step-1 input[type="text"]/,
+ ~s/#step-2 input[type="text"]/,
~s/a[phx-click="add-step"]/
]
@@ -217,10 +212,10 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert element_exists?(
doc,
- ~s/input[type="hidden"][name="funnel[steps][3][goal_id]"]#submit-step-3/
+ ~s/#step-3 select[hidden][name="funnel[steps][3][step_data]"]/
)
- assert element_exists?(doc, ~s/input[type="text"][name="display-step-1"]#step-1/)
+ assert element_exists?(doc, ~s/#step-3 input[type="text"]/)
assert element_exists?(
doc,
@@ -249,85 +244,9 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
doc = lv |> element(~s/#remove-step-2/) |> render_click()
- assert element_exists?(doc, ~s/input#step-1/)
- assert element_exists?(doc, ~s/input#step-3/)
- refute element_exists?(doc, ~s/input#step-2/)
- end
-
- test "save button becomes active once at least two steps are selected", %{
- conn: conn,
- site: site
- } do
- setup_goals(site)
- lv = get_liveview(conn, site)
- lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
-
- assert lv = find_live_child(lv, "funnels-form")
-
- lv
- |> element("li#dropdown-step-1-option-1 a")
- |> render_click()
-
- doc =
- lv
- |> element("li#dropdown-step-2-option-1 a")
- |> render_click()
-
- assert element_exists?(doc, ~s/form button#save:disabled/)
-
- doc =
- lv
- |> element("form")
- |> render_change(%{
- funnel: %{
- name: "My test funnel",
- steps: [
- %{goal_id: 1},
- %{goal_id: 2}
- ]
- }
- })
-
- assert element_exists?(doc, ~s/form button#save/)
- refute element_exists?(doc, ~s/form button#save:disabled/)
- end
-
- test "save button saves a new funnel", %{
- conn: conn,
- site: site
- } do
- setup_goals(site)
- lv = get_liveview(conn, site)
- lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
-
- assert lv = find_live_child(lv, "funnels-form")
-
- lv
- |> element("li#dropdown-step-1-option-1 a")
- |> render_click()
-
- lv
- |> element("li#dropdown-step-2-option-1 a")
- |> render_click()
-
- lv
- |> element("form")
- |> render_change(%{
- funnel: %{
- name: "My test funnel",
- steps: [
- %{goal_id: 1},
- %{goal_id: 2}
- ]
- }
- })
-
- lv
- |> element(~s/form/)
- |> render_submit()
-
- assert %Plausible.Funnel{steps: [_, _], funnel_type: :sequential} =
- Plausible.Funnels.get(site, "My test funnel")
+ assert element_exists?(doc, ~s/#step-1 input/)
+ assert element_exists?(doc, ~s/#step-3 input/)
+ refute element_exists?(doc, ~s/#step-2 input/)
end
test "save button saves strict-order setting", %{
@@ -441,10 +360,19 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert lv = find_live_child(lv, "funnels-form")
- assert lv |> element("#step-1") |> render() |> text_of_attr("value") ==
- "Visit /go/to/blog/**"
+ %{"display_name" => "Visit /go/to/blog/**"} =
+ lv
+ |> element("#step-1_submit option[selected]")
+ |> render()
+ |> text_of_attr("value")
+ |> JSON.decode!()
- assert lv |> element("#step-2") |> render() |> text_of_attr("value") == "Signup"
+ %{"display_name" => "Signup"} =
+ lv
+ |> element("#step-2_submit option[selected]")
+ |> render()
+ |> text_of_attr("value")
+ |> JSON.decode!()
end
test "funnel is re-evaluated when strict-order is changed", %{
@@ -619,45 +547,6 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert text_of_element(render(lv), ~s/#funnel-eval/) =~ "Last month conversion rate: 50%"
end
- test "clicking save after editing the funnel, updates it", %{
- conn: conn,
- site: site
- } do
- {:ok, [f1_id, _]} = setup_funnels(site)
- {:ok, %{id: goal_id}} = Plausible.Goals.create(site, %{"page_path" => "/"})
-
- lv = get_liveview(conn, site)
-
- lv
- |> element(~s/button[phx-click="edit-funnel"][phx-value-funnel-id="#{f1_id}"]/)
- |> render_click()
-
- assert lv = find_live_child(lv, "funnels-form")
-
- lv
- |> element("li#dropdown-step-2-option-1 a")
- |> render_click()
-
- lv
- |> element("form")
- |> render_change(%{
- funnel: %{
- name: "Updated funnel",
- steps: [
- %{goal_id: 1},
- %{goal_id: goal_id}
- ]
- }
- })
-
- lv
- |> element(~s/form/)
- |> render_submit()
-
- assert %Plausible.Funnel{steps: [_, %Plausible.Funnel.Step{goal_id: ^goal_id}]} =
- Plausible.Funnels.get(site, "Updated funnel")
- end
-
test "clicking save after editing the funnel, updates strict-order setting", %{
conn: conn,
site: site
@@ -725,31 +614,6 @@ defmodule PlausibleWeb.Live.FunnelSettingsTest do
assert %Plausible.Funnel{funnel_type: :flexible} =
Plausible.Funnels.get(site, "Editable flexible funnel")
end
-
- test "funnel gets evaluated on every select, assuming a second has passed between selections",
- %{
- conn: conn,
- site: site
- } do
- setup_goals(site)
- lv = get_liveview(conn, site)
- lv |> element(~s/button[phx-click="add-funnel"]/) |> render_click()
-
- assert lv = find_live_child(lv, "funnels-form")
-
- lv |> element("li#dropdown-step-1-option-1 a") |> render_click()
-
- lv |> element("li#dropdown-step-2-option-1 a") |> render_click()
-
- doc = lv |> element("#step-eval-0") |> render()
- assert text_of_element(doc, ~s/#step-eval-0/) =~ "Visitors: 0"
-
- doc = lv |> element("#step-eval-1") |> render()
- assert text_of_element(doc, ~s/#step-eval-1/) =~ "Dropoff: 0%"
-
- doc = lv |> element("#funnel-eval") |> render()
- assert text_of_element(doc, ~s/#funnel-eval/) =~ "Last month conversion rate: 0%"
- end
end
defp setup_funnels(site, names \\ []) do
diff --git a/test/support/dev/controllers/e2e_controller.ex b/test/support/dev/controllers/e2e_controller.ex
index 0691da3874..311c291021 100644
--- a/test/support/dev/controllers/e2e_controller.ex
+++ b/test/support/dev/controllers/e2e_controller.ex
@@ -87,9 +87,13 @@ defmodule PlausibleWeb.E2EController do
site = Plausible.Repo.get_by!(Plausible.Site, domain: domain)
steps =
- Enum.map(steps, fn step ->
- goal = get_goal(site, step)
- %{"goal_id" => goal.id}
+ Enum.map(steps, fn
+ goal_name when is_binary(goal_name) ->
+ goal = get_goal(site, goal_name)
+ %{"goal_id" => goal.id}
+
+ goal_data when is_map(goal_data) ->
+ goal_data
end)
{:ok, _} = Plausible.Funnels.create(site, name, steps)
@@ -97,6 +101,38 @@ defmodule PlausibleWeb.E2EController do
send_resp(conn, 200, Jason.encode!(%{"ok" => true}))
end
+ def get_funnel(conn, %{"domain" => domain, "name" => name}) do
+ site = Plausible.Repo.get_by!(Plausible.Site, domain: domain)
+
+ funnel =
+ Plausible.Funnel
+ |> Plausible.Repo.get_by!(site_id: site.id, name: name)
+ |> Plausible.Repo.preload(steps: :goal)
+
+ steps = Plausible.Funnel.goals(funnel)
+
+ funnel_data =
+ funnel
+ |> Map.take([:id, :name, :funnel_type, :site_id])
+ |> Map.put(
+ :steps,
+ Enum.map(
+ steps,
+ &Map.take(&1, [
+ :id,
+ :event_name,
+ :page_path,
+ :scroll_threshold,
+ :display_name,
+ :currency,
+ :custom_props
+ ])
+ )
+ )
+
+ send_resp(conn, 200, Jason.encode!(funnel_data))
+ end
+
def put_verification_scenario(conn, %{"domain" => domain, "scenario" => scenario} = params) do
opts = [
slowdown: params["options"]["slowdown"] || 0,
diff --git a/test/support/factory.ex b/test/support/factory.ex
index 9b7a440077..fb1984b93c 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -178,19 +178,19 @@ defmodule Plausible.Factory do
attrs =
case {attrs, display_name_provided?} do
{%{page_path: path}, false} when is_binary(path) ->
- Map.put(attrs, :display_name, "Visit " <> path)
+ Map.put(attrs, :display_name, Plausible.Goal.display_name(nil, path))
{%{page_path: path}, false} when is_function(path, 0) ->
attrs
- |> Map.put(:display_name, "Visit " <> path.())
+ |> Map.put(:display_name, Plausible.Goal.display_name(nil, path.()))
|> Map.put(:page_path, path.())
{%{event_name: event_name}, false} when is_binary(event_name) ->
- Map.put(attrs, :display_name, event_name)
+ Map.put(attrs, :display_name, Plausible.Goal.display_name(event_name, nil))
{%{event_name: event_name}, false} when is_function(event_name, 0) ->
attrs
- |> Map.put(:display_name, event_name.())
+ |> Map.put(:display_name, Plausible.Goal.display_name(event_name.(), nil))
|> Map.put(:event_name, event_name.())
_ ->