Skip to content

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

Script v2: Include prefix "pa-" in tracker_script_config.id (#5621)

Artur Pata70eb552132f1
diff --git a/lib/plausible/data_migration/prefix_tracker_script_configuration_id.ex b/lib/plausible/data_migration/prefix_tracker_script_configuration_id.ex
new file mode 100644
index 0000000000..9932c2d80e
--- /dev/null
+++ b/lib/plausible/data_migration/prefix_tracker_script_configuration_id.ex
@@ -0,0 +1,96 @@
+defmodule Plausible.DataMigration.PrefixTrackerScriptConfigurationId do
+ @moduledoc """
+ Migration to update tracker script configuration IDs to use the new prefixed format.
+
+ This migration:
+ 1. Processes tracker configurations in batches to avoid memory issues
+ 2. Uses batch updates for better performance
+ 3. Handles the case where tracker configurations might not exist for all sites
+ 4. Provides progress logging
+ """
+
+ import Ecto.Query
+ alias Plausible.Repo
+ alias Plausible.Site.TrackerScriptConfiguration
+
+ def run(batch_size \\ 100) do
+ total_configs = count_total_configurations()
+ IO.puts("Found #{total_configs} total tracker configurations to process")
+
+ process_batch(nil, total_configs, batch_size, 0)
+ end
+
+ defp count_total_configurations do
+ Repo.aggregate(
+ from(config in TrackerScriptConfiguration,
+ where: not like(config.id, "pa-%")
+ ),
+ :count,
+ :id
+ )
+ end
+
+ defp process_batch(last_id, total_configs, batch_size, processed_count) do
+ configs = get_configurations_batch(last_id, batch_size)
+
+ if length(configs) > 0 do
+ batch_num = div(processed_count, batch_size) + 1
+ start_pos = processed_count + 1
+ end_pos = processed_count + length(configs)
+
+ IO.puts("Processing batch #{batch_num} (#{start_pos}-#{end_pos} of #{total_configs})")
+
+ process_configurations_batch(configs, batch_num)
+
+ process_batch(
+ List.last(configs).id,
+ total_configs,
+ batch_size,
+ processed_count + length(configs)
+ )
+ else
+ IO.puts("Migration completed!")
+ end
+ end
+
+ defp get_configurations_batch(last_id, batch_size) do
+ query =
+ from(config in TrackerScriptConfiguration,
+ where: not like(config.id, "pa-%"),
+ order_by: [asc: config.id],
+ limit: ^batch_size,
+ select: %{
+ id: config.id
+ }
+ )
+
+ query =
+ if last_id do
+ from(config in query, where: config.id > ^last_id)
+ else
+ query
+ end
+
+ Repo.all(query)
+ end
+
+ defp process_configurations_batch(configs, batch_num) do
+ config_ids = Enum.map(configs, & &1.id)
+
+ case Repo.query(
+ "UPDATE tracker_script_configuration SET id = 'pa-' || id WHERE id = ANY($1)",
+ [config_ids]
+ ) do
+ {:ok, %{num_rows: updated_count}} ->
+ if updated_count != length(configs) do
+ IO.puts(
+ "Warning: Batch #{batch_num} - Expected to update #{length(configs)} configurations, but updated #{updated_count}"
+ )
+ end
+
+ {:error, error} ->
+ IO.puts("Error updating batch #{batch_num}: #{inspect(error)}")
+ # Continue with next batch instead of crashing
+ end
+ end
+end
diff --git a/lib/plausible/ecto/types/nanoid.ex b/lib/plausible/ecto/types/nanoid.ex
index 97a0c139ec..9197f5aeec 100644
--- a/lib/plausible/ecto/types/nanoid.ex
+++ b/lib/plausible/ecto/types/nanoid.ex
@@ -1,6 +1,6 @@
-defmodule Plausible.Ecto.Types.Nanoid do
+defmodule Plausible.Ecto.Types.NanoidBase do
@moduledoc """
- Custom column type for nanoid strings
+ Base module for nanoid string types
"""
use Ecto.Type
@@ -15,6 +15,37 @@ defmodule Plausible.Ecto.Types.Nanoid do
def dump(value) when is_binary(value), do: {:ok, value}
def dump(_), do: :error
+ @callback autogenerate() :: String.t()
+end
+
+defmodule Plausible.Ecto.Types.Nanoid do
+ @moduledoc """
+ Custom column type for nanoid strings
+ """
+
+ @behaviour Plausible.Ecto.Types.NanoidBase
+
+ def type(), do: Plausible.Ecto.Types.NanoidBase.type()
+ def cast(value), do: Plausible.Ecto.Types.NanoidBase.cast(value)
+ def load(value), do: Plausible.Ecto.Types.NanoidBase.load(value)
+ def dump(value), do: Plausible.Ecto.Types.NanoidBase.dump(value)
+
@spec autogenerate() :: String.t()
def autogenerate(), do: Nanoid.generate()
end
+
+defmodule Plausible.Ecto.Types.TrackerScriptNanoid do
+ @moduledoc """
+ Custom column type for tracker script configuration nanoid strings with pa- prefix
+ """
+
+ @behaviour Plausible.Ecto.Types.NanoidBase
+
+ def type(), do: Plausible.Ecto.Types.NanoidBase.type()
+ def cast(value), do: Plausible.Ecto.Types.NanoidBase.cast(value)
+ def load(value), do: Plausible.Ecto.Types.NanoidBase.load(value)
+ def dump(value), do: Plausible.Ecto.Types.NanoidBase.dump(value)
+
+ @spec autogenerate() :: String.t()
+ def autogenerate(), do: "pa-#{Nanoid.generate()}"
+end
diff --git a/lib/plausible/site/tracker_script_configuration.ex b/lib/plausible/site/tracker_script_configuration.ex
index 5571b5356c..6550a2e485 100644
--- a/lib/plausible/site/tracker_script_configuration.ex
+++ b/lib/plausible/site/tracker_script_configuration.ex
@@ -20,7 +20,7 @@ defmodule Plausible.Site.TrackerScriptConfiguration do
:pageview_props
]}
- @primary_key {:id, Plausible.Ecto.Types.Nanoid, autogenerate: true}
+ @primary_key {:id, Plausible.Ecto.Types.TrackerScriptNanoid, autogenerate: true}
schema "tracker_script_configuration" do
field :installation_type, Ecto.Enum, values: [:manual, :wordpress, :gtm, nil]
diff --git a/lib/plausible_web/live/installationv2/instructions.ex b/lib/plausible_web/live/installationv2/instructions.ex
index bc261742fa..8b4d602e0d 100644
--- a/lib/plausible_web/live/installationv2/instructions.ex
+++ b/lib/plausible_web/live/installationv2/instructions.ex
@@ -245,6 +245,10 @@ defmodule PlausibleWeb.Live.InstallationV2.Instructions do
end
defp tracker_url(tracker_script_configuration) do
- "https://plausible.io/js/s-#{tracker_script_configuration.id}.js"
+ if String.starts_with?(tracker_script_configuration.id, "pa-") do
+ "https://plausible.io/js/#{tracker_script_configuration.id}.js"
+ else
+ "https://plausible.io/js/s-#{tracker_script_configuration.id}.js"
+ end
end
end
diff --git a/lib/plausible_web/plugs/tracker_plug.ex b/lib/plausible_web/plugs/tracker_plug.ex
index 939a17a019..08285169e5 100644
--- a/lib/plausible_web/plugs/tracker_plug.ex
+++ b/lib/plausible_web/plugs/tracker_plug.ex
@@ -42,8 +42,16 @@ defmodule PlausibleWeb.TrackerPlug do
case conn.request_path do
"/js/s-" <> path ->
if String.ends_with?(path, ".js") do
- tag = String.replace_trailing(path, ".js", "")
- request_tracker_script(tag, conn)
+ id = String.replace_trailing(path, ".js", "")
+ request_tracker_script(id, conn)
+ else
+ conn
+ end
+
+ "/js/pa-" <> path ->
+ if String.ends_with?(path, ".js") do
+ id = String.replace_trailing(path, ".js", "")
+ request_tracker_script("pa-" <> id, conn)
else
conn
end
@@ -61,47 +69,61 @@ defmodule PlausibleWeb.TrackerPlug do
def telemetry_event(name), do: [:plausible, :tracker_script, :request, name]
- defp request_tracker_script(tag, conn) do
- script_tag = get_plausible_web_script_tag(tag)
-
- if script_tag do
- :telemetry.execute(
- telemetry_event(:v2),
- %{},
- %{status: 200}
- )
+ # at this point, id might be both with and without the `pa-` prefix
+ defp request_tracker_script(id, conn) do
+ case get_plausible_web_script(id) do
+ {:ok, script_tag} ->
+ :telemetry.execute(
+ telemetry_event(:v2),
+ %{},
+ %{status: 200}
+ )
- conn
- |> put_resp_header("content-type", "application/javascript")
- |> put_resp_header("x-content-type-options", "nosniff")
- |> put_resp_header("cross-origin-resource-policy", "cross-origin")
- |> put_resp_header("access-control-allow-origin", "*")
- |> put_resp_header("cache-control", "public, max-age=60, no-transform")
- # CDN-Tag is used by BunnyCDN to tag cached resources. This allows us to purge
- # specific tracker scripts from the CDN cache.
- |> put_resp_header("cdn-tag", "tracker_script::#{tag}")
- |> send_resp(200, script_tag)
- |> halt()
- else
- :telemetry.execute(
- telemetry_event(:v2),
- %{},
- %{status: 404}
- )
+ conn
+ |> put_resp_header("content-type", "application/javascript")
+ |> put_resp_header("x-content-type-options", "nosniff")
+ |> put_resp_header("cross-origin-resource-policy", "cross-origin")
+ |> put_resp_header("access-control-allow-origin", "*")
+ |> put_resp_header("cache-control", "public, max-age=60, no-transform")
+ # CDN-Tag is used by BunnyCDN to tag cached resources. This allows us to purge
+ # specific tracker scripts from the CDN cache.
+ |> put_resp_header("cdn-tag", "tracker_script::#{id}")
+ |> send_resp(200, script_tag)
+ |> halt()
+
+ {:error, :not_found} ->
+ :telemetry.execute(
+ telemetry_event(:v2),
+ %{},
+ %{status: 404}
+ )
- conn
- |> send_resp(404, "Not found")
- |> halt()
+ conn
+ |> send_resp(404, "Not found")
+ |> halt()
end
end
- defp get_plausible_web_script_tag(tag) do
- on_ee do
- # On cloud, we generate the script always on the fly relying on CDN caching
- PlausibleWeb.TrackerScriptCache.get_from_source(tag)
+ # at this point, id can be both with and without the `pa-` prefix
+ defp get_plausible_web_script(id) do
+ script =
+ on_ee do
+ # On cloud, we generate the script always on the fly relying on CDN caching
+ PlausibleWeb.TrackerScriptCache.get_from_source(id)
+ else
+ # On self-hosted, we have a pre-warmed cache for the script
+ PlausibleWeb.TrackerScriptCache.get(id)
+ end
+
+ if script do
+ {:ok, script}
else
- # On self-hosted, we have a pre-warmed cache for the script
- PlausibleWeb.TrackerScriptCache.get(tag)
+ if not String.starts_with?(id, "pa-") do
+ # maybe the script been migrated to the new format already
+ get_plausible_web_script("pa-" <> id)
+ else
+ {:error, :not_found}
+ end
end
end
diff --git a/test/plausible/data_migration/prefix_tracker_script_configuration_id_test.exs b/test/plausible/data_migration/prefix_tracker_script_configuration_id_test.exs
new file mode 100644
index 0000000000..1fa71b5ace
--- /dev/null
+++ b/test/plausible/data_migration/prefix_tracker_script_configuration_id_test.exs
@@ -0,0 +1,161 @@
+defmodule Plausible.DataMigration.PrefixTrackerScriptConfigurationIdTest do
+ use Plausible.DataCase, async: true
+ use Plausible.Teams.Test
+
+ import ExUnit.CaptureIO
+ import Ecto.Query
+
+ alias Plausible.DataMigration.PrefixTrackerScriptConfigurationId
+ alias Plausible.Site.TrackerScriptConfiguration
+ alias Plausible.Site
+ alias Plausible.Repo
+
+ setup do
+ # Clear the database tables before each test
+ Repo.delete_all(TrackerScriptConfiguration)
+ Repo.delete_all(Site)
+ :ok
+ end
+
+ describe "running the migration" do
+ setup do
+ remove_constraint_if_exists()
+ :ok
+ end
+
+ test "runs for empty dataset" do
+ output =
+ capture_io(fn ->
+ assert :ok = PrefixTrackerScriptConfigurationId.run(2)
+ end)
+
+ assert output =~ "Found 0 total tracker configurations to process"
+ assert output =~ "Migration completed!"
+ end
+
+ test "handles mixed configurations correctly" do
+ # Mix of old and new format configurations
+ _config1 = create_tracker_config("alpha")
+ config2 = create_tracker_config("pa-beta")
+ _config3 = create_tracker_config("gamma")
+ _config4 = create_tracker_config("delta")
+ _config5 = create_tracker_config("epsilon")
+
+ output =
+ capture_io(fn ->
+ assert :ok = PrefixTrackerScriptConfigurationId.run(2)
+ end)
+
+ assert output =~ "Found 4 total tracker configurations to process"
+ assert output =~ "Processing batch 1 (1-2 of 4)"
+ assert output =~ "Processing batch 2 (3-4 of 4)"
+ assert output =~ "Migration completed!"
+
+ # Verify only old format configurations were updated
+ updated_configs =
+ Repo.all(
+ from c in TrackerScriptConfiguration,
+ where: c.id in ["pa-alpha", "pa-gamma", "pa-delta", "pa-epsilon"]
+ )
+
+ assert length(updated_configs) == 4
+ # Verify the one with pa- prefix was not changed
+ assert Repo.get_by(TrackerScriptConfiguration, id: "pa-beta") == config2
+ end
+
+ test "is idempotent - can be run multiple times safely" do
+ config = create_tracker_config("alpha")
+
+ # Run migration first time
+ output1 =
+ capture_io(fn ->
+ assert :ok = PrefixTrackerScriptConfigurationId.run(2)
+ end)
+
+ assert output1 =~ "Found 1 total tracker configurations to process"
+ assert output1 =~ "Processing batch 1 (1-1 of 1)"
+
+ # Verify only the ID changed
+ assert Repo.get_by(TrackerScriptConfiguration, id: "pa-alpha") ==
+ struct(config, id: "pa-alpha")
+
+ # Run migration second time
+ output2 =
+ capture_io(fn ->
+ assert :ok = PrefixTrackerScriptConfigurationId.run(2)
+ end)
+
+ assert output2 =~ "Found 0 total tracker configurations to process"
+ assert output2 =~ "Migration completed!"
+
+ # Verify configuration didn't change further
+ assert Repo.get_by(TrackerScriptConfiguration, id: "pa-alpha") ==
+ struct(config, id: "pa-alpha")
+ end
+ end
+
+ describe "fails gracefully" do
+ setup do
+ add_constraint()
+ :ok
+ end
+
+ test "handles database errors gracefully" do
+ _config1 = create_tracker_config("alpha")
+ _config2 = create_tracker_config("beta")
+
+ output =
+ capture_io(fn ->
+ assert :ok = PrefixTrackerScriptConfigurationId.run(1)
+ end)
+
+ # Should still complete without crashing
+ assert output =~ "Found 2 total tracker configurations to process"
+ assert output =~ "Processing batch 1 (1-1 of 2)"
+ assert output =~ "Error updating batch 1: "
+ assert output =~ "Processing batch 2 (2-2 of 2)"
+ assert output =~ "Migration completed!"
+ end
+ end
+
+ # Helper function to create tracker configurations with specific IDs
+ defp create_tracker_config(id) do
+ site = new_site()
+
+ # Use Repo.insert_all to bypass the autogenerate and set a specific ID
+ Repo.insert_all(
+ TrackerScriptConfiguration,
+ [
+ %{
+ id: id,
+ site_id: site.id,
+ installation_type: :manual,
+ track_404_pages: false,
+ hash_based_routing: false,
+ outbound_links: false,
+ file_downloads: false,
+ revenue_tracking: false,
+ tagged_events: false,
+ form_submissions: false,
+ pageview_props: false,
+ inserted_at: NaiveDateTime.utc_now(:second),
+ updated_at: NaiveDateTime.utc_now(:second)
+ }
+ ]
+ )
+
+ Repo.get_by(TrackerScriptConfiguration, id: id)
+ end
+
+ defp add_constraint() do
+ Repo.query!(
+ "ALTER TABLE tracker_script_configuration ADD CONSTRAINT prevent_pa_prefix CHECK (id NOT LIKE 'pa-%')"
+ )
+ end
+
+ defp remove_constraint_if_exists() do
+ Repo.query!(
+ "ALTER TABLE tracker_script_configuration DROP CONSTRAINT IF EXISTS prevent_pa_prefix"
+ )
+ end
+end
diff --git a/test/plausible_web/live/installationv2_test.exs b/test/plausible_web/live/installationv2_test.exs
index e2429e0bb6..ff62b70a9e 100644
--- a/test/plausible_web/live/installationv2_test.exs
+++ b/test/plausible_web/live/installationv2_test.exs
@@ -91,6 +91,22 @@ defmodule PlausibleWeb.Live.InstallationV2Test do
assert html =~ "Verify NPM installation"
end
+ test "manual installations has script snippet with expected ID", %{conn: conn, site: site} do
+ stub_detection_manual()
+ {lv, _html} = get_lv(conn, site, "?type=manual&flow=review")
+
+ assert eventually(fn ->
+ html = render(lv)
+ {html =~ "Verify Script installation", html}
+ end)
+
+ html = render(lv)
+ config = Plausible.Repo.get_by!(TrackerScriptConfiguration, site_id: site.id)
+ assert html =~ "Privacy-friendly analytics by Plausible"
+ assert html =~ "https://plausible.io/js/#{config.id}.js"
+ assert html =~ "defer=!0"
+ end
+
test "manual installation shows optional measurements", %{conn: conn, site: site} do
stub_detection_manual()
{lv, _html} = get_lv(conn, site, "?type=manual&flow=review")
diff --git a/test/plausible_web/plugs/tracker_plug_test.exs b/test/plausible_web/plugs/tracker_plug_test.exs
index e315732936..7601ebb60e 100644
--- a/test/plausible_web/plugs/tracker_plug_test.exs
+++ b/test/plausible_web/plugs/tracker_plug_test.exs
@@ -42,7 +42,7 @@ defmodule PlausibleWeb.TrackerPlugTest do
:installation
)
- response = get(conn, "/js/s-#{tracker_script_configuration.id}.js") |> response(200)
+ response = get(conn, "/js/#{tracker_script_configuration.id}.js") |> response(200)
assert String.contains?(response, "!function(){var")
assert String.contains?(response, "domain:\"#{site.domain}\"")
@@ -62,11 +62,38 @@ defmodule PlausibleWeb.TrackerPlugTest do
:installation
)
- response = get(conn, "/js/s-#{tracker_script_configuration.id}.js") |> response(200)
+ response = get(conn, "/js/#{tracker_script_configuration.id}.js") |> response(200)
assert String.contains?(response, "window.plausible")
end
+ test "returns the script requested with the legacy s- prefix, even if the id has been prefixed with pa- in the database",
+ %{conn: conn} do
+ site = new_site()
+
+ tracker_script_configuration =
+ Tracker.update_script_configuration!(
+ site,
+ Map.put(@example_config, :site_id, site.id),
+ :installation
+ )
+
+ assert String.starts_with?(tracker_script_configuration.id, "pa-")
+
+ response =
+ get(
+ conn,
+ "/js/s-#{String.replace_leading(tracker_script_configuration.id, "pa-", "")}.js"
+ )
+ |> response(200)
+
+ assert String.contains?(response, "!function(){var")
+ assert String.contains?(response, "domain:\"#{site.domain}\"")
+ assert String.contains?(response, "formSubmissions:!0")
+ refute String.contains?(response, "outboundLinks:!0")
+ refute String.contains?(response, "fileDownloads:!0")
+ end
+
test "returns 404 for unknown site", %{conn: conn} do
conn
|> get("/js/s-a14125a2-000-000-0000-000000000000.js")