Skip to content

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

Onboarding review flow (#5491)

Uku Taht973b6266256d
diff --git a/extra/lib/plausible_web/live/customer_support/live/team.ex b/extra/lib/plausible_web/live/customer_support/live/team.ex
index b3faa18dea..aac4a237d4 100644
--- a/extra/lib/plausible_web/live/customer_support/live/team.ex
+++ b/extra/lib/plausible_web/live/customer_support/live/team.ex
@@ -358,10 +358,9 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
:if={not mod.free?()}
x-on:change="featureChangeCallback(event)"
type="checkbox"
- name={"#{f.name}[features[]][]"}
- value={mod.name()}
+ value={mod in (f.source.changes[:features] || [])}
+ name={"#{f.name}[features[]][#{mod.name()}]"}
label={mod.display_name()}
- checked={mod in (f.source.changes[:features] || [])}
/>
<div class="mt-8 flex align-center gap-x-4">
@@ -878,7 +877,12 @@ defmodule PlausibleWeb.CustomerSupport.Live.Team do
end
defp update_features_to_list(params) do
- Map.put(params, "features", Enum.reject(params["features[]"], &(&1 == "false" or &1 == "")))
+ features =
+ params["features[]"]
+ |> Enum.reject(fn {_key, value} -> value == "false" or value == "" end)
+ |> Enum.map(fn {key, _value} -> key end)
+
+ Map.put(params, "features", features)
end
defp preview_number(n) do
diff --git a/lib/plausible_web/live/components/form.ex b/lib/plausible_web/live/components/form.ex
index e8a379b2a9..7da02fc21b 100644
--- a/lib/plausible_web/live/components/form.ex
+++ b/lib/plausible_web/live/components/form.ex
@@ -106,7 +106,7 @@ defmodule PlausibleWeb.Live.Components.Form do
<input type="hidden" name={@name} value="false" disabled={@rest[:disabled]} />
<input
type="checkbox"
- value={assigns[:value] || "true"}
+ value="true"
checked={@checked}
id={@id}
name={@name}
diff --git a/lib/plausible_web/live/installation.ex b/lib/plausible_web/live/installation.ex
index edf0643bd9..8427e203d2 100644
--- a/lib/plausible_web/live/installation.ex
+++ b/lib/plausible_web/live/installation.ex
@@ -40,50 +40,47 @@ defmodule PlausibleWeb.Live.Installation do
:viewer
])
- socket =
- if FunWithFlags.enabled?(:scriptv2, for: site) do
- redirect(socket, to: "/#{domain}/installationv2?flow=#{params["flow"]}")
- else
- socket
+ if FunWithFlags.enabled?(:scriptv2, for: site) do
+ {:ok, redirect(socket, to: "/#{domain}/installationv2?flow=#{params["flow"]}")}
+ else
+ flow = params["flow"]
+
+ tracker_script_configuration =
+ PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site)
+
+ installation_type = get_installation_type(flow, tracker_script_configuration, 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,
+ checks: [
+ Checks.FetchBody,
+ Checks.ScanBody
+ ],
+ report_to: self(),
+ async?: true,
+ slowdown: 0
+ )
end
- flow = params["flow"]
-
- tracker_script_configuration =
- PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site.id)
-
- installation_type = get_installation_type(flow, tracker_script_configuration, 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,
- checks: [
- Checks.FetchBody,
- Checks.ScanBody
- ],
- report_to: self(),
- async?: true,
- slowdown: 0
- )
+ {:ok,
+ assign(socket,
+ uri_params: Map.take(params, @valid_qs_params),
+ connected?: connected?(socket),
+ site: site,
+ site_created?: params["site_created"] == "true",
+ flow: flow,
+ installation_type: installation_type,
+ initial_installation_type: installation_type,
+ domain: domain,
+ config: config
+ )}
end
-
- {:ok,
- assign(socket,
- uri_params: Map.take(params, @valid_qs_params),
- connected?: connected?(socket),
- site: site,
- site_created?: params["site_created"] == "true",
- flow: flow,
- installation_type: installation_type,
- initial_installation_type: installation_type,
- domain: domain,
- config: config
- )}
end
def handle_info({:verification_end, %State{} = state}, socket) do
diff --git a/lib/plausible_web/live/installationv2.ex b/lib/plausible_web/live/installationv2.ex
index 5f9a4e917d..5f7863d75e 100644
--- a/lib/plausible_web/live/installationv2.ex
+++ b/lib/plausible_web/live/installationv2.ex
@@ -19,19 +19,34 @@ defmodule PlausibleWeb.Live.InstallationV2 do
])
tracker_script_configuration =
- PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site.id)
+ PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site, %{
+ outbound_links: true,
+ form_submissions: true,
+ file_downloads: true,
+ installation_type: :manual
+ })
{:ok,
assign(socket,
site: site,
- tracker_script_configuration: tracker_script_configuration,
- flow: "provisioning",
- installation_type: params["type"] || "manual"
+ tracker_script_configuration_form:
+ to_form(
+ Plausible.Site.TrackerScriptConfiguration.installation_changeset(
+ tracker_script_configuration,
+ %{}
+ )
+ ),
+ flow: params["flow"] || "provisioning",
+ installation_type: get_installation_type(params, tracker_script_configuration)
)}
end
def handle_params(params, _url, socket) do
- {:noreply, assign(socket, installation_type: params["type"] || "manual")}
+ {:noreply,
+ assign(socket,
+ installation_type:
+ get_installation_type(params, socket.assigns.tracker_script_configuration_form.data)
+ )}
end
def render(assigns) do
@@ -55,32 +70,35 @@ defmodule PlausibleWeb.Live.InstallationV2 do
</.tab>
</div>
- <.manual_instructions
- :if={@installation_type == "manual"}
- tracker_script_configuration={@tracker_script_configuration}
- />
+ <.form for={@tracker_script_configuration_form} phx-submit="submit" class="mt-4">
+ <.input
+ type="hidden"
+ field={@tracker_script_configuration_form[:installation_type]}
+ value={@installation_type}
+ />
+ <.manual_instructions
+ :if={@installation_type == "manual"}
+ tracker_script_configuration_form={@tracker_script_configuration_form}
+ />
- <.wordpress_instructions :if={@installation_type == "wordpress"} />
- <.gtm_instructions :if={@installation_type == "gtm"} />
- <.npm_instructions :if={@installation_type == "npm"} />
+ <.wordpress_instructions :if={@installation_type == "wordpress"} flow={@flow} />
+ <.gtm_instructions :if={@installation_type == "gtm"} />
+ <.npm_instructions :if={@installation_type == "npm"} />
- <.button phx-click="submit" class="w-full mt-8">
- <%= if @flow == PlausibleWeb.Flows.domain_change() do %>
- I understand, I'll update my website
- <% else %>
+ <.button type="submit" class="w-full mt-8">
<%= if @flow == PlausibleWeb.Flows.review() do %>
Verify your installation
<% else %>
Start collecting data
<% end %>
- <% end %>
- </.button>
+ </.button>
+ </.form>
</.focus_box>
</div>
"""
end
- attr :tracker_script_configuration, :map, required: true
+ attr :tracker_script_configuration_form, :map, required: true
defp manual_instructions(assigns) do
~H"""
@@ -97,12 +115,127 @@ defmodule PlausibleWeb.Live.InstallationV2 do
Once done, click the button below to verify your installation.
</div>
- <div class="mt-8">
- <.snippet_form tracker_script_configuration={@tracker_script_configuration} />
+ <.snippet_form tracker_script_configuration={@tracker_script_configuration_form.data} />
+ <.h2 class="mt-8 text-sm font-medium">Optional measurements</.h2>
+ <.script_config_control
+ field={@tracker_script_configuration_form[: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_config_control
+ field={@tracker_script_configuration_form[: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_config_control
+ field={@tracker_script_configuration_form[:form_submissions]}
+ label="Form submissions"
+ tooltip="Automatically track form submissions. These count towards your billable pageviews."
+ learn_more="https://plausible.io/docs/form-submissions-tracking"
+ />
+
+ <.disclosure>
+ <.disclosure_button class="mt-4 flex items-center group">
+ <.h2 class="text-sm font-medium">Advanced options</.h2>
+ <Heroicons.chevron_down mini class="size-4 ml-1 mt-0.5 group-data-[open=true]:rotate-180" />
+ </.disclosure_button>
+ <.disclosure_panel>
+ <ul class="list-disc list-inside mt-2 space-y-2">
+ <.advanced_option
+ variant="tagged-events"
+ label="Manual tagging"
+ 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"
+ />
+ <.advanced_option
+ variant="404"
+ 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"
+ />
+ <.advanced_option
+ variant="hash"
+ label="Hashed page paths"
+ tooltip="Automatically track page paths that use a # in the URL."
+ learn_more="https://plausible.io/docs/hash-based-routing"
+ />
+ <.advanced_option
+ 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"
+ />
+ <.advanced_option
+ variant="revenue"
+ 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"
+ />
+ </ul>
+ </.disclosure_panel>
+ </.disclosure>
+ """
+ end
+
+ attr :field, :any, required: true
+ attr :label, :string, required: true
+ attr :tooltip, :string, required: true
+ attr :learn_more, :string, required: true
+
+ defp script_config_control(assigns) do
+ ~H"""
+ <div class="mt-2 p-1 text-sm">
+ <div class="flex items-center">
+ <.input mt?={false} field={@field} label={@label} type="checkbox" />
+ <div class="ml-2 collapse md:visible">
+ <.tooltip sticky?={false}>
+ <:tooltip_content>
+ {@tooltip}
+ <br /><br />Click to learn more.
+ </:tooltip_content>
+ <a href={@learn_more} target="_blank" rel="noopener noreferrer">
+ <Heroicons.information_circle class="text-indigo-700 dark:text-gray-500 w-5 h-5 hover:stroke-2" />
+ </a>
+ </.tooltip>
+ </div>
+ <div class="ml-2 visible md:invisible">
+ <a href={@learn_more} target="_blank" rel="noopener noreferrer">
+ <Heroicons.information_circle class="text-indigo-700 dark:text-gray-500 w-5 h-5 hover:stroke-2" />
+ </a>
+ </div>
+ </div>
</div>
"""
end
+ defp advanced_option(assigns) do
+ ~H"""
+ <li class="p-1 text-sm">
+ <div class="inline-flex items-center">
+ <div>{@label}</div>
+ <div class="ml-2 collapse md:visible">
+ <.tooltip sticky?={false}>
+ <:tooltip_content>
+ {@tooltip}
+ <br /><br />Click to learn more.
+ </:tooltip_content>
+ <a href={@learn_more} target="_blank" rel="noopener noreferrer">
+ <Heroicons.information_circle class="text-indigo-700 dark:text-gray-500 w-5 h-5 hover:stroke-2" />
+ </a>
+ </.tooltip>
+ </div>
+ <div class="ml-2 visible md:invisible">
+ <a href={@learn_more} target="_blank" rel="noopener noreferrer">
+ <Heroicons.information_circle class="text-indigo-700 dark:text-gray-500 w-5 h-5 hover:stroke-2" />
+ </a>
+ </div>
+ </div>
+ </li>
+ """
+ end
+
defp wordpress_instructions(assigns) do
~H"""
<.title class="mt-4">
@@ -180,34 +313,32 @@ defmodule PlausibleWeb.Live.InstallationV2 do
defp snippet_form(assigns) do
~H"""
- <form>
- <div class="relative">
- <textarea
- id="snippet"
- 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="8"
- readonly
- ><%= render_snippet(@tracker_script_configuration) %></textarea>
-
- <a
- onclick="var input = document.getElementById('snippet'); input.focus(); input.select(); document.execCommand('copy'); event.stopPropagation();"
- href="javascript:void(0)"
- class="absolute flex items-center text-xs font-medium text-indigo-600 no-underline hover:underline bottom-2 right-4 p-2 bg-white dark:bg-gray-900"
- >
- <Heroicons.document_duplicate class="pr-1 text-indigo-600 dark:text-indigo-500 w-5 h-5" />
- <span>
- COPY
- </span>
- </a>
- </div>
- </form>
+ <div class="relative">
+ <textarea
+ id="snippet"
+ 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="4"
+ readonly
+ ><%= render_snippet(@tracker_script_configuration) %></textarea>
+
+ <a
+ onclick="var input = document.getElementById('snippet'); input.focus(); input.select(); document.execCommand('copy'); event.stopPropagation();"
+ href="javascript:void(0)"
+ class="absolute flex items-center text-xs font-medium text-indigo-600 no-underline hover:underline bottom-2 right-4 p-2 bg-white dark:bg-gray-900"
+ >
+ <Heroicons.document_duplicate class="pr-1 text-indigo-600 dark:text-indigo-500 w-5 h-5" />
+ <span>
+ COPY
+ </span>
+ </a>
+ </div>
"""
end
- def handle_event("submit", _params, socket) do
+ def handle_event("submit", %{"tracker_script_configuration" => params}, socket) do
PlausibleWeb.Tracker.update_script_configuration(
socket.assigns.site,
- %{installation_type: socket.assigns.installation_type},
+ params,
:installation
)
@@ -296,4 +427,12 @@ defmodule PlausibleWeb.Live.InstallationV2 do
</svg>
"""
end
+
+ defp get_installation_type(params, tracker_script_configuration) do
+ if params["type"] do
+ params["type"]
+ else
+ Atom.to_string(tracker_script_configuration.installation_type)
+ end
+ end
end
diff --git a/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex b/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex
index 0579002ccc..dc6d8d8152 100644
--- a/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex
+++ b/lib/plausible_web/plugins/api/controllers/tracker_script_configuration.ex
@@ -18,7 +18,7 @@ defmodule PlausibleWeb.Plugins.API.Controllers.TrackerScriptConfiguration do
@spec get(Plug.Conn.t(), %{}) :: Plug.Conn.t()
def get(conn, _params) do
site = conn.assigns.authorized_site
- configuration = PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site.id)
+ configuration = PlausibleWeb.Tracker.get_or_create_tracker_script_configuration!(site)
conn
|> put_view(Views.TrackerScriptConfiguration)
diff --git a/lib/plausible_web/tracker.ex b/lib/plausible_web/tracker.ex
index c351eab8c5..e7a7fde9aa 100644
--- a/lib/plausible_web/tracker.ex
+++ b/lib/plausible_web/tracker.ex
@@ -46,7 +46,7 @@ defmodule PlausibleWeb.Tracker do
def update_script_configuration(site, config_update, changeset_type) do
{:ok, updated_config} =
Repo.transaction(fn ->
- original_config = get_or_create_tracker_script_configuration!(site.id)
+ original_config = get_or_create_tracker_script_configuration!(site)
changeset = changeset(original_config, config_update, changeset_type)
updated_config = Repo.update!(changeset)
@@ -71,21 +71,36 @@ defmodule PlausibleWeb.Tracker do
updated_config
end
- def get_or_create_tracker_script_configuration!(site_id) do
- configuration = Repo.get_by(TrackerScriptConfiguration, site_id: site_id)
+ def get_or_create_tracker_script_configuration!(site, params \\ %{}) do
+ configuration = Repo.get_by(TrackerScriptConfiguration, site_id: site.id)
if configuration do
configuration
else
- %TrackerScriptConfiguration{site_id: site_id}
- |> Repo.insert!()
+ {:ok, created_config} =
+ Repo.transaction(fn ->
+ created_config =
+ TrackerScriptConfiguration.installation_changeset(
+ %TrackerScriptConfiguration{site_id: site.id},
+ params
+ )
+ |> Repo.insert!()
+
+ sync_goals(site, %{}, created_config)
+
+ created_config
+ end)
+
+ created_config
end
end
# Sync plausible goals with the updated script config
defp sync_goals(site, original_config, updated_config) do
[:track_404_pages, :outbound_links, :file_downloads]
- |> Enum.map(fn key -> {key, Map.get(original_config, key), Map.get(updated_config, key)} end)
+ |> Enum.map(fn key ->
+ {key, Map.get(original_config, key, false), Map.get(updated_config, key, false)}
+ end)
|> Enum.each(fn
{:track_404_pages, false, true} -> Plausible.Goals.create_404(site)
{:track_404_pages, true, false} -> Plausible.Goals.delete_404(site)
diff --git a/test/plausible_web/live/customer_support/teams_test.exs b/test/plausible_web/live/customer_support/teams_test.exs
index ee8babe720..381b101961 100644
--- a/test/plausible_web/live/customer_support/teams_test.exs
+++ b/test/plausible_web/live/customer_support/teams_test.exs
@@ -130,19 +130,19 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
"site_limit" => "1000",
"team_member_limit" => "30",
"hourly_api_request_limit" => "1000",
- "features[]" => [
- "false",
- "false",
- "false",
- "teams",
- "false",
- "shared_links",
- "false",
- "false",
- "false",
- "false",
- "sites_api"
- ]
+ "features[]" => %{
+ "stats_api" => "false",
+ "outbound_links" => "false",
+ "form_submissions" => "false",
+ "file_downloads" => "false",
+ "funnels" => "false",
+ "props" => "false",
+ "shared_segments" => "false",
+ "revenue_goals" => "false",
+ "site_segments" => "false",
+ "shared_links" => "true",
+ "sites_api" => "true"
+ }
}
})
@@ -163,18 +163,19 @@ defmodule PlausibleWeb.Live.CustomerSupport.TeamsTest do
"site_limit" => "1000",
"team_member_limit" => "30",
"hourly_api_request_limit" => "1000",
- "features[]" => [
- "false",
- "false",
- "false",
- "false",
- "shared_links",
- "false",
- "false",
- "false",
- "false",
- "sites_api"
- ]
+ "features[]" => %{
+ "stats_api" => "false",
+ "outbound_links" => "false",
+ "form_submissions" => "false",
+ "file_downloads" => "false",
+ "funnels" => "false",
+ "props" => "false",
+ "shared_segments" => "false",
+ "revenue_goals" => "false",
+ "site_segments" => "false",
+ "shared_links" => "true",
+ "sites_api" => "true"
+ }
}
})
diff --git a/test/plausible_web/tracker_test.exs b/test/plausible_web/tracker_test.exs
index 92a1bfefce..c4b4f41aeb 100644
--- a/test/plausible_web/tracker_test.exs
+++ b/test/plausible_web/tracker_test.exs
@@ -34,6 +34,34 @@ defmodule PlausibleWeb.TrackerTest do
}
end
+ test "can create config with params" do
+ site = new_site()
+
+ tracker_script_configuration =
+ Tracker.get_or_create_tracker_script_configuration!(site, %{
+ outbound_links: true,
+ form_submissions: true,
+ installation_type: :manual
+ })
+
+ assert tracker_script_configuration.outbound_links
+ assert tracker_script_configuration.form_submissions
+ refute tracker_script_configuration.file_downloads
+ assert tracker_script_configuration.installation_type == :manual
+ end
+
+ test "goals are created when config is created" do
+ site = new_site()
+
+ Tracker.get_or_create_tracker_script_configuration!(site, %{
+ outbound_links: true,
+ installation_type: :manual
+ })
+
+ assert Repo.get_by(Plausible.Goal, site_id: site.id, display_name: "Outbound Link: Click")
+ refute Repo.get_by(Plausible.Goal, site_id: site.id, display_name: "File Download")
+ end
+
test "can update config" do
site = new_site()
tracker_script_configuration = create_config(site)
@@ -57,7 +85,7 @@ defmodule PlausibleWeb.TrackerTest do
site = new_site()
tracker_script_configuration =
- Tracker.get_or_create_tracker_script_configuration!(site.id)
+ Tracker.get_or_create_tracker_script_configuration!(site)
Tracker.update_script_configuration(
site,
@@ -75,7 +103,7 @@ defmodule PlausibleWeb.TrackerTest do
site = new_site()
tracker_script_configuration =
- Tracker.get_or_create_tracker_script_configuration!(site.id)
+ Tracker.get_or_create_tracker_script_configuration!(site)
Tracker.update_script_configuration(site, %{installation_type: :wordpress}, :installation)