Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Return explicit journey end event (#6340)
Adrian Gruntkowski14e31c442611
diff --git a/assets/js/dashboard/extra/exploration.js b/assets/js/dashboard/extra/exploration.jsindex ee9f85b232..cc60f0dfc8 100644--- a/assets/js/dashboard/extra/exploration.js+++ b/assets/js/dashboard/extra/exploration.js@@ -22,6 +22,8 @@ import { ChevronUpDownIcon, ChevronRightIcon } from '@heroicons/react/20/solid'import { FlagIcon, MagnifyingGlassIcon } from '@heroicons/react/24/outline'import { popover } from '../components/popover'+const JOURNEY_END_EVENT = '__journey_end__'+const DIRECTION = { FORWARD: 'forward', BACKWARD: 'backward' }const DIRECTION_OPTIONS = [@@ -102,6 +104,19 @@ function stepsToJourneyParam(steps) {)}+function maybeEmptyResults(results, activeFilter) {+ if (+ results.length === 0 ||+ (!activeFilter &&+ results.length === 1 &&+ results[0].step.name === JOURNEY_END_EVENT)+ ) {+ return []+ } else {+ return results+ }+}+// Keep only entries with index < fromIndex, discarding everything at or after.// Used to truncate frozen candidate snapshots when the journey is shortened.function truncateFrozenAt(frozen, fromIndex) {@@ -354,7 +369,9 @@ function CandidateCard({colIndex,onSelect}) {- const isCustomEvent = step.name !== 'pageview'+ const isJourneyEnd = step.name === JOURNEY_END_EVENT+ const isCustomEvent =+ step.name !== 'pageview' && step.name !== JOURNEY_END_EVENTconst isGoal = step.is_goalconst visitorsToShow =@@ -382,12 +399,16 @@ function CandidateCard({? 'bg-gray-100/60 dark:bg-gray-850': 'hover:bg-gray-100/60 dark:hover:bg-gray-850'+ const pointer = isJourneyEnd ? 'pointer-events-none' : ''++ const onSelectHandler = isJourneyEnd ? () => {} : onSelect+return (<li><buttondata-exploration-step={isSelected ? colIndex : undefined}- className={`group relative w-full text-left text-sm rounded-sm overflow-hidden focus:outline-none ${rowBg}`}- onClick={() => onSelect(isSelected ? null : step)}+ className={`group relative w-full text-left text-sm rounded-sm overflow-hidden focus:outline-none ${rowBg} ${pointer}`}+ onClick={() => onSelectHandler(isSelected ? null : step)}><divclassName={`absolute top-0 left-0 h-full rounded-sm transition-[width] ease-in-out ${barBg}`}@@ -801,7 +822,10 @@ function useExplorationData(site, dashboardState, inViewport) {if (!isStale())setState((prev) => ({...prev,- activeResults: r?.next ?? [],+ activeResults: maybeEmptyResults(+ r?.next ?? [],+ prev.activeFilter+ ),rateLimited: false}))})@@ -846,7 +870,10 @@ function useExplorationData(site, dashboardState, inViewport) {if (!isStale())setState((prev) => ({...prev,- activeResults: r?.next ?? [],+ activeResults: maybeEmptyResults(+ r?.next ?? [],+ prev.activeFilter+ ),rateLimited: false}))})@@ -897,7 +924,10 @@ function useExplorationData(site, dashboardState, inViewport) {setState((prev) => {const next = {...prev,- activeResults: response?.next ?? [],+ activeResults: maybeEmptyResults(+ response?.next ?? [],+ prev.activeFilter+ ),rateLimited: false}if (includeFunnel) {diff --git a/extra/lib/plausible/stats/exploration.ex b/extra/lib/plausible/stats/exploration.exindex 667c999056..e1ea1b0c1b 100644--- a/extra/lib/plausible/stats/exploration.ex+++ b/extra/lib/plausible/stats/exploration.ex@@ -7,9 +7,9 @@ defmodule Plausible.Stats.Exploration doimport Plausible.Stats.SQL.Fragmentsimport Plausible.Stats.Util, only: [percentage: 2]- alias Plausible.Stats.Exploration.Journeyalias Plausible.ClickhouseRepoalias Plausible.Stats.Base+ alias Plausible.Stats.Exploration.Journeyalias Plausible.Stats.Filtersalias Plausible.Stats.Query@@ -143,7 +143,7 @@ defmodule Plausible.Stats.Exploration doquery,[],[],- MapSet.new(),+ MapSet.new([{Journey.Step.journey_end_event(), ""}]),max_steps,max_candidates,include_wildcard?@@ -241,7 +241,6 @@ defmodule Plausible.Stats.Exploration doq_matches =from(s in subquery(q_steps),- where: selected_as(:name) != "",select: %{user_id: s.user_id,name: selected_as(field(s, ^next_name), :name),@@ -286,8 +285,12 @@ defmodule Plausible.Stats.Exploration dofrom(m in subquery(q_all_combined_matches),select: %{step: %Journey.Step{- label: selected_as(m.label, :label),- name: m.name,+ label:+ selected_as(+ fragment("if(? != '', ?, ?)", m.name, m.label, ^Journey.Step.journey_end_label()),+ :label+ ),+ name: fragment("if(? != '', ?, ?)", m.name, m.name, ^Journey.Step.journey_end_event()),pathname: m.pathname,includes_subpaths: m.includes_subpaths,subpaths_count: m.subpaths_count,diff --git a/extra/lib/plausible/stats/journey/step.ex b/extra/lib/plausible/stats/journey/step.exindex 3ee3ede69e..6ac4ec4913 100644--- a/extra/lib/plausible/stats/journey/step.ex+++ b/extra/lib/plausible/stats/journey/step.ex@@ -12,6 +12,15 @@ defmodule Plausible.Stats.Exploration.Journey.Step dosubpaths_count: 0,is_goal: false+ @journey_end_event "__journey_end__"+ @journey_end_label "no further action"++ @spec journey_end_event() :: String.t()+ def journey_end_event, do: @journey_end_event++ @spec journey_end_label() :: String.t()+ def journey_end_label, do: @journey_end_label+@spec from(map()) :: t()def from(step) donew(step.name, step.pathname, step.includes_subpaths, step.subpaths_count, step.is_goal)@@ -21,10 +30,15 @@ defmodule Plausible.Stats.Exploration.Journey.Step dodef new(name, pathname, includes_subpaths \\ false, subpaths_count \\ 0, is_goal \\ false)when is_boolean(includes_subpaths) and is_integer(subpaths_count) dolabel =- if name != "pageview" do- name- else- pathname+ cond do+ name == @journey_end_event ->+ @journey_end_label++ name != "pageview" ->+ name++ true ->+ pathnameend%__MODULE__{diff --git a/lib/plausible/goal.ex b/lib/plausible/goal.exindex 51fb732b89..f5900aff9b 100644--- a/lib/plausible/goal.ex+++ b/lib/plausible/goal.ex@@ -1,10 +1,21 @@defmodule Plausible.Goal do+ @moduledoc """+ Goal schema.+ """+use Plausibleuse Ecto.Schema+import Ecto.Changeset@type t() :: %__MODULE__{}+ on_ee do+ @journey_end_event Plausible.Stats.Exploration.Journey.Step.journey_end_event()+ else+ @journey_end_event nil+ end+schema "goals" dofield :event_name, :stringfield :page_path, :string@@ -153,6 +164,10 @@ defmodule Plausible.Goal dovalue == "engagement" ->{:error, "The event name 'engagement' is reserved and cannot be used as a goal"}+ journey_end_event?(value) ->+ {:error,+ "The event name '#{@journey_end_event}' is reserved and cannot be used as a goal"}+value && String.match?(value, ~r/^.+/) ->:ok@@ -223,6 +238,14 @@ defmodule Plausible.Goal do[]endend++ on_ee do+ defp journey_end_event?(name) do+ name == @journey_end_event+ end+ else+ defp journey_end_event?(_name), do: always(false)+ endenddefimpl Jason.Encoder, for: Plausible.Goal dodiff --git a/test/plausible/goals_test.exs b/test/plausible/goals_test.exsindex ddbb5301a8..16ace3c2b7 100644--- a/test/plausible/goals_test.exs+++ b/test/plausible/goals_test.exs@@ -346,6 +346,17 @@ defmodule Plausible.GoalsTest doendon_ee do+ @journey_end_event Plausible.Stats.Exploration.Journey.Step.journey_end_event()++ test "create/2 fails to create a goal with '#{@journey_end_event}' as event_name (reserved)" do+ site = new_site()+ assert {:error, changeset} = Goals.create(site, %{"event_name" => @journey_end_event})++ assert {"The event name '#{@journey_end_event}' is reserved and cannot be used as a goal",+ _} =+ changeset.errors[:event_name]+ end+test "create/2 sets site.updated_at for revenue goal" dosite_1 = new_site(updated_at: DateTime.add(DateTime.utc_now(), -3600))diff --git a/test/plausible/stats/exploration_test.exs b/test/plausible/stats/exploration_test.exsindex 25c37e0b4b..cdac6652bf 100644--- a/test/plausible/stats/exploration_test.exs+++ b/test/plausible/stats/exploration_test.exs@@ -6,6 +6,9 @@ defmodule Plausible.Stats.ExplorationTest doalias Plausible.Stats.Explorationalias Plausible.Stats.QueryBuilder+ @journey_end_event Exploration.Journey.Step.journey_end_event()+ @journey_end_label Exploration.Journey.Step.journey_end_label()+setup dosite = new_site()@@ -572,6 +575,67 @@ defmodule Plausible.Stats.ExplorationTest doassert next_step.visitors == 1end+ test "allows to filter by journey end event label" do+ site = new_site()+ now = DateTime.utc_now()++ populate_stats(site, [+ build(:pageview,+ user_id: 123,+ pathname: "/home",+ timestamp: DateTime.shift(now, minute: -50)+ ),+ build(:pageview,+ user_id: 123,+ pathname: "/login",+ timestamp: DateTime.shift(now, minute: -40)+ ),+ build(:pageview,+ user_id: 124,+ pathname: "/home",+ timestamp: DateTime.shift(now, minute: -30)+ ),+ build(:pageview,+ user_id: 124,+ pathname: "/login",+ timestamp: DateTime.shift(now, minute: -20)+ ),+ build(:pageview,+ user_id: 124,+ pathname: "/dashboard",+ timestamp: DateTime.shift(now, minute: -10)+ ),+ build(:pageview,+ user_id: 125,+ pathname: "/home",+ timestamp: DateTime.shift(now, minute: -50)+ ),+ build(:pageview,+ user_id: 125,+ pathname: "/login",+ timestamp: DateTime.shift(now, minute: -40)+ ),+ build(:pageview,+ user_id: 125,+ pathname: "/dashboard",+ timestamp: DateTime.shift(now, minute: -30)+ )+ ])++ query = QueryBuilder.build!(site, input_date_range: :all)++ journey = [+ %Exploration.Journey.Step{name: "pageview", pathname: "/home"},+ %Exploration.Journey.Step{name: "pageview", pathname: "/login"}+ ]++ assert {:ok, [next_step]} =+ Exploration.next_steps(site, query, journey, search_term: "no further")++ assert next_step.step.label == "no further action"+ assert next_step.visitors == 1+ end+test "includes root path (/) in suggestions" dosite = new_site()@@ -661,6 +725,40 @@ defmodule Plausible.Stats.ExplorationTest doassert next_step2.visitors == 1end+ test "there can be multiple journey suggestions for a single user/session" do+ site = new_site()++ now = DateTime.utc_now()++ ago = fn ms -> DateTime.shift(now, minute: -1 * ms) end++ populate_stats(site, [+ build(:pageview, user_id: 123, pathname: "/home", timestamp: ago.(100)),+ build(:pageview, user_id: 123, pathname: "/login", timestamp: ago.(99)),+ build(:pageview, user_id: 123, pathname: "/dashboard", timestamp: ago.(98)),+ build(:pageview, user_id: 123, pathname: "/home", timestamp: ago.(97)),+ build(:pageview, user_id: 123, pathname: "/login", timestamp: ago.(96)),+ build(:pageview, user_id: 123, pathname: "/sites", timestamp: ago.(95))+ ])++ journey = [+ %Exploration.Journey.Step{name: "pageview", pathname: "/home"},+ %Exploration.Journey.Step{name: "pageview", pathname: "/login"}+ ]++ query = QueryBuilder.build!(site, input_date_range: :all)++ assert {:ok,+ [+ %{step: %{pathname: "/dashboard"}, visitors: 1},+ %{step: %{pathname: "/sites"}, visitors: 1}+ ]} =+ Exploration.next_steps(site, query, journey,+ search_term: "",+ direction: :forward+ )+ end+test "does not suggest the same path/pathname as in previous step (regression test)" dosite = new_site()@@ -720,6 +818,7 @@ defmodule Plausible.Stats.ExplorationTest doassert {:ok,[+ %{step: %{name: @journey_end_event}},%{step: %{pathname: "/:dashboard"}}]} =Exploration.next_steps(site, query, journey,@@ -1166,9 +1265,12 @@ defmodule Plausible.Stats.ExplorationTest do}]- assert {:ok, [next_step]} = Exploration.next_steps(site, query, journey)+ assert {:ok, [next_step1, next_step2]} = Exploration.next_steps(site, query, journey)- assert next_step.step.label == "/a-blog"+ assert next_step1.step.label == @journey_end_label+ assert next_step1.visitors == 2+ assert next_step2.step.label == "/a-blog"+ assert next_step2.visitors == 1endtest "suggestions matching goal pattern from previous step are excluded" do@@ -1231,9 +1333,12 @@ defmodule Plausible.Stats.ExplorationTest do}]- assert {:ok, [next_step]} = Exploration.next_steps(site, query, journey)+ assert {:ok, [next_step1, next_step2]} = Exploration.next_steps(site, query, journey)- assert next_step.step.label == "/blog"+ assert next_step1.step.label == @journey_end_label+ assert next_step1.visitors == 3+ assert next_step2.step.label == "/blog"+ assert next_step2.visitors == 1endend