Skip to content

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

Broadcast tracker script config updates (#5806)

Artur Pata0e0415fd6bf8
diff --git a/lib/plausible/cache.ex b/lib/plausible/cache.ex
index 8790d67399..c03c96b5fa 100644
--- a/lib/plausible/cache.ex
+++ b/lib/plausible/cache.ex
@@ -64,7 +64,7 @@ defmodule Plausible.Cache do
alias Plausible.Cache.Adapter
require Logger
- @spec broadcast_put(any(), Keyword.t()) :: :ok
+ @spec broadcast_put(any(), any(), Keyword.t()) :: :ok
def broadcast_put(key, value, opts \\ []) do
cache_name = Keyword.get(opts, :cache_name, name())
multicall_timeout = Keyword.get(opts, :multicall_timeout, :timer.seconds(5))
diff --git a/lib/plausible_web/tracker.ex b/lib/plausible_web/tracker.ex
index 12391ca617..6003613533 100644
--- a/lib/plausible_web/tracker.ex
+++ b/lib/plausible_web/tracker.ex
@@ -6,6 +6,7 @@ defmodule PlausibleWeb.Tracker do
use Plausible
use Plausible.Repo
alias Plausible.Site.TrackerScriptConfiguration
+ import Ecto.Query
path = Application.app_dir(:plausible, "priv/tracker/js/plausible-web.js")
# On CI, the file might not be present for static checks so we create an empty one
@@ -33,7 +34,9 @@ defmodule PlausibleWeb.Tracker do
end
# Exposed for testing
- def plausible_main_config(tracker_script_configuration) do
+ def plausible_main_config(
+ %TrackerScriptConfiguration{site: %{domain: _domain}} = tracker_script_configuration
+ ) do
%{
domain: tracker_script_configuration.site.domain,
endpoint: tracker_ingestion_endpoint(),
@@ -43,7 +46,9 @@ defmodule PlausibleWeb.Tracker do
}
end
- def build_script(%TrackerScriptConfiguration{} = tracker_script_configuration) do
+ def build_script(
+ %TrackerScriptConfiguration{site: %{domain: _domain}} = tracker_script_configuration
+ ) do
config_js_content =
tracker_script_configuration
|> plausible_main_config()
@@ -63,21 +68,32 @@ defmodule PlausibleWeb.Tracker do
def build_script(nil), do: nil
+ defp broadcast_script_upsert(tracker_script_configuration) do
+ PlausibleWeb.TrackerScriptCache.broadcast_put(
+ tracker_script_configuration.id,
+ PlausibleWeb.TrackerScriptCache.cache_content(tracker_script_configuration)
+ )
+ end
+
def update_script_configuration(site, config_update, changeset_type) do
Repo.transact(fn ->
with {:ok, original_config} <- get_or_create_tracker_script_configuration(site),
changeset <- changeset(original_config, config_update, changeset_type),
{:ok, updated_config} <-
- Repo.update(changeset) do
- sync_goals(site, original_config, updated_config)
+ Repo.update(changeset),
+ %TrackerScriptConfiguration{} = reloaded_config <-
+ maybe_reload_tracker_script_configuration(updated_config) do
+ sync_goals(site, original_config, reloaded_config)
on_ee do
if should_purge_cache?(changeset) do
- purge_cache!(updated_config.id)
+ purge_cache!(reloaded_config.id)
end
+ else
+ :ok = broadcast_script_upsert(reloaded_config)
end
- {:ok, updated_config}
+ {:ok, reloaded_config}
end
end)
end
@@ -127,9 +143,13 @@ defmodule PlausibleWeb.Tracker do
%TrackerScriptConfiguration{site_id: site.id},
params
)
- ) do
- sync_goals(site, %{}, created_config)
- {:ok, created_config}
+ ),
+ %TrackerScriptConfiguration{} = reloaded_config <-
+ maybe_reload_tracker_script_configuration(created_config) do
+ sync_goals(site, %{}, reloaded_config)
+
+ :ok = broadcast_script_upsert(reloaded_config)
+ {:ok, reloaded_config}
end
end)
end
@@ -154,17 +174,26 @@ defmodule PlausibleWeb.Tracker do
"manual"
end
- on_ee do
- import Ecto.Query
+ def get_tracker_script_configuration_base_query() do
+ from(t in TrackerScriptConfiguration,
+ join: s in assoc(t, :site),
+ select: %{t | site: %{domain: s.domain}}
+ )
+ end
- defp get_tracker_script_configuration_by_id(id) do
- from(t in TrackerScriptConfiguration,
- where: t.id == ^id,
- join: s in assoc(t, :site),
- preload: [site: s]
- )
- |> Plausible.Repo.one()
- end
+ def get_tracker_script_configuration_by_id(id) do
+ get_tracker_script_configuration_base_query()
+ |> where([t], t.id == ^id)
+ |> Plausible.Repo.one()
+ end
+
+ on_ee do
+ defp maybe_reload_tracker_script_configuration(tracker_script_configuration),
+ do: tracker_script_configuration
+ else
+ # This loads the necessary associations (:site), that aren't returned with inserts and updates
+ defp maybe_reload_tracker_script_configuration(tracker_script_configuration),
+ do: get_tracker_script_configuration_by_id(tracker_script_configuration.id)
end
# Sync plausible goals with the updated script config
diff --git a/lib/plausible_web/tracker_script_cache.ex b/lib/plausible_web/tracker_script_cache.ex
index 031aebbc57..89e894dd82 100644
--- a/lib/plausible_web/tracker_script_cache.ex
+++ b/lib/plausible_web/tracker_script_cache.ex
@@ -6,6 +6,7 @@ defmodule PlausibleWeb.TrackerScriptCache do
On EE instances, we cache valid tracker script ids to avoid database lookups.
"""
alias Plausible.Site.TrackerScriptConfiguration
+ alias PlausibleWeb.Tracker
import Ecto.Query
use Plausible
@@ -19,28 +20,29 @@ defmodule PlausibleWeb.TrackerScriptCache do
@impl true
def child_id(), do: :cache_tracker_script
+ on_ee do
+ @doc "Caches that the config exists"
+ def cache_content(%TrackerScriptConfiguration{} = _tracker_script_configuration), do: true
+ else
+ @doc "Caches the full tracker script"
+ def cache_content(
+ %TrackerScriptConfiguration{site: %{domain: _domain}} = tracker_script_configuration
+ ),
+ do: Tracker.build_script(tracker_script_configuration)
+ end
+
@impl true
def count_all() do
Plausible.Repo.aggregate(TrackerScriptConfiguration, :count)
end
@impl true
- def base_db_query() do
- from(
- t in TrackerScriptConfiguration,
- join: s in assoc(t, :site),
- preload: [site: s]
- )
- end
+ def base_db_query(), do: Tracker.get_tracker_script_configuration_base_query()
@impl true
def get_from_source(id) do
- query =
- base_db_query()
- |> where([t], t.id == ^id)
-
- case Plausible.Repo.one(query) do
- %TrackerScriptConfiguration{} = tracker_script_configuration ->
+ case Tracker.get_tracker_script_configuration_by_id(id) do
+ %TrackerScriptConfiguration{site: %{domain: _domain}} = tracker_script_configuration ->
cache_content(tracker_script_configuration)
_ ->
@@ -58,12 +60,4 @@ defmodule PlausibleWeb.TrackerScriptCache do
]
end)
end
-
- defp cache_content(tracker_script_configuration) do
- if ee?() do
- true
- else
- PlausibleWeb.Tracker.build_script(tracker_script_configuration)
- end
- end
end
diff --git a/test/plausible_web/tracker_script_cache_test.exs b/test/plausible_web/tracker_script_cache_test.exs
index 31f0c8714b..773a543191 100644
--- a/test/plausible_web/tracker_script_cache_test.exs
+++ b/test/plausible_web/tracker_script_cache_test.exs
@@ -29,7 +29,10 @@ defmodule PlausibleWeb.TrackerScriptCacheTest do
on_ee do
assert result == true
else
+ # it's the script
assert is_binary(result)
+ # the config has been expanded into the script template
+ assert result =~ ~r/domain:\"#{site.domain}\"/
end
refute TrackerScriptCache.get("nonexistent", cache_name: test, force?: true)