Skip to content

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

tracker_script_configuration: schema, double-writes (step 2) (#5407)

Karl-Aksel Puulmannb9728177826c
diff --git a/lib/plausible/ecto/types/nanoid.ex b/lib/plausible/ecto/types/nanoid.ex
new file mode 100644
index 0000000000..97a0c139ec
--- /dev/null
+++ b/lib/plausible/ecto/types/nanoid.ex
@@ -0,0 +1,20 @@
+defmodule Plausible.Ecto.Types.Nanoid do
+ @moduledoc """
+ Custom column type for nanoid strings
+ """
+
+ use Ecto.Type
+
+ def type(), do: :string
+
+ def cast(value) when is_binary(value), do: {:ok, value}
+ def cast(_), do: :error
+
+ def load(value), do: {:ok, value}
+
+ def dump(value) when is_binary(value), do: {:ok, value}
+ def dump(_), do: :error
+
+ @spec autogenerate() :: String.t()
+ def autogenerate(), do: Nanoid.generate()
+end
diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex
index ce812e2007..598f113b7f 100644
--- a/lib/plausible/site.ex
+++ b/lib/plausible/site.ex
@@ -41,6 +41,8 @@ defmodule Plausible.Site do
on_replace: :update,
defaults_to_struct: true
+ has_one :tracker_script_configuration, Plausible.Site.TrackerScriptConfiguration
+
has_many :goals, Plausible.Goal, preload_order: [desc: :id]
has_many :revenue_goals, Plausible.Goal, where: [currency: {:not, nil}]
has_one :google_auth, GoogleAuth
diff --git a/lib/plausible/site/installation_meta.ex b/lib/plausible/site/installation_meta.ex
index 58de0d8df8..1ab46912a0 100644
--- a/lib/plausible/site/installation_meta.ex
+++ b/lib/plausible/site/installation_meta.ex
@@ -4,10 +4,35 @@ defmodule Plausible.Site.InstallationMeta do
"""
use Ecto.Schema
+ alias Plausible.Site.TrackerScriptConfiguration
+
@type t() :: %__MODULE__{}
embedded_schema do
field :installation_type, :string, default: "manual"
field :script_config, :map, default: %{}
end
+
+ def to_tracker_script_configuration(site, installation_meta \\ nil) do
+ installation_meta =
+ installation_meta || site.installation_meta || %Plausible.Site.InstallationMeta{}
+
+ %TrackerScriptConfiguration{
+ site_id: site.id,
+ installation_type:
+ Map.get(installation_meta, :installation_type) |> remap_installation_type(),
+ track_404_pages: Map.get(installation_meta.script_config, "404", false),
+ hash_based_routing: Map.get(installation_meta.script_config, "hash", false),
+ outbound_links: Map.get(installation_meta.script_config, "outbound-links", false),
+ file_downloads: Map.get(installation_meta.script_config, "file-downloads", false),
+ revenue_tracking: Map.get(installation_meta.script_config, "revenue", false),
+ tagged_events: Map.get(installation_meta.script_config, "tagged-events", false),
+ form_submissions: Map.get(installation_meta.script_config, "form-submissions", false),
+ pageview_props: Map.get(installation_meta.script_config, "pageview-props", false)
+ }
+ end
+
+ defp remap_installation_type("WordPress"), do: "wordpress"
+ defp remap_installation_type("GTM"), do: "gtm"
+ defp remap_installation_type(value), do: value
end
diff --git a/lib/plausible/site/tracker_script_configuration.ex b/lib/plausible/site/tracker_script_configuration.ex
new file mode 100644
index 0000000000..dca9a65f68
--- /dev/null
+++ b/lib/plausible/site/tracker_script_configuration.ex
@@ -0,0 +1,68 @@
+defmodule Plausible.Site.TrackerScriptConfiguration do
+ @moduledoc """
+ Schema for tracker script configuration
+ """
+ use Ecto.Schema
+ import Ecto.Changeset
+
+ @type t() :: %__MODULE__{}
+
+ @primary_key {:id, Plausible.Ecto.Types.Nanoid, autogenerate: true}
+ schema "tracker_script_configuration" do
+ field :installation_type, Ecto.Enum, values: [:manual, :wordpress, :gtm, nil]
+
+ field :track_404_pages, :boolean, default: false
+ field :hash_based_routing, :boolean, default: false
+ field :outbound_links, :boolean, default: false
+ field :file_downloads, :boolean, default: false
+ field :revenue_tracking, :boolean, default: false
+ field :tagged_events, :boolean, default: false
+ field :form_submissions, :boolean, default: false
+ field :pageview_props, :boolean, default: false
+
+ belongs_to :site, Plausible.Site
+
+ timestamps()
+ end
+
+ def changeset(struct, params) do
+ struct
+ |> cast(params, [
+ :installation_type,
+ :track_404_pages,
+ :hash_based_routing,
+ :outbound_links,
+ :file_downloads,
+ :revenue_tracking,
+ :tagged_events,
+ :form_submissions,
+ :pageview_props,
+ :site_id
+ ])
+ |> validate_required([:site_id])
+ end
+
+ def upsert(configuration_map) do
+ changeset = changeset(%__MODULE__{}, configuration_map)
+
+ Plausible.Repo.insert(
+ changeset,
+ on_conflict: {:replace, fields_to_update(configuration_map)},
+ conflict_target: [:site_id],
+ returning: true
+ )
+ end
+
+ defp fields_to_update(configuration_map) do
+ fields = __MODULE__.__schema__(:fields)
+
+ configuration_map
+ |> Map.keys()
+ |> Enum.map(fn
+ key when is_atom(key) -> key
+ key when is_binary(key) -> String.to_existing_atom(key)
+ end)
+ |> Enum.filter(&(&1 in fields))
+ |> Enum.concat([:updated_at])
+ end
+end
diff --git a/lib/plausible_web/live/installation.ex b/lib/plausible_web/live/installation.ex
index 6681be0e9a..ee94808807 100644
--- a/lib/plausible_web/live/installation.ex
+++ b/lib/plausible_web/live/installation.ex
@@ -4,22 +4,23 @@ defmodule PlausibleWeb.Live.Installation do
"""
use PlausibleWeb, :live_view
alias Plausible.Verification.{Checks, State}
+ alias Plausible.Site.InstallationMeta
- @script_extension_params [
- "outbound-links",
- "tagged-events",
- "file-downloads",
- "hash",
- "pageview-props",
- "revenue"
- ]
+ @script_extension_params %{
+ "outbound_links" => "outbound-links",
+ "tagged_events" => "tagged-events",
+ "file_downloads" => "file-downloads",
+ "hash_based_routing" => "hash",
+ "pageview_props" => "pageview-props",
+ "revenue_tracking" => "revenue"
+ }
- @script_config_params ["404" | @script_extension_params]
+ @script_config_params ["track_404_pages" | Map.keys(@script_extension_params)]
@installation_types [
- "GTM",
+ "gtm",
"manual",
- "WordPress"
+ "wordpress"
]
@valid_qs_params @script_config_params ++ ["installation_type", "flow"]
@@ -48,15 +49,15 @@ defmodule PlausibleWeb.Live.Installation do
end
flow = params["flow"]
- meta = site.installation_meta || %Plausible.Site.InstallationMeta{}
- script_config =
- @script_config_params
- |> Enum.into(%{}, &{&1, false})
- |> Map.merge(meta.script_config)
- |> Map.take(@script_config_params)
+ tracker_script_configuration = InstallationMeta.to_tracker_script_configuration(site)
+ installation_type = get_installation_type(flow, tracker_script_configuration, params)
- installation_type = get_installation_type(flow, meta, params)
+ config =
+ Map.new(@script_config_params, fn key ->
+ string_key = String.to_existing_atom(key)
+ {key, Map.get(tracker_script_configuration, string_key)}
+ end)
if connected?(socket) and is_nil(installation_type) do
Checks.run("https://#{domain}", domain,
@@ -80,15 +81,15 @@ defmodule PlausibleWeb.Live.Installation do
installation_type: installation_type,
initial_installation_type: installation_type,
domain: domain,
- script_config: script_config
+ config: config
)}
end
def handle_info({:verification_end, %State{} = state}, socket) do
installation_type =
case state.diagnostics do
- %{wordpress_likely?: true} -> "WordPress"
- %{gtm_likely?: true} -> "GTM"
+ %{wordpress_likely?: true} -> "wordpress"
+ %{gtm_likely?: true} -> "gtm"
_ -> "manual"
end
@@ -116,10 +117,10 @@ defmodule PlausibleWeb.Live.Installation do
<.spinner class="spinner block text-center h-8 w-8" />
</div>
</:title>
- <:title :if={@installation_type == "WordPress"}>
+ <:title :if={@installation_type == "wordpress"}>
Install WordPress plugin
</:title>
- <:title :if={@installation_type == "GTM"}>
+ <:title :if={@installation_type == "gtm"}>
Install Google Tag Manager
</:title>
<:title :if={@installation_type == "manual"}>
@@ -157,7 +158,7 @@ defmodule PlausibleWeb.Live.Installation do
</p>
</:subtitle>
- <:subtitle :if={@installation_type == "WordPress"}>
+ <:subtitle :if={@installation_type == "wordpress"}>
We've detected your website is using WordPress. Here's how to integrate Plausible:
<.focus_list>
<:item>
@@ -170,7 +171,7 @@ defmodule PlausibleWeb.Live.Installation do
</:item>
</.focus_list>
</:subtitle>
- <:subtitle :if={@installation_type == "GTM"}>
+ <:subtitle :if={@installation_type == "gtm"}>
We've detected your website is using Google Tag Manager. Here's how to integrate Plausible:
<.focus_list>
<:item>
@@ -193,12 +194,8 @@ defmodule PlausibleWeb.Live.Installation do
Once done, click the button below to verify your installation.
</:subtitle>
- <div :if={@installation_type in ["manual", "GTM"]}>
- <.snippet_form
- installation_type={@installation_type}
- script_config={@script_config}
- domain={@domain}
- />
+ <div :if={@installation_type in ["manual", "gtm"]}>
+ <.snippet_form installation_type={@installation_type} config={@config} domain={@domain} />
</div>
<.button_link
@@ -218,18 +215,18 @@ defmodule PlausibleWeb.Live.Installation do
<% end %>
</.button_link>
- <:footer :if={@initial_installation_type == "WordPress" and @installation_type == "manual"}>
- <.styled_link href={} phx-click="switch-installation-type" phx-value-method="WordPress">
+ <:footer :if={@initial_installation_type == "wordpress" and @installation_type == "manual"}>
+ <.styled_link href={} phx-click="switch-installation-type" phx-value-method="wordpress">
Click here
</.styled_link>
if you prefer WordPress installation method.
</:footer>
<:footer :if={
- (@initial_installation_type == "GTM" and @installation_type == "manual") or
+ (@initial_installation_type == "gtm" and @installation_type == "manual") or
(@initial_installation_type == "manual" and @installation_type == "manual")
}>
- <.styled_link href={} phx-click="switch-installation-type" phx-value-method="GTM">
+ <.styled_link href={} phx-click="switch-installation-type" phx-value-method="gtm">
Click here
</.styled_link>
if you prefer Google Tag Manager installation method.
@@ -246,8 +243,8 @@ defmodule PlausibleWeb.Live.Installation do
"""
end
- defp render_snippet("manual", domain, %{"404" => true} = script_config) do
- script_config = Map.put(script_config, "404", false)
+ defp render_snippet("manual", domain, %{"track_404_pages" => true} = script_config) do
+ script_config = Map.put(script_config, "track_404_pages", false)
"""
#{render_snippet("manual", domain, script_config)}
@@ -259,16 +256,16 @@ defmodule PlausibleWeb.Live.Installation do
~s|<script defer data-domain="#{domain}" src="#{tracker_url(script_config)}"></script>|
end
- defp render_snippet("GTM", domain, %{"404" => true} = script_config) do
- script_config = Map.put(script_config, "404", false)
+ defp render_snippet("gtm", domain, %{"track_404_pages" => true} = script_config) do
+ script_config = Map.put(script_config, "track_404_pages", false)
"""
- #{render_snippet("GTM", domain, script_config)}
- #{render_snippet_404("GTM")}
+ #{render_snippet("gtm", domain, script_config)}
+ #{render_snippet_404("gtm")}
"""
end
- defp render_snippet("GTM", domain, script_config) do
+ defp render_snippet("gtm", domain, script_config) do
"""
<script>
var script = document.createElement('script');
@@ -285,7 +282,7 @@ defmodule PlausibleWeb.Live.Installation do
"<script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>"
end
- def render_snippet_404("GTM") do
+ def render_snippet_404("gtm") do
render_snippet_404()
end
@@ -297,7 +294,7 @@ defmodule PlausibleWeb.Live.Installation do
type="checkbox"
id={"check-#{@variant}"}
name={@variant}
- checked={@config[@variant]}
+ checked={Map.get(@config, @variant, false)}
class="block h-5 w-5 rounded dark:bg-gray-700 border-gray-300 text-indigo-600 focus:ring-indigo-600 mr-2"
/>
<label for={"check-#{@variant}"}>
@@ -333,7 +330,7 @@ defmodule PlausibleWeb.Live.Installation do
class="w-full border-1 border-gray-300 rounded-md p-4 text-sm text-gray-700 dark:border-gray-500 dark:bg-gray-900 dark:text-gray-300"
rows="5"
readonly
- ><%= render_snippet(@installation_type, @domain, @script_config) %></textarea>
+ ><%= render_snippet(@installation_type, @domain, @config) %></textarea>
<a
onclick="var input = document.getElementById('snippet'); input.focus(); input.select(); document.execCommand('copy'); event.stopPropagation();"
@@ -349,50 +346,50 @@ defmodule PlausibleWeb.Live.Installation do
<.h2 class="mt-8 text-sm font-medium">Enable optional measurements:</.h2>
<.script_extension_control
- config={@script_config}
- variant="outbound-links"
+ config={@config}
+ variant="outbound_links"
label="Outbound links"
tooltip="Automatically track clicks on external links. These count towards your billable pageviews."
learn_more="https://plausible.io/docs/outbound-link-click-tracking"
/>
<.script_extension_control
- config={@script_config}
- variant="file-downloads"
+ config={@config}
+ variant="file_downloads"
label="File downloads"
tooltip="Automatically track file downloads. These count towards your billable pageviews."
learn_more="https://plausible.io/docs/file-downloads-tracking"
/>
<.script_extension_control
- config={@script_config}
- variant="404"
+ config={@config}
+ variant="track_404_pages"
label="404 error pages"
tooltip="Find 404 error pages on your site. These count towards your billable pageviews. Additional action required."
learn_more="https://plausible.io/docs/error-pages-tracking-404"
/>
<.script_extension_control
- config={@script_config}
- variant="hash"
+ config={@config}
+ variant="hash_based_routing"
label="Hashed page paths"
tooltip="Automatically track page paths that use a # in the URL."
learn_more="https://plausible.io/docs/hash-based-routing"
/>
<.script_extension_control
- config={@script_config}
- variant="tagged-events"
+ config={@config}
+ variant="tagged_events"
label="Custom events"
tooltip="Tag site elements like buttons, links and forms to track user activity. These count towards your billable pageviews. Additional action required."
learn_more="https://plausible.io/docs/custom-event-goals"
/>
<.script_extension_control
- config={@script_config}
- variant="pageview-props"
+ config={@config}
+ variant="pageview_props"
label="Custom properties"
tooltip="Attach custom properties (also known as custom dimensions) to pageviews or custom events to create custom metrics. Additional action required."
learn_more="https://plausible.io/docs/custom-props/introduction"
/>
<.script_extension_control
- config={@script_config}
- variant="revenue"
+ config={@config}
+ variant="revenue_tracking"
label="Ecommerce revenue"
tooltip="Assign monetary values to purchases and track revenue attribution. Additional action required."
learn_more="https://plausible.io/docs/ecommerce-revenue-tracking"
@@ -408,10 +405,11 @@ defmodule PlausibleWeb.Live.Installation do
end
def handle_event("update-script-config", params, socket) do
- new_params =
- Enum.into(@script_config_params, %{}, &{&1, params[&1] == "on"})
+ new_config =
+ @script_config_params
+ |> Map.new(fn key -> {key, Map.get(params, key) == "on"} end)
- flash = snippet_change_flash(socket.assigns.script_config, new_params)
+ flash = snippet_change_flash(socket.assigns.config, new_config)
socket =
if flash do
@@ -420,21 +418,21 @@ defmodule PlausibleWeb.Live.Installation do
socket
end
- socket = update_uri_params(socket, new_params)
+ socket = update_uri_params(socket, new_config)
{:noreply, socket}
end
def handle_params(params, _uri, socket) do
- socket = do_handle_params(socket, params)
- persist_installation_meta(socket)
- {:noreply, socket}
- end
+ socket =
+ socket
+ |> update_installation_type(params)
+ |> update_script_config(params)
+ |> persist_tracker_script_configuration()
- defp do_handle_params(socket, params) when is_map(params) do
- Enum.reduce(params, socket, &param_reducer/2)
+ {:noreply, socket}
end
- defp param_reducer({"installation_type", installation_type}, socket)
+ defp update_installation_type(socket, %{"installation_type" => installation_type})
when installation_type in @installation_types do
assign(socket,
installation_type: installation_type,
@@ -442,52 +440,17 @@ defmodule PlausibleWeb.Live.Installation do
)
end
- defp param_reducer({k, v}, socket)
- when k in @script_config_params do
- update_script_config(socket, k, v == "true")
- end
-
- defp param_reducer(_, socket) do
- socket
- end
-
- defp update_script_config(socket, "outbound-links" = key, true) do
- Plausible.Goals.create_outbound_links(socket.assigns.site)
- update_script_config(socket, %{key => true})
- end
-
- defp update_script_config(socket, "outbound-links" = key, false) do
- Plausible.Goals.delete_outbound_links(socket.assigns.site)
- update_script_config(socket, %{key => false})
- end
-
- defp update_script_config(socket, "file-downloads" = key, true) do
- Plausible.Goals.create_file_downloads(socket.assigns.site)
- update_script_config(socket, %{key => true})
- end
-
- defp update_script_config(socket, "file-downloads" = key, false) do
- Plausible.Goals.delete_file_downloads(socket.assigns.site)
- update_script_config(socket, %{key => false})
- end
-
- defp update_script_config(socket, "404" = key, true) do
- Plausible.Goals.create_404(socket.assigns.site)
- update_script_config(socket, %{key => true})
- end
-
- defp update_script_config(socket, "404" = key, false) do
- Plausible.Goals.delete_404(socket.assigns.site)
- update_script_config(socket, %{key => false})
- end
+ defp update_installation_type(socket, _params), do: socket
- defp update_script_config(socket, key, value) do
- update_script_config(socket, %{key => value})
- end
+ defp update_script_config(socket, params) do
+ configuration_update =
+ @script_config_params
+ |> Enum.filter(&Map.has_key?(params, &1))
+ |> Map.new(fn key -> {key, Map.get(params, key) == "true"} end)
- defp update_script_config(socket, kv) when is_map(kv) do
- new_script_config = Map.merge(socket.assigns.script_config, kv)
- assign(socket, script_config: new_script_config)
+ assign(socket,
+ config: Map.merge(socket.assigns.config, configuration_update)
+ )
end
defp update_uri_params(socket, params) when is_map(params) do
@@ -508,45 +471,52 @@ defmodule PlausibleWeb.Live.Installation do
end
@domain_change PlausibleWeb.Flows.domain_change()
- defp get_installation_type(@domain_change, meta, params) do
- meta.installation_type || get_installation_type(nil, nil, params)
+ defp get_installation_type(@domain_change, tracker_script_configuration, params) do
+ tracker_script_configuration.installation_type || get_installation_type(nil, nil, params)
end
- defp get_installation_type(_site, _meta, params) do
+ defp get_installation_type(_type, _tracker_script_configuration, params) do
Enum.find(@installation_types, &(&1 == params["installation_type"]))
end
defp tracker_url(script_config) do
- extensions = Enum.filter(script_config, fn {_, value} -> value end)
+ extensions =
+ @script_extension_params
+ |> Enum.flat_map(fn {key, extension} ->
+ if(Map.get(script_config, key), do: [extension], else: [])
+ end)
- tracker =
- ["script" | Enum.map(extensions, fn {key, _} -> key end)]
- |> Enum.join(".")
+ tracker = Enum.join(["script" | extensions], ".")
"#{PlausibleWeb.Endpoint.url()}/js/#{tracker}.js"
end
- defp persist_installation_meta(socket) do
- Plausible.Sites.update_installation_meta!(
+ defp persist_tracker_script_configuration(socket) do
+ updated_tracker_script_configuration =
+ Map.merge(socket.assigns.config, %{
+ "site_id" => socket.assigns.site.id,
+ "installation_type" => socket.assigns.installation_type
+ })
+
+ PlausibleWeb.Tracker.update_script_configuration(
socket.assigns.site,
- %{
- installation_type: socket.assigns.installation_type,
- script_config: socket.assigns.script_config
- }
+ updated_tracker_script_configuration
)
+
+ socket
end
defp snippet_change_flash(old_config, new_config) do
change =
- Enum.find(new_config, fn {key, _value} ->
- old_config[key] != new_config[key]
+ Enum.find(new_config, fn {key, new_value} ->
+ Map.get(old_config, key) != new_value
end)
case change do
nil ->
nil
- {k, false} when k in ["outbound-links", "file-downloads", "404"] ->
+ {k, false} when k in ["outbound_links", "file_downloads", "track_404_pages"] ->
"Snippet updated and goal deleted. Please insert the newest snippet into your site"
{_, _} ->
diff --git a/lib/plausible_web/tracker.ex b/lib/plausible_web/tracker.ex
index e4e1cf94b7..e6a80c9578 100644
--- a/lib/plausible_web/tracker.ex
+++ b/lib/plausible_web/tracker.ex
@@ -3,6 +3,9 @@ defmodule PlausibleWeb.Tracker do
Helper module for building the dynamic tracker script. Used by PlausibleWeb.TrackerPlug.
"""
+ use Plausible.Repo
+ alias Plausible.Site.TrackerScriptConfiguration
+
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)
@@ -44,4 +47,52 @@ defmodule PlausibleWeb.Tracker do
manual: false
}
end
+
+ def update_script_configuration(site, config_update) do
+ installation_meta_update = to_installation_meta(config_update)
+
+ Repo.transaction(fn ->
+ Plausible.Sites.update_installation_meta!(site, installation_meta_update)
+ {:ok, _} = TrackerScriptConfiguration.upsert(config_update)
+
+ update_goals(site, config_update)
+ end)
+ end
+
+ # Sync plausible goals with the updated script config
+ defp update_goals(site, config_update) do
+ config_update
+ |> Enum.map(fn {key, value} -> {to_atom(key), value} end)
+ |> Enum.each(fn
+ {:track_404_pages, true} -> Plausible.Goals.create_404(site)
+ {:track_404_pages, false} -> Plausible.Goals.delete_404(site)
+ {:outbound_links, true} -> Plausible.Goals.create_outbound_links(site)
+ {:outbound_links, false} -> Plausible.Goals.delete_outbound_links(site)
+ {:file_downloads, true} -> Plausible.Goals.create_file_downloads(site)
+ {:file_downloads, false} -> Plausible.Goals.delete_file_downloads(site)
+ _ -> nil
+ end)
+ end
+
+ defp to_installation_meta(config_update) do
+ %{
+ installation_type: remap_installation_type(config_update["installation_type"]),
+ script_config: %{
+ "404" => Map.get(config_update, "track_404_pages", false),
+ "hash" => Map.get(config_update, "hash_based_routing", false),
+ "outbound-links" => Map.get(config_update, "outbound_links", false),
+ "file-downloads" => Map.get(config_update, "file_downloads", false),
+ "revenue" => Map.get(config_update, "revenue_tracking", false),
+ "tagged-events" => Map.get(config_update, "tagged_events", false),
+ "pageview-props" => Map.get(config_update, "pageview_props", false)
+ }
+ }
+ end
+
+ defp remap_installation_type("wordpress"), do: "WordPress"
+ defp remap_installation_type("gtm"), do: "GTM"
+ defp remap_installation_type(value), do: value
+
+ defp to_atom(str) when is_binary(str), do: String.to_existing_atom(str)
+ defp to_atom(atom) when is_atom(atom), do: atom
end
diff --git a/test/plausible/site/tracker_script_configuration_test.exs b/test/plausible/site/tracker_script_configuration_test.exs
new file mode 100644
index 0000000000..5ad3d565da
--- /dev/null
+++ b/test/plausible/site/tracker_script_configuration_test.exs
@@ -0,0 +1,59 @@
+defmodule Plausible.Site.TrackerScriptConfigurationTest do
+ use Plausible
+ use Plausible.DataCase, async: true
+ use Plausible.Teams.Test
+
+ alias Plausible.Site.TrackerScriptConfiguration
+
+ test "#upsert/2" do
+ site = insert(:site)
+
+ {:ok, initial_configuration} =
+ TrackerScriptConfiguration.upsert(%{
+ site_id: site.id,
+ installation_type: nil,
+ track_404_pages: true,
+ hash_based_routing: true,
+ outbound_links: true,
+ pageview_props: true
+ })
+
+ assert_matches %{
+ id: ^any(:string),
+ site_id: ^site.id,
+ installation_type: nil,
+ track_404_pages: true,
+ hash_based_routing: true,
+ outbound_links: true,
+ file_downloads: false,
+ revenue_tracking: false,
+ tagged_events: false,
+ form_submissions: false,
+ pageview_props: true
+ } = initial_configuration
+
+ {:ok, updated_configuration} =
+ TrackerScriptConfiguration.upsert(%{
+ site_id: site.id,
+ installation_type: :wordpress,
+ track_404_pages: false,
+ outbound_links: true,
+ file_downloads: true,
+ pageview_props: false
+ })
+
+ assert_matches %{
+ id: ^initial_configuration.id,
+ site_id: ^site.id,
+ installation_type: :wordpress,
+ track_404_pages: false,
+ hash_based_routing: true,
+ outbound_links: true,
+ file_downloads: true,
+ revenue_tracking: false,
+ tagged_events: false,
+ form_submissions: false,
+ pageview_props: false
+ } = updated_configuration
+ end
+end
diff --git a/test/plausible_web/live/installation_test.exs b/test/plausible_web/live/installation_test.exs
index c35d73a26a..dc3729f193 100644
--- a/test/plausible_web/live/installation_test.exs
+++ b/test/plausible_web/live/installation_test.exs
@@ -70,7 +70,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
test "renders pre-determined installation type: WordPress", %{conn: conn, site: site} do
resp =
conn
- |> get("/#{site.domain}/installation?installation_type=WordPress")
+ |> get("/#{site.domain}/installation?installation_type=wordpress")
|> html_response(200)
assert resp =~ "Install WordPress plugin"
@@ -79,13 +79,13 @@ defmodule PlausibleWeb.Live.InstallationTest do
assert resp =~
Routes.site_path(PlausibleWeb.Endpoint, :verification, site.domain,
- installation_type: "WordPress"
+ installation_type: "wordpress"
)
end
test "renders pre-determined installation type: GTM", %{conn: conn, site: site} do
resp =
- conn |> get("/#{site.domain}/installation?installation_type=GTM") |> html_response(200)
+ conn |> get("/#{site.domain}/installation?installation_type=gtm") |> html_response(200)
assert resp =~ "Install Google Tag Manager"
assert resp =~ "Start collecting data"
@@ -93,7 +93,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
assert resp =~
Routes.site_path(PlausibleWeb.Endpoint, :verification, site.domain,
- installation_type: "GTM"
+ installation_type: "gtm"
)
end
@@ -144,7 +144,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
} do
stub_fetch_body(200, "wp-content")
- {lv, _} = get_lv(conn, site, "?installation_type=GTM")
+ {lv, _} = get_lv(conn, site, "?installation_type=gtm")
refute eventually(fn ->
html = render(lv)
@@ -164,7 +164,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
assert text_of_element(html, "textarea#snippet") ==
"&amp;lt;script defer data-domain=&amp;quot;#{site.domain}&amp;quot; src=&amp;quot;http://localhost:8000/js/script.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;"
- for param <- PlausibleWeb.Live.Installation.script_extension_params() do
+ for {param, script_extension} <- PlausibleWeb.Live.Installation.script_extension_params() do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
@@ -172,7 +172,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
})
html = lv |> render()
- assert text_of_element(html, "textarea#snippet") =~ "/js/script.#{param}.js"
+ assert text_of_element(html, "textarea#snippet") =~ "/js/script.#{script_extension}.js"
lv
|> element(~s|form#snippet-form|)
@@ -186,11 +186,11 @@ defmodule PlausibleWeb.Live.InstallationTest do
end
test "allows GTM snippet customization", %{conn: conn, site: site} do
- {lv, html} = get_lv(conn, site, "?installation_type=GTM")
+ {lv, html} = get_lv(conn, site, "?installation_type=gtm")
assert text_of_element(html, "textarea#snippet") =~ "script.defer = true"
- for param <- PlausibleWeb.Live.Installation.script_extension_params() do
+ for {param, script_extension} <- PlausibleWeb.Live.Installation.script_extension_params() do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
@@ -198,7 +198,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
})
html = lv |> render()
- assert text_of_element(html, "textarea#snippet") =~ "/js/script.#{param}.js"
+ assert text_of_element(html, "textarea#snippet") =~ "/js/script.#{script_extension}.js"
lv
|> element(~s|form#snippet-form|)
@@ -217,7 +217,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
- "404" => "on"
+ "track_404_pages" => "on"
})
html = lv |> render()
@@ -246,17 +246,19 @@ defmodule PlausibleWeb.Live.InstallationTest do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
- "file-downloads" => "on",
- "outbound-links" => "on",
- "404" => "on"
+ "file_downloads" => "on",
+ "outbound_links" => "on",
+ "track_404_pages" => "on"
})
lv |> render()
- assert [clicks, downloads, error_404] = Plausible.Goals.for_site(site)
- assert clicks.event_name == "Outbound Link: Click"
+ assert [track_404_pages, downloads, clicks] =
+ Plausible.Goals.for_site(site) |> Enum.sort_by(& &1.event_name)
+
+ assert track_404_pages.event_name == "404"
assert downloads.event_name == "File Download"
- assert error_404.event_name == "404"
+ assert clicks.event_name == "Outbound Link: Click"
end
test "turning off file-downloads, outbound-links and 404 deletes special goals", %{
@@ -270,9 +272,9 @@ defmodule PlausibleWeb.Live.InstallationTest do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
- "file-downloads" => "on",
- "outbound-links" => "on",
- "404" => "on"
+ "file_downloads" => "on",
+ "outbound_links" => "on",
+ "track_404_pages" => "on"
})
assert [_, _, _] = Plausible.Goals.for_site(site)
@@ -280,8 +282,8 @@ defmodule PlausibleWeb.Live.InstallationTest do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
- "file-downloads" => "on",
- "outbound-links" => "on"
+ "file_downloads" => "on",
+ "outbound_links" => "on"
})
assert render(lv) =~ "Snippet updated and goal deleted"
@@ -289,7 +291,7 @@ defmodule PlausibleWeb.Live.InstallationTest do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
- "file-downloads" => "on"
+ "file_downloads" => "on"
})
assert render(lv) =~ "Snippet updated and goal deleted"
@@ -312,10 +314,10 @@ defmodule PlausibleWeb.Live.InstallationTest do
lv
|> element(~s|form#snippet-form|)
|> render_change(%{
- "tagged-events" => "on",
- "hash" => "on",
- "pageview-props" => "on",
- "revenue" => "on"
+ "tagged_events" => "on",
+ "hash_based_routing" => "on",
+ "pageview_props" => "on",
+ "revenue_tracking" => "on"
})
assert render(lv) =~ "Snippet updated. Please insert the newest snippet into your site"