You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible/goals.ex

249 lines
6.6 KiB
Elixir

defmodule Plausible.Goals do
use Plausible
use Plausible.Repo
use Plausible.Funnel.Const
import Ecto.Query
alias Plausible.Goal
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
alias Ecto.Multi
@spec create(Plausible.Site.t(), map(), Keyword.t()) ::
{:ok, Goal.t()} | {:error, Ecto.Changeset.t()} | {:error, :upgrade_required}
@doc """
Creates a Goal for a site.
If the created goal is a revenue goal, it sets site.updated_at to be
refreshed by the sites cache, as revenue goals are used during ingestion.
"""
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
def create(site, params, opts \\ []) do
upsert? = Keyword.get(opts, :upsert?, false)
Repo.transaction(fn ->
case insert_goal(site, params, upsert?) do
{:ok, :insert, goal} ->
on_full_build do
now = Keyword.get(opts, :now, DateTime.utc_now())
# credo:disable-for-next-line Credo.Check.Refactor.Nesting
if Plausible.Goal.Revenue.revenue?(goal) do
Plausible.Site.Cache.touch_site!(site, now)
end
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
end
Repo.preload(goal, :site)
{:ok, :upsert, goal} ->
Repo.preload(goal, :site)
{:error, cause} ->
Repo.rollback(cause)
end
end)
end
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
def find_or_create(site, %{
"goal_type" => "event",
"event_name" => event_name,
"currency" => currency
})
when is_binary(event_name) and is_binary(currency) do
params = %{"event_name" => event_name, "currency" => currency}
with {:ok, goal} <- create(site, params, upsert?: true) do
if to_string(goal.currency) == currency do
{:ok, goal}
else
# we must disallow creation of the same goal name with different currency
changeset =
goal
|> Goal.changeset()
|> Ecto.Changeset.add_error(
:event_name,
"'#{goal.event_name}' (with currency: #{goal.currency}) has already been taken"
)
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
{:error, changeset}
end
end
end
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
def find_or_create(site, %{"goal_type" => "event", "event_name" => event_name})
when is_binary(event_name) do
create(site, %{"event_name" => event_name}, upsert?: true)
end
def find_or_create(_, %{"goal_type" => "event"}), do: {:missing, "event_name"}
def find_or_create(site, %{"goal_type" => "page", "page_path" => page_path}) do
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
create(site, %{"page_path" => page_path}, upsert?: true)
end
def find_or_create(_, %{"goal_type" => "page"}), do: {:missing, "page_path"}
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
def for_site(site, opts \\ []) do
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
site
|> for_site_query(opts)
|> Repo.all()
|> Enum.map(&maybe_trim/1)
end
def for_site_query(site, opts \\ []) do
query =
from g in Goal,
inner_join: assoc(g, :site),
where: g.site_id == ^site.id,
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
order_by: [desc: g.id],
preload: [:site]
if opts[:preload_funnels?] == true and full_build?() do
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
from(g in query,
left_join: assoc(g, :funnels),
group_by: g.id,
preload: [:funnels]
)
else
query
end
end
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
@doc """
If a goal belongs to funnel(s), we need to inspect their number of steps.
If it exceeds the minimum allowed (defined via `Plausible.Funnel.min_steps/0`),
the funnel will be reduced (i.e. a step associated with the goal to be deleted
is removed), so that the minimum number of steps is preserved. This is done
implicitly, by postgres, as per on_delete: :delete_all.
Otherwise, for associated funnel(s) consisting of minimum number steps only,
funnel record(s) are removed completely along with the targeted goal.
"""
Improve goal settings UX (#3293) * Add Heroicons dependency * Add name_of/1 html helper Currently with Floki there's no way to query for `[name=foo[some]]` selector * Update changelog * Make goal deletion possible with only goal id * Remove stale goal controllers * Improve ComboBox component - make sure the list options are always of the parent input width - allow passing a suggestion function instead of a module * Stale fixup * Update routes * Use the new goals route in funnel settings * Use a function in the funnel combo * Use function in the props combo * Remove old goals form * Implement new goal settings * Update moduledoc * Fix revenue switch in dark mode * Connect live socket on goal settings page * Fixup * Use Heroicons.trash icon * Tweak goals search input * Remove unused alias * Fix search/button alignment * Fix backspace icon alignment * Delegate :superadmin check to get_for_user/3 I'll do props settings separately, it's work in progress in a branch on top of this one already. cc @ukutaht * Rename socket assigns * Fixup to 5c9f58e * Fixup * Render ComboBox suggestions asynchronously This commit: - prevents redundant work by checking the socket connection - allows passing no options to the ComboBox component, so that when combined with the `async` option, the options are asynchronously initialized post-render - allows updating the suggestions asynchronously with the `async` option set to `true` - helpful in case of DB queries used for suggestions * Update tests * Throttle comboboxes * Update tests * Dim the search input * Use debounce=200 in ComboBox component * Move creatable option to the top * Ensure there's always a leading slash for goals * Test pageview goals with leading / missing * Make the modal scrollable on small viewports
3 years ago
def delete(id, %Plausible.Site{id: site_id}) do
delete(id, site_id)
end
def delete(id, site_id) do
goal_query =
from(g in Goal,
where: g.id == ^id,
where: g.site_id == ^site_id
)
goal_query = on_full_build(do: preload(goal_query, funnels: :steps), else: goal_query)
result =
Multi.new()
|> Multi.one(
:goal,
goal_query
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
)
|> Multi.run(:funnel_ids_to_wipe, fn
_, %{goal: nil} ->
{:error, :not_found}
_, %{goal: %{funnels: []}} ->
{:ok, []}
_, %{goal: %{funnels: funnels}} ->
funnels_to_wipe =
funnels
|> Enum.filter(&(Enum.count(&1.steps) == Funnel.Const.min_steps()))
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
|> Enum.map(& &1.id)
{:ok, funnels_to_wipe}
end)
|> Multi.merge(fn
%{funnel_ids_to_wipe: []} ->
Ecto.Multi.new()
%{funnel_ids_to_wipe: [_ | _] = funnel_ids} ->
Ecto.Multi.new()
|> Multi.delete_all(
:delete_funnels,
from(f in "funnels",
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
where: f.id in ^funnel_ids
)
)
end)
|> Multi.delete_all(
:delete_goals,
fn _ ->
Funnel site settings (#3039) * Update formatter config * Install LiveView JS integration & hooks * Temporarily update endpoint/session config * Optionally allow preloading funnels for goals * Site controller * Implement funnel settings lib/plausible_web/live/funnel_settings/combo_box.ex - restored from: 054de6e2 Fix the tab/blur bug again 20da4c89 Rename InputPicker to ComboBox lib/plausible_web/live/funnel_settings/form.ex - restored from: 9bedda3b Remove potential FIXME 20da4c89 Rename InputPicker to ComboBox 028036ad Review comments aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 0dde27fd Remove inspect call eed588a7 Start testing the funnel editor 0e95228b Extract funnel settings test module 7b16ace5 Leverage aplinejs to deal with the tyranny 8dc6a3e7 wip cf228630 wip 30a43fd1 wip 89f10ecb wip 950a18d9 Dirty funnel save 298a6a53 wip 7690d50f wip 639c6238 fixup aa59adeb wip ff75c00b wip lib/plausible_web/live/funnel_settings/list.ex - restored from: 4eae122c Fix data-confirm attr interpolation 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list 298a6a53 wip ff75c00b wip test/plausible_web/live/funnel_settings/funnel_settings/combo_box_test.exs - restored from: 20da4c89 Rename InputPicker to ComboBox test/plausible_web/live/funnel_settings/funnel_settings_test.exs - restored from: 34822ff4 Bootstrap InputPicker tests lib/plausible_web/live/funnel_settings.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 0e95228b Extract funnel settings test module 8dc6a3e7 wip 89f10ecb wip 950a18d9 Dirty funnel save 298a6a53 wip aa59adeb wip ff75c00b wip test/plausible_web/controllers/error_report_controller_test.exs - restored from: 34822ff4 Bootstrap InputPicker tests test/support/html.ex - restored from: 0a53979d Improve InputPicker tests - include AlpineJS assertions 34822ff4 Bootstrap InputPicker tests lib/plausible_web/views/layout_view.ex - restored from: b490403b !ifxup lib/plausible_web/templates/site/settings_funnels.html.eex - restored from: 51f0397d Implement deleting funnels ea1315f3 Test funnels list in settings 7b16ace5 Leverage aplinejs to deal with the tyranny ff75c00b wip 4da25c35 Fixup lib/plausible_web/templates/layout/app.html.eex - restored from: ff75c00b wip * Add funnel settings route * Warn about funnels deletion when deleting goals lib/plausible_web/templates/site/settings_goals.html.eex - restored from: fdd9bcd0 Fixup f1e6364d Merge remote-tracking branch 'origin/master' into funnels-rebase 9d0b7c6d Fix markup error 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 639c6238 fixup aa59adeb wip * Split new JS LiveView additions * Put funnels behind a feature flag * Integrate dashboard feature toggle * Update signing salt for live view * Update moduledocs * Update live reloader config * Use Phoenix.HTML.Safe for goal names * Workaround to get flashes working in embedded liveview * Keep feature toggles idempotent, rename property to setting We'll still retain the ability to flip bools on a lower level. * Update moduledocs * Make live flash disappear after 5s * Tailwind: purge .heex files too * Update docs link * Add live components to tailwind purge config * Update another flaky test Ref f0bdf872 cc @vinibrsl * Fix combobox input length w/ WebKit * Intoduce generic notice component * Revert "Fix combobox input length w/ WebKit" This reverts commit 3c653a6d85d5000167631e10ef45a93c13b41ed1. * Fix combobox input length on webkit * Make whole combobox item clickable, not only text * Fix glitch moving Save button on activation * Tweak dark mode * Show funnel form without waiting for funnel name input * Tweak dark mode * Include static Phoenix components in tailwind purge * Tune funnels form into a liveview of its own This is so that ComboBoxes can publish their selections and unavailable choices can be propagated to other siblings. * Push less data over websocket * Undo Lsp/formatter race condition * Fixup typespecs * Bust CI cache
3 years ago
from g in Goal,
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
where: g.id == ^id,
Improve goal settings UX (#3293) * Add Heroicons dependency * Add name_of/1 html helper Currently with Floki there's no way to query for `[name=foo[some]]` selector * Update changelog * Make goal deletion possible with only goal id * Remove stale goal controllers * Improve ComboBox component - make sure the list options are always of the parent input width - allow passing a suggestion function instead of a module * Stale fixup * Update routes * Use the new goals route in funnel settings * Use a function in the funnel combo * Use function in the props combo * Remove old goals form * Implement new goal settings * Update moduledoc * Fix revenue switch in dark mode * Connect live socket on goal settings page * Fixup * Use Heroicons.trash icon * Tweak goals search input * Remove unused alias * Fix search/button alignment * Fix backspace icon alignment * Delegate :superadmin check to get_for_user/3 I'll do props settings separately, it's work in progress in a branch on top of this one already. cc @ukutaht * Rename socket assigns * Fixup to 5c9f58e * Fixup * Render ComboBox suggestions asynchronously This commit: - prevents redundant work by checking the socket connection - allows passing no options to the ComboBox component, so that when combined with the `async` option, the options are asynchronously initialized post-render - allows updating the suggestions asynchronously with the `async` option set to `true` - helpful in case of DB queries used for suggestions * Update tests * Throttle comboboxes * Update tests * Dim the search input * Use debounce=200 in ComboBox component * Move creatable option to the top * Ensure there's always a leading slash for goals * Test pageview goals with leading / missing * Make the modal scrollable on small viewports
3 years ago
where: g.site_id == ^site_id
Implement backend core for funnels (#3023) * Implement Funnel schema * Implement Funnels context module lib/plausible/funnels.ex - restored from: dca93621 Mark Vini's suggestion applied 7d1eec75 Update lib/plausible/funnels.ex 028036ad Review comments b694ccd5 Express the guard nicely acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 51f0397d Implement deleting funnels cf228630 wip 1f6fe25d Add number of steps to funnels list 89f10ecb wip 950a18d9 Dirty funnel save e62bfce5 Refactor funnel result evaluation 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 7a88fe44 Outline basic error handling c8ae3eaf Move Funnels to StatsController and use base query 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip fbd14e23 Absolute dirty wip 3ddd15ad wip 34ceb3f5 dirty wip test/plausible_web/controllers/api/stats_controller/funnels_test.exs - restored from: 9b532273 Test funnel stats controller Co-authored-by: Uku Taht <uku.taht@gmail.com> * Update Goals implementation lib/plausible/goal/schema.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion fbd14e23 Absolute dirty wip lib/plausible/goals.ex - restored from: aea4ebc4 Access Funnel min/max steps via the __using__/1 macro a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 950a18d9 Dirty funnel save f06c3fcf wip test/plausible/goals_test.exs - restored from: a418c039 Implement funnel reduction logic on goal deletion 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion dee5bc9e Fixup tests * Update seeds * Split funnel schema modules * Add moduledoc to Funnel schema * Update Funnel.Step moduledoc * Add typespecs to Plausible.Funnels * Implement Phoenix.HTML.Safe for Funnel schema * Fixup a test after ordering goals by desc:id test/plausible_web/controllers/api/stats_controller/conversions_test.exs - restored from: 26a493cb Fixup a test after ordering goals by desc:id * Annotate Goal schema with type t() * Reword schema module docs * Fix up typespecs * Stop tagging Funnels.get/2 result * Nuke Goals.by_id!/2 * Test Funnels module test/plausible/funnels_test.exs - restored from: b7a6d3ad ya, rly 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list dee5bc9e Fixup tests 950a18d9 Dirty funnel save 88e38a33 Update funnel tests bf47f654 Calculate conversion rate/dropoff in the backend 9e62ffb1 Make existing tests pass 2929bfd4 Minor rework b490403b !ifxup ae44db77 Add Funnels.list/1 function 4f3216b3 Unique steps 6e0c3efc Ensure step ordering works cea93ba1 It works I think c50ac0c2 WIP a466ec4c Dirty wip 34ceb3f5 dirty wip Co-authored-by: Uku Taht <uku.taht@gmail.com> * Apply changes related to 242081e5 * Evaluate funnels through `Stats.funnel/3` entry point --------- Co-authored-by: Uku Taht <uku.taht@gmail.com>
3 years ago
end
)
|> Repo.transaction()
case result do
{:ok, _} -> :ok
{:error, _step, reason, _context} -> {:error, reason}
end
end
Funnel site settings (#3039) * Update formatter config * Install LiveView JS integration & hooks * Temporarily update endpoint/session config * Optionally allow preloading funnels for goals * Site controller * Implement funnel settings lib/plausible_web/live/funnel_settings/combo_box.ex - restored from: 054de6e2 Fix the tab/blur bug again 20da4c89 Rename InputPicker to ComboBox lib/plausible_web/live/funnel_settings/form.ex - restored from: 9bedda3b Remove potential FIXME 20da4c89 Rename InputPicker to ComboBox 028036ad Review comments aea4ebc4 Access Funnel min/max steps via the __using__/1 macro 0dde27fd Remove inspect call eed588a7 Start testing the funnel editor 0e95228b Extract funnel settings test module 7b16ace5 Leverage aplinejs to deal with the tyranny 8dc6a3e7 wip cf228630 wip 30a43fd1 wip 89f10ecb wip 950a18d9 Dirty funnel save 298a6a53 wip 7690d50f wip 639c6238 fixup aa59adeb wip ff75c00b wip lib/plausible_web/live/funnel_settings/list.ex - restored from: 4eae122c Fix data-confirm attr interpolation 51f0397d Implement deleting funnels 1f6fe25d Add number of steps to funnels list 298a6a53 wip ff75c00b wip test/plausible_web/live/funnel_settings/funnel_settings/combo_box_test.exs - restored from: 20da4c89 Rename InputPicker to ComboBox test/plausible_web/live/funnel_settings/funnel_settings_test.exs - restored from: 34822ff4 Bootstrap InputPicker tests lib/plausible_web/live/funnel_settings.ex - restored from: 028036ad Review comments acd9c4f2 Prepare ephemeral funnel definitions so that users can test funnels 51f0397d Implement deleting funnels 0e95228b Extract funnel settings test module 8dc6a3e7 wip 89f10ecb wip 950a18d9 Dirty funnel save 298a6a53 wip aa59adeb wip ff75c00b wip test/plausible_web/controllers/error_report_controller_test.exs - restored from: 34822ff4 Bootstrap InputPicker tests test/support/html.ex - restored from: 0a53979d Improve InputPicker tests - include AlpineJS assertions 34822ff4 Bootstrap InputPicker tests lib/plausible_web/views/layout_view.ex - restored from: b490403b !ifxup lib/plausible_web/templates/site/settings_funnels.html.eex - restored from: 51f0397d Implement deleting funnels ea1315f3 Test funnels list in settings 7b16ace5 Leverage aplinejs to deal with the tyranny ff75c00b wip 4da25c35 Fixup lib/plausible_web/templates/layout/app.html.eex - restored from: ff75c00b wip * Add funnel settings route * Warn about funnels deletion when deleting goals lib/plausible_web/templates/site/settings_goals.html.eex - restored from: fdd9bcd0 Fixup f1e6364d Merge remote-tracking branch 'origin/master' into funnels-rebase 9d0b7c6d Fix markup error 4a4ddbdc Optionally preload funnels for goals and stub funnel-goal deletion ebdc4333 Extend the prompt in case of funnel-goal deletion 639c6238 fixup aa59adeb wip * Split new JS LiveView additions * Put funnels behind a feature flag * Integrate dashboard feature toggle * Update signing salt for live view * Update moduledocs * Update live reloader config * Use Phoenix.HTML.Safe for goal names * Workaround to get flashes working in embedded liveview * Keep feature toggles idempotent, rename property to setting We'll still retain the ability to flip bools on a lower level. * Update moduledocs * Make live flash disappear after 5s * Tailwind: purge .heex files too * Update docs link * Add live components to tailwind purge config * Update another flaky test Ref f0bdf872 cc @vinibrsl * Fix combobox input length w/ WebKit * Intoduce generic notice component * Revert "Fix combobox input length w/ WebKit" This reverts commit 3c653a6d85d5000167631e10ef45a93c13b41ed1. * Fix combobox input length on webkit * Make whole combobox item clickable, not only text * Fix glitch moving Save button on activation * Tweak dark mode * Show funnel form without waiting for funnel name input * Tweak dark mode * Include static Phoenix components in tailwind purge * Tune funnels form into a liveview of its own This is so that ComboBoxes can publish their selections and unavailable choices can be propagated to other siblings. * Push less data over websocket * Undo Lsp/formatter race condition * Fixup typespecs * Bust CI cache
3 years ago
@spec count(Plausible.Site.t()) :: non_neg_integer()
def count(site) do
Repo.aggregate(
from(
g in Goal,
where: g.site_id == ^site.id
),
:count
)
end
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
defp insert_goal(site, params, upsert?) do
params = Map.delete(params, "site_id")
insert_opts =
if upsert? do
[on_conflict: :nothing]
else
[]
end
changeset = Goal.changeset(%Goal{site_id: site.id}, params)
with :ok <- maybe_check_feature_access(site, changeset),
{:ok, goal} <- Repo.insert(changeset, insert_opts) do
Plugins API: 2nd pass with Goals resource + SharedLinks schema changes (#3396) * Remove "Context" namespace level * Change Goal string representation * Alias Schemas in Plugin API Test Case template * Update schema & tests for SharedLink resource * Update Goals interface - make it possible to create revenue goals - extract "for site" query to a standalone function * Fixup typespecs * Alias Errors module in OpenAPI controllers * Add missing goals test * Implement Goals Plugins API resource * Add extra test to confirm changeset error propagation * Mute credo * Fix typos * Handle changeset traversal in `Errors` * Use upserts in `Goals.find_or_create` * Extract touch_site! to Site.Cache, address credo, improve code docs * Apply formatting * Remove unused inner join * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update test/plausible_web/plugins/api/controllers/goals_test.exs Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Update error message on revenue goal currency clash * Remove unused code --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>
3 years ago
# Upsert with `on_conflict: :nothing` strategy
# will result in goal struct missing primary key field
# which is generated by the database.
if goal.id do
{:ok, :insert, goal}
else
get_params =
goal
|> Map.take([:site_id, :event_name, :page_path])
|> Enum.reject(fn {_, value} -> is_nil(value) end)
{:ok, :upsert, Repo.get_by!(Goal, get_params)}
end
end
end
defp maybe_check_feature_access(site, changeset) do
if Ecto.Changeset.get_field(changeset, :currency) do
site = Plausible.Repo.preload(site, :owner)
Plausible.Billing.Feature.RevenueGoals.check_availability(site.owner)
else
:ok
end
end
defp maybe_trim(%Goal{} = goal) do
# we make sure that even if we saved goals erroneously with trailing
# space, it's removed during fetch
goal
|> Map.update!(:event_name, &maybe_trim/1)
|> Map.update!(:page_path, &maybe_trim/1)
end
defp maybe_trim(s) when is_binary(s) do
String.trim(s)
end
defp maybe_trim(other) do
other
end
end