Skip to content

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

ScriptV2: plausible-main application code (#5389)

Karl-Aksel Puulmannf8658320d5bb
diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml
index d957fdc5c1..caf84fa055 100644
--- a/.github/workflows/elixir.yml
+++ b/.github/workflows/elixir.yml
@@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true
env:
- CACHE_VERSION: v12
+ CACHE_VERSION: v13
PERSISTENT_CACHE_DIR: cached
jobs:
diff --git a/lib/plausible_web/endpoint.ex b/lib/plausible_web/endpoint.ex
index f85b68ff8d..e950374304 100644
--- a/lib/plausible_web/endpoint.ex
+++ b/lib/plausible_web/endpoint.ex
@@ -35,7 +35,7 @@ defmodule PlausibleWeb.Endpoint do
#
# You should set gzip to true if you are running phx.digest
# when deploying your static files in production.
- plug(PlausibleWeb.Tracker)
+ plug(PlausibleWeb.TrackerPlug)
plug(PlausibleWeb.Favicon)
static_paths = ~w(css js images favicon.ico)
diff --git a/lib/plausible_web/plugs/tracker.ex b/lib/plausible_web/plugs/tracker.ex
deleted file mode 100644
index 64a81aa9fa..0000000000
--- a/lib/plausible_web/plugs/tracker.ex
+++ /dev/null
@@ -1,81 +0,0 @@
-defmodule PlausibleWeb.Tracker do
- import Plug.Conn
- use Agent
-
- base_variants = [
- "hash",
- "outbound-links",
- "exclusions",
- "compat",
- "local",
- "manual",
- "file-downloads",
- "pageview-props",
- "tagged-events",
- "revenue",
- "pageleave"
- ]
-
- # Generates Power Set of all variants
- variants =
- 1..Enum.count(base_variants)
- |> Enum.map(fn x ->
- Combination.combine(base_variants, x)
- |> Enum.map(fn y -> Enum.sort(y) |> Enum.join(".") end)
- end)
- |> List.flatten()
-
- @base_filenames ["plausible", "script", "analytics"]
- @files_available ["plausible.js", "p.js"] ++ Enum.map(variants, fn v -> "plausible.#{v}.js" end)
-
- def init(opts) do
- Keyword.merge(opts, files_available: MapSet.new(@files_available))
- end
-
- def call(conn, files_available: files_available) do
- filename =
- case conn.request_path do
- "/js/p.js" ->
- "p.js"
-
- "/js/" <> requested_filename ->
- sorted_script_variant(requested_filename)
-
- _ ->
- nil
- end
-
- if filename && MapSet.member?(files_available, filename) do
- location = Application.app_dir(:plausible, "priv/tracker/js/" <> filename)
-
- 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=86400, must-revalidate")
- |> send_file(200, location)
- |> halt()
- else
- conn
- end
- end
-
- # Variants which do not factor into output
- @ignore_variants ["js", "pageleave"]
-
- defp sorted_script_variant(requested_filename) do
- case String.split(requested_filename, ".") do
- [base_filename | rest] when base_filename in @base_filenames ->
- sorted_variants =
- rest
- |> Enum.reject(&(&1 in @ignore_variants))
- |> Enum.sort()
-
- Enum.join(["plausible"] ++ sorted_variants ++ ["js"], ".")
-
- _ ->
- nil
- end
- end
-end
diff --git a/lib/plausible_web/plugs/tracker_plug.ex b/lib/plausible_web/plugs/tracker_plug.ex
new file mode 100644
index 0000000000..20a0aeb054
--- /dev/null
+++ b/lib/plausible_web/plugs/tracker_plug.ex
@@ -0,0 +1,117 @@
+defmodule PlausibleWeb.TrackerPlug do
+ @moduledoc """
+ Plug to serve the Plausible tracker script.
+ """
+
+ import Plug.Conn
+ import Ecto.Query
+ use Agent
+
+ base_variants = [
+ "hash",
+ "outbound-links",
+ "exclusions",
+ "compat",
+ "local",
+ "manual",
+ "file-downloads",
+ "pageview-props",
+ "tagged-events",
+ "revenue",
+ "pageleave"
+ ]
+
+ # Generates Power Set of all variants
+ legacy_variants =
+ 1..Enum.count(base_variants)
+ |> Enum.map(fn x ->
+ Combination.combine(base_variants, x)
+ |> Enum.map(fn y -> Enum.sort(y) |> Enum.join(".") end)
+ end)
+ |> List.flatten()
+
+ @base_legacy_filenames ["plausible", "script", "analytics"]
+ @files_available ["plausible.js", "p.js"] ++
+ Enum.map(legacy_variants, fn v -> "plausible.#{v}.js" end)
+
+ def init(opts) do
+ Keyword.merge(opts, files_available: MapSet.new(@files_available))
+ end
+
+ def call(conn, files_available: files_available) 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)
+ else
+ conn
+ end
+
+ "/js/p.js" ->
+ legacy_request_file("p.js", files_available, conn)
+
+ "/js/" <> requested_filename ->
+ sorted_script_variant(requested_filename) |> legacy_request_file(files_available, conn)
+
+ _ ->
+ conn
+ end
+ end
+
+ defp request_tracker_script(tag, conn) do
+ site = Plausible.Repo.one(from s in Plausible.Site, where: s.installation_meta["id"] == ^tag)
+
+ if site do
+ script_tag = PlausibleWeb.Tracker.plausible_main_script_tag(site)
+
+ 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")
+ |> send_resp(200, script_tag)
+ |> halt()
+ else
+ conn
+ |> send_resp(404, "Not found")
+ |> halt()
+ end
+ end
+
+ defp legacy_request_file(filename, files_available, conn) do
+ if filename && MapSet.member?(files_available, filename) do
+ location = Application.app_dir(:plausible, "priv/tracker/js/" <> filename)
+
+ 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=86400, must-revalidate")
+ |> send_file(200, location)
+ |> halt()
+ else
+ conn
+ end
+ end
+
+ # Variants which do not factor into output
+ @ignore_variants ["js", "pageleave"]
+
+ defp sorted_script_variant(requested_filename) do
+ case String.split(requested_filename, ".") do
+ [base_filename | rest] when base_filename in @base_legacy_filenames ->
+ sorted_variants =
+ rest
+ |> Enum.reject(&(&1 in @ignore_variants))
+ |> Enum.sort()
+
+ Enum.join(["plausible"] ++ sorted_variants ++ ["js"], ".")
+
+ _ ->
+ nil
+ end
+ end
+end
diff --git a/lib/plausible_web/tracker.ex b/lib/plausible_web/tracker.ex
new file mode 100644
index 0000000000..037b00d692
--- /dev/null
+++ b/lib/plausible_web/tracker.ex
@@ -0,0 +1,48 @@
+defmodule PlausibleWeb.Tracker do
+ @moduledoc """
+ Helper module for building the dynamic tracker script. Used by PlausibleWeb.TrackerPlug.
+ """
+
+ path = Application.app_dir(:plausible, "priv/tracker/js/plausible-main.js")
+ # On CI, the file might not be present for static checks so we create an empty one
+ File.touch!(path)
+
+ @plausible_main_script File.read!(path)
+ @external_resource "priv/tracker/js/plausible-main.js"
+
+ def plausible_main_script_tag(site) do
+ config_js_content =
+ site
+ |> plausible_main_config()
+ |> Enum.flat_map(fn
+ {key, value} when is_binary(value) -> ["#{key}:#{JSON.encode!(value)}"]
+ # :TRICKY: Save bytes by using short-hand for true
+ {key, true} -> ["#{key}:!0"]
+ # Not enabled values can be omitted
+ {_key, false} -> []
+ end)
+ |> Enum.sort_by(&String.length/1, :desc)
+ |> Enum.join(",")
+
+ @plausible_main_script
+ |> String.replace("\"<%= @config_json %>\"", "{#{config_js_content}}")
+ end
+
+ def plausible_main_config(site) do
+ script_config = site.installation_meta.script_config
+
+ %{
+ domain: site.domain,
+ endpoint: "#{PlausibleWeb.Endpoint.url()}/api/event",
+ hash: Map.get(script_config, "hash", false),
+ outboundLinks: Map.get(script_config, "outbound-links", false),
+ fileDownloads: Map.get(script_config, "file-downloads", false),
+ pageviewProps: Map.get(script_config, "pageview-props", false),
+ taggedEvents: Map.get(script_config, "tagged-events", false),
+ revenue: Map.get(script_config, "revenue", false),
+ # Options not directly exposed via onboarding
+ local: false,
+ manual: false
+ }
+ end
+end
diff --git a/test/plausible_web/plugs/tracker_plug_test.exs b/test/plausible_web/plugs/tracker_plug_test.exs
new file mode 100644
index 0000000000..fdfa47d26a
--- /dev/null
+++ b/test/plausible_web/plugs/tracker_plug_test.exs
@@ -0,0 +1,124 @@
+defmodule PlausibleWeb.TrackerPlugTest do
+ @moduledoc """
+ This test module uses auto-generated JavaScript tracker files in priv/tracker/js.
+
+ To speed up running Elixir tests locally, the tracker files are not automatically generated
+ with every `mix test` command. That means, you have to manually run the below command
+ once before executing `mix test`. This is to make sure all tracker files are in place and up to date.
+
+ ```
+ npm run deploy --prefix tracker
+ ```
+
+ If you're making changes in the tracker template files (`src/plausible.js`, `compile.js`),
+ do regenerate the files before running tests, so they're up to date.
+ """
+ use PlausibleWeb.ConnCase, async: true
+ use Plug.Test
+ use Plausible.Teams.Test
+
+ describe "plausible-main.js" do
+ @example_config %{
+ "404" => true,
+ "hash" => true,
+ "file-downloads" => false,
+ "outbound-links" => false,
+ "pageview-props" => false,
+ "tagged-events" => true,
+ "revenue" => false
+ }
+
+ test "returns the script for an existing site", %{conn: conn} do
+ site =
+ new_site()
+ |> Plausible.Sites.update_installation_meta!(%{script_config: @example_config})
+
+ response = get(conn, "/js/s-#{site.installation_meta.id}.js") |> response(200)
+
+ assert String.contains?(response, "!function(){var")
+ assert String.contains?(response, "domain:\"#{site.domain}\"")
+ assert String.contains?(response, "hash:!0")
+ assert String.contains?(response, "taggedEvents:!0")
+ refute String.contains?(response, "outboundLinks:!0")
+ end
+
+ # window.plausible is a substring checked for by the wordpress plugin to avoid 'optimization' by other wordpress plugins
+ test "script contains window.plausible", %{conn: conn} do
+ site =
+ new_site()
+ |> Plausible.Sites.update_installation_meta!(%{script_config: @example_config})
+
+ response = get(conn, "/js/s-#{site.installation_meta.id}.js") |> response(200)
+
+ assert String.contains?(response, "window.plausible")
+ end
+
+ test "returns 404 for unknown site", %{conn: conn} do
+ conn
+ |> get("/js/s-a14125a2-000-000-0000-000000000000.js")
+ |> response(404)
+ end
+ end
+
+ describe "legacy variants" do
+ test "returns legacy script p.js" do
+ assert String.contains?(get_script("p.js"), "; samesite=strict; path=/")
+ end
+
+ test "returns plausible script with every alias" do
+ plausible_js_response = get_script("plausible.js")
+
+ assert plausible_js_response == get_script("script.js")
+ assert plausible_js_response == get_script("analytics.js")
+ end
+
+ test "returns the right script extensions no matter the order" do
+ response = get_script("plausible.compat.file-downloads.hash.outbound-links.js")
+
+ assert String.contains?(response, "getElementById(\"plausible\")")
+ assert String.contains?(response, "file-types")
+ assert String.contains?(response, "hashchange")
+ assert String.contains?(response, "Outbound Link: Click")
+
+ refute String.contains?(response, "data-exclude")
+ # local extension disabled
+ assert String.contains?(response, "/^localhost$|^127(\\.[0-9]+)")
+
+ assert response == get_script("plausible.outbound-links.file-downloads.compat.hash.js")
+ end
+
+ test "pageleave extension" do
+ # Some customers who have participated in the private preview of the
+ # scroll depth feature, have updated their tracking scripts to
+ # `script.pageleave.js` per our request. With the public release of
+ # scroll depth, this functionality is included in the default script,
+ # but we must continue to serve `script.pageleave.js` for as long as
+ # those customers are still using it.
+ assert get_script("script.pageleave.js") == get_script("script.js")
+ assert get_script("script.manual.pageleave.js") == get_script("script.manual.js")
+ end
+
+ for variant <- [
+ "plausible.js",
+ "script.manual.pageview-props.tagged-events.pageleave.js",
+ "script.compat.local.exclusions.js",
+ "script.hash.revenue.file-downloads.pageview-props.js"
+ ] do
+ test "variant #{variant} is available" do
+ script = get_script(unquote(variant))
+ assert String.starts_with?(script, "!function(){var")
+ assert String.length(script) > 200
+
+ assert String.contains?(script, "window.plausible")
+ end
+ end
+ end
+
+ def get_script(filename) do
+ opts = PlausibleWeb.TrackerPlug.init([])
+
+ conn(:get, "/js/#{filename}")
+ |> PlausibleWeb.TrackerPlug.call(opts)
+ |> response(200)
+ end
+end
diff --git a/test/plausible_web/plugs/tracker_test.exs b/test/plausible_web/plugs/tracker_test.exs
deleted file mode 100644
index 4c649b7ed9..0000000000
--- a/test/plausible_web/plugs/tracker_test.exs
+++ /dev/null
@@ -1,76 +0,0 @@
-defmodule PlausibleWeb.TrackerTest do
- @moduledoc """
- This test module uses auto-generated JavaScript tracker files in priv/tracker/js.
-
- To speed up running Elixir tests locally, the tracker files are not automatically generated
- with every `mix test` command. That means, you have to manually run the below command
- once before executing `mix test`. This is to make sure all tracker files are in place and up to date.
-
- ```
- npm run deploy --prefix tracker
- ```
-
- If you're making changes in the tracker template files (`src/plausible.js`, `compile.js`),
- do regenerate the files before running tests, so they're up to date.
- """
- use PlausibleWeb.ConnCase, async: true
- use Plug.Test
-
- test "returns legacy script p.js" do
- assert String.contains?(get_script("p.js"), "; samesite=strict; path=/")
- end
-
- test "returns plausible script with every alias" do
- plausible_js_response = get_script("plausible.js")
-
- assert plausible_js_response == get_script("script.js")
- assert plausible_js_response == get_script("analytics.js")
- end
-
- test "returns the right script extensions no matter the order" do
- response = get_script("plausible.compat.file-downloads.hash.outbound-links.js")
-
- assert String.contains?(response, "getElementById(\"plausible\")")
- assert String.contains?(response, "file-types")
- assert String.contains?(response, "hashchange")
- assert String.contains?(response, "Outbound Link: Click")
-
- assert !String.contains?(response, "data-exclude")
- # local extension disabled
- assert String.contains?(response, "/^localhost$|^127(\\.[0-9]+)")
-
- assert response == get_script("plausible.outbound-links.file-downloads.compat.hash.js")
- end
-
- test "pageleave extension" do
- # Some customers who have participated in the private preview of the
- # scroll depth feature, have updated their tracking scripts to
- # `script.pageleave.js` per our request. With the public release of
- # scroll depth, this functionality is included in the default script,
- # but we must continue to serve `script.pageleave.js` for as long as
- # those customers are still using it.
- assert get_script("script.pageleave.js") == get_script("script.js")
- assert get_script("script.manual.pageleave.js") == get_script("script.manual.js")
- end
-
- for variant <- [
- "plausible.js",
- "script.manual.pageview-props.tagged-events.pageleave.js",
- "script.compat.local.exclusions.js",
- "script.hash.revenue.file-downloads.pageview-props.js"
- ] do
- test "variant #{variant} is available" do
- script = get_script(unquote(variant))
- assert String.starts_with?(script, "!function(){var")
- assert String.length(script) > 200
- end
- end
-
- def get_script(filename) do
- opts = PlausibleWeb.Tracker.init([])
-
- conn(:get, "/js/#{filename}")
- |> PlausibleWeb.Tracker.call(opts)
- |> response(200)
- end
-end
diff --git a/test/plausible_web/tracker_test.exs b/test/plausible_web/tracker_test.exs
new file mode 100644
index 0000000000..94d67f95fa
--- /dev/null
+++ b/test/plausible_web/tracker_test.exs
@@ -0,0 +1,57 @@
+defmodule PlausibleWeb.TrackerTest do
+ use Plausible.DataCase, async: true
+ use Plausible.Teams.Test
+
+ describe "plausible-main.js" do
+ @example_config %{
+ "404" => true,
+ "hash" => true,
+ "file-downloads" => false,
+ "outbound-links" => false,
+ "pageview-props" => false,
+ "tagged-events" => true,
+ "revenue" => false
+ }
+
+ test "can calculate config" do
+ site =
+ new_site()
+ |> Plausible.Sites.update_installation_meta!(%{script_config: @example_config})
+
+ assert PlausibleWeb.Tracker.plausible_main_config(site) == %{
+ domain: site.domain,
+ endpoint: "#{PlausibleWeb.Endpoint.url()}/api/event",
+ hash: true,
+ outboundLinks: false,
+ fileDownloads: false,
+ pageviewProps: false,
+ taggedEvents: true,
+ revenue: false,
+ local: false,
+ manual: false
+ }
+ end
+
+ test "can turn config into a script tag" do
+ site =
+ new_site()
+ |> Plausible.Sites.update_installation_meta!(%{script_config: @example_config})
+
+ script_tag = PlausibleWeb.Tracker.plausible_main_script_tag(site)
+
+ assert script_tag =~
+ ~s(={endpoint:"#{PlausibleWeb.Endpoint.url()}/api/event",domain:"#{site.domain}",taggedEvents:!0,hash:!0})
+ end
+
+ test "script tag escapes problematic characters as expected" do
+ site =
+ new_site()
+ |> Plausible.Sites.update_installation_meta!(%{script_config: @example_config})
+ |> Map.merge(%{domain: "naughty domain &amp;<> \"\'\nfoo "})
+
+ script_tag = PlausibleWeb.Tracker.plausible_main_script_tag(site)
+
+ assert script_tag =~ ~s(domain:"naughty domain &amp;<> \\"'\\nfoo ")
+ end
+ end
+end