Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Allow marking custom events as non-interactive (#5295)
Karl-Aksel Puulmann2114c6232d04
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 62df8f4b91..2c89d87c62 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -3,11 +3,16 @@ All notable changes to this project will be documented in this file.## Unreleased-### Added+### Added++- Custom events can now be marked as non-interactive in events API and tracker script. Events marked as non-interactive are not counted towards bounce rate.+-### Removed+### Removed++### Changed-### Changed+- A session is now marked as a bounce if it has less than 2 pageviews and no interactive custom events.### Fixeddiff --git a/lib/plausible/clickhouse_event_v2.ex b/lib/plausible/clickhouse_event_v2.exindex 3366195576..a8c9d2974f 100644--- a/lib/plausible/clickhouse_event_v2.ex+++ b/lib/plausible/clickhouse_event_v2.ex@@ -47,6 +47,9 @@ defmodule Plausible.ClickhouseEventV2 dofield :browser_version, Ch, type: "LowCardinality(String)"field :acquisition_channel, Ch, type: "LowCardinality(String)", writable: :never++ # Virtual field used during event processing+ field :interactive?, :boolean, default: true, virtual: true, writable: :neverenddef new(attrs) dodiff --git a/lib/plausible/ingestion/event.ex b/lib/plausible/ingestion/event.exindex bed0d239bc..5453a247be 100644--- a/lib/plausible/ingestion/event.ex+++ b/lib/plausible/ingestion/event.ex@@ -266,7 +266,8 @@ defmodule Plausible.Ingestion.Event dohostname: event.request.hostname,pathname: event.request.pathname,scroll_depth: event.request.scroll_depth,- engagement_time: event.request.engagement_time+ engagement_time: event.request.engagement_time,+ interactive?: event.request.interactive?})enddiff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.exindex c61a68ad5e..95831febe0 100644--- a/lib/plausible/ingestion/request.ex+++ b/lib/plausible/ingestion/request.ex@@ -55,6 +55,7 @@ defmodule Plausible.Ingestion.Request dofield :scroll_depth, :integerfield :engagement_time, :integerfield :tracker_script_version, :integer, default: 0+ field :interactive?, :boolean, default: trueon_ee dofield :revenue_source, :map@@ -95,6 +96,7 @@ defmodule Plausible.Ingestion.Request do|> put_pathname()|> put_query_params()|> put_revenue_source(request_body)+ |> put_interactive(request_body)|> put_tracker_script_version(request_body)|> map_domains(request_body)|> Changeset.validate_required([@@ -233,6 +235,16 @@ defmodule Plausible.Ingestion.Request do|> validate_props()end+ defp put_interactive(changeset, %{} = request_body) do+ case request_body["i"] || request_body["interactive"] do+ interactive? when is_boolean(interactive?) ->+ Changeset.put_change(changeset, :interactive?, interactive?)++ _ ->+ changeset+ end+ end+defp filter_bad_props({k, v}, acc) docond doEnum.any?([k, v], &(is_list(&1) or is_map(&1))) -> accdiff --git a/lib/plausible/ingestion/write_buffer.ex b/lib/plausible/ingestion/write_buffer.exindex 3947ee06aa..81eb9e6777 100644--- a/lib/plausible/ingestion/write_buffer.ex+++ b/lib/plausible/ingestion/write_buffer.ex@@ -150,5 +150,5 @@ defmodule Plausible.Ingestion.WriteBuffer do}end- defp fields_to_ignore(), do: [:acquisition_channel]+ defp fields_to_ignore(), do: [:acquisition_channel, :interactive?]enddiff --git a/lib/plausible/session/cache_store.ex b/lib/plausible/session/cache_store.exindex f115698f46..d7f6f3de65 100644--- a/lib/plausible/session/cache_store.ex+++ b/lib/plausible/session/cache_store.ex@@ -93,26 +93,31 @@ defmodule Plausible.Session.CacheStore doenddefp update_session(session, event) do+ pageview? = event.name == "pageview"+ pageviews = if(pageview?, do: session.pageviews + 1, else: session.pageviews)+%{session| timestamp: event.timestamp,entry_page:- if(session.entry_page == "" and event.name == "pageview",+ if(session.entry_page == "" and pageview?,do: event.pathname,else: session.entry_page),hostname:- if(event.name == "pageview" and session.hostname == "",+ if(pageview? and session.hostname == "",do: event.hostname,else: session.hostname),- exit_page: if(event.name == "pageview", do: event.pathname, else: session.exit_page),- exit_page_hostname:- if(event.name == "pageview", do: event.hostname, else: session.exit_page_hostname),- is_bounce: false,+ exit_page: if(pageview?, do: event.pathname, else: session.exit_page),+ exit_page_hostname: if(pageview?, do: event.hostname, else: session.exit_page_hostname),+ is_bounce:+ if(session.is_bounce,+ do: not (pageviews >= 2 or (event.interactive? and not pageview?)),+ else: session.is_bounce+ ),duration: NaiveDateTime.diff(event.timestamp, session.start) |> abs,- pageviews:- if(event.name == "pageview", do: session.pageviews + 1, else: session.pageviews),+ pageviews: pageviews,events: session.events + 1}end@@ -127,7 +132,7 @@ defmodule Plausible.Session.CacheStore doentry_page: if(event.name == "pageview", do: event.pathname, else: ""),exit_page: if(event.name == "pageview", do: event.pathname, else: ""),exit_page_hostname: if(event.name == "pageview", do: event.hostname, else: ""),- is_bounce: true,+ is_bounce: event.name == "pageview" or not event.interactive?,duration: 0,pageviews: if(event.name == "pageview", do: 1, else: 0),events: 1,diff --git a/test/plausible/ingestion/request_test.exs b/test/plausible/ingestion/request_test.exsindex 1abbebf9f7..17105a1986 100644--- a/test/plausible/ingestion/request_test.exs+++ b/test/plausible/ingestion/request_test.exs@@ -456,6 +456,22 @@ defmodule Plausible.Ingestion.RequestTest doassert {:error, _} = exceeding_read_limitend+ test "respects interactive parameter" do+ params = %{+ name: "pageview",+ domain: "dummy.site",+ url: "http://dummy.site/index.html",+ interactive: false+ }++ assert {:ok, request} =+ build_conn(:post, "/api/events", params)+ |> put_req_header("user-agent", "Mozilla")+ |> Request.build()++ refute request.interactive?+ end+@tag :ee_onlytest "encodable" doparams = %{@@ -494,7 +510,8 @@ defmodule Plausible.Ingestion.RequestTest do"ip_classification" => nil,"scroll_depth" => nil,"engagement_time" => nil,- "tracker_script_version" => 0+ "tracker_script_version" => 0,+ "interactive?" => true}assert %NaiveDateTime{} = NaiveDateTime.from_iso8601!(request["timestamp"])diff --git a/test/plausible/session/cache_store_test.exs b/test/plausible/session/cache_store_test.exsindex dd215b4442..7ca4aa7453 100644--- a/test/plausible/session/cache_store_test.exs+++ b/test/plausible/session/cache_store_test.exs@@ -552,8 +552,7 @@ defmodule Plausible.Session.CacheStoreTest do%{e | pathname: "/exit"}])- session_q = from s in Plausible.ClickhouseSessionV2, where: s.site_id == ^e.site_id- session = Plausible.ClickhouseRepo.one!(session_q, settings: [final: true])+ session = get_session(e.site_id, final: true)refute session.is_bounceassert session.entry_page == "/"@@ -562,6 +561,58 @@ defmodule Plausible.Session.CacheStoreTest doendend+ describe "bounce detection" do+ test "single pageview is bounce" do+ session = create_session([build(:pageview)])+ assert session.is_bounce+ end++ test "two pageviews is not a bounce" do+ session = create_session([build(:pageview), build(:pageview)])+ refute session.is_bounce+ end++ test "(implicitly interactive) custom event is not a bounce" do+ session = create_session([build(:event, name: "interactive")])+ refute session.is_bounce+ end++ test "non-interactive custom event is a bounce" do+ session = create_session([build(:event, name: "non-interactive", interactive?: false)])+ assert session.is_bounce+ end++ test "pageview followed by non-interactive custom event is a bounce" do+ session =+ create_session([+ build(:pageview),+ build(:event, name: "non-interactive", interactive?: false)+ ])++ assert session.is_bounce+ end++ test "non-interactive custom event followed by pageview is a bounce" do+ session =+ create_session([+ build(:event, name: "non-interactive", interactive?: false),+ build(:pageview)+ ])++ assert session.is_bounce+ end++ test "non-interactive custom event followed by interactive custom event is not a bounce" do+ session =+ create_session([+ build(:event, name: "non-interactive", interactive?: false),+ build(:event, name: "interactive")+ ])++ refute session.is_bounce+ end+ end+defp new_site_id() do[[site_id]] =Plausible.ClickhouseRepo.query!("select max(site_id) + rand() from sessions_v2 FINAL").rows@@ -574,13 +625,25 @@ defmodule Plausible.Session.CacheStoreTest doPlausible.Session.WriteBuffer.flush()end- defp get_session(site_id) do+ defp create_session(events) do+ event_params = %{site_id: new_site_id(), user_id: 1}++ events+ |> Enum.map(&Map.merge(&1, event_params))+ |> flush()++ get_session(event_params.site_id, final: true)+ end++ defp get_session(site_id, opts \\ []) dosession_q =from s in Plausible.ClickhouseSessionV2,where: s.site_id == ^site_id,- order_by: [desc: :timestamp],+ order_by: [desc: :events],limit: 1- Plausible.ClickhouseRepo.one!(session_q)+ settings = [final: Keyword.get(opts, :final, true)]++ Plausible.ClickhouseRepo.one!(session_q, settings: settings)endenddiff --git a/test/support/test_utils.ex b/test/support/test_utils.exindex 681e49636a..ad38e3edd5 100644--- a/test/support/test_utils.ex+++ b/test/support/test_utils.ex@@ -87,23 +87,6 @@ defmodule Plausible.TestUtils do{:ok, conn: conn}end- def create_pageviews(pageviews) do- pageviews =- Enum.map(pageviews, fn pageview ->- pageview =- pageview- |> Map.delete(:site)- |> Map.put(:site_id, pageview.site.id)-- Factory.build(:pageview, pageview)- |> Map.from_struct()- |> Map.drop([:__meta__, :acquisition_channel])- |> update_in([:timestamp], &to_naive_truncate/1)- end)-- Plausible.IngestRepo.insert_all(Plausible.ClickhouseEventV2, pageviews)- end-def log_in(%{user: user, conn: conn}) doconn =conndiff --git a/test/workers/send_email_report_test.exs b/test/workers/send_email_report_test.exsindex 05864e7a25..37fa29b362 100644--- a/test/workers/send_email_report_test.exs+++ b/test/workers/send_email_report_test.exs@@ -52,19 +52,19 @@ defmodule Plausible.Workers.SendEmailReportTest dosunday_before_last = Timex.shift(last_monday, minutes: -1)this_monday = Timex.beginning_of_week(now)- create_pageviews([+ populate_stats(site, [# Sunday before last, not counted- %{site: site, timestamp: Timezone.convert(sunday_before_last, "UTC")},+ build(:pageview, timestamp: Timezone.convert(sunday_before_last, "UTC")),# Sunday before last, not counted- %{site: site, timestamp: Timezone.convert(sunday_before_last, "UTC")},+ build(:pageview, timestamp: Timezone.convert(sunday_before_last, "UTC")),# Last monday, counted- %{site: site, timestamp: Timezone.convert(last_monday, "UTC")},+ build(:pageview, timestamp: Timezone.convert(last_monday, "UTC")),# Last sunday, counted- %{site: site, timestamp: Timezone.convert(last_sunday, "UTC")},+ build(:pageview, timestamp: Timezone.convert(last_sunday, "UTC")),# This monday, not counted- %{site: site, timestamp: Timezone.convert(this_monday, "UTC")},+ build(:pageview, timestamp: Timezone.convert(this_monday, "UTC")),# This monday, not counted- %{site: site, timestamp: Timezone.convert(this_monday, "UTC")}+ build(:pageview, timestamp: Timezone.convert(this_monday, "UTC"))])perform_job(SendEmailReport, %{"site_id" => site.id, "interval" => "weekly"})diff --git a/test/workers/send_site_setup_emails_test.exs b/test/workers/send_site_setup_emails_test.exsindex 0b018ff163..fbf614b3bb 100644--- a/test/workers/send_site_setup_emails_test.exs+++ b/test/workers/send_site_setup_emails_test.exs@@ -64,7 +64,10 @@ defmodule Plausible.Workers.SendSiteSetupEmailsTest dosubject: "Your Plausible setup: Waiting for the first page views")- create_pageviews([%{site: site}])+ populate_stats(site, [+ build(:pageview)+ ])+perform_job(SendSiteSetupEmails, %{})assert_email_delivered_with(diff --git a/tracker/package.json b/tracker/package.jsonindex d6d014025b..9145a69742 100644--- a/tracker/package.json+++ b/tracker/package.json@@ -1,5 +1,5 @@{- "tracker_script_version": 4,+ "tracker_script_version": 5,"scripts": {"deploy": "node compile.js","test": "npm run deploy && npx playwright test",diff --git a/tracker/src/plausible.js b/tracker/src/plausible.jsindex be8248ea27..fab0a7eb9f 100644--- a/tracker/src/plausible.js+++ b/tracker/src/plausible.js@@ -233,6 +233,9 @@if (options && options.props) {payload.p = options.props}+ if (options && options.interactive === false) {+ payload.i = false+ }{{#if revenue}}if (options && options.revenue) {payload.$ = options.revenuediff --git a/tracker/test/custom-event-edge-cases.spec.js b/tracker/test/custom-event-edge-cases.spec.jsindex 59325de3e8..efbb0a141f 100644--- a/tracker/test/custom-event-edge-cases.spec.js+++ b/tracker/test/custom-event-edge-cases.spec.js@@ -3,34 +3,43 @@ const { test } = require('@playwright/test')const { LOCAL_SERVER_ADDR } = require('./support/server')test.describe('script.file-downloads.outbound-links.tagged-events.js', () => {- test('sends only outbound link event when clicked link is both download and outbound', async ({ page }) => {- await page.goto('/custom-event-edge-case.html')- const downloadURL = await page.locator('#outbound-download-link').getAttribute('href')-- await expectPlausibleInAction(page, {- action: () => page.click('#outbound-download-link'),- expectedRequests: [{n: 'Outbound Link: Click', p: {url: downloadURL}}]- })+ test('sends only outbound link event when clicked link is both download and outbound', async ({ page }) => {+ await page.goto('/custom-event-edge-case.html')+ const downloadURL = await page.locator('#outbound-download-link').getAttribute('href')++ await expectPlausibleInAction(page, {+ action: () => page.click('#outbound-download-link'),+ expectedRequests: [{ n: 'Outbound Link: Click', p: { url: downloadURL } }]+ })+ })++ test('sends file download event when local download link clicked', async ({ page }) => {+ await page.goto('/custom-event-edge-case.html')+ const downloadURL = LOCAL_SERVER_ADDR + '/' + await page.locator('#local-download').getAttribute('href')++ await expectPlausibleInAction(page, {+ action: () => page.click('#local-download'),+ expectedRequests: [{ n: 'File Download', p: { url: downloadURL } }]})+ })- test('sends file download event when local download link clicked', async ({ page }) => {- await page.goto('/custom-event-edge-case.html')- const downloadURL = LOCAL_SERVER_ADDR + '/' + await page.locator('#local-download').getAttribute('href')+ test('sends only tagged event when clicked link is tagged + outbound + download', async ({ page }) => {+ await page.goto('/custom-event-edge-case.html')- await expectPlausibleInAction(page, {- action: () => page.click('#local-download'),- expectedRequests: [{n: 'File Download', p: {url: downloadURL}}]- })+ await expectPlausibleInAction(page, {+ action: () => page.click('#tagged-outbound-download-link'),+ expectedRequests: [{ n: 'Foo', p: { url: 'https://awesome.website.com/file.pdf' } }],+ awaitedRequestCount: 3,+ expectedRequestCount: 1})+ })- test('sends only tagged event when clicked link is tagged + outbound + download', async ({ page }) => {- await page.goto('/custom-event-edge-case.html')+ test('sends manual non-interactive custom event', async ({ page }) => {+ await page.goto('/custom-event-edge-case.html')- await expectPlausibleInAction(page, {- action: () => page.click('#tagged-outbound-download-link'),- expectedRequests: [{n: 'Foo', p: {url: 'https://awesome.website.com/file.pdf'}}],- awaitedRequestCount: 3,- expectedRequestCount: 1- })+ await expectPlausibleInAction(page, {+ action: () => page.click('#non-interactive-custom-event'),+ expectedRequests: [{ n: 'non-interactive custom event', i: false }]})+ })})diff --git a/tracker/test/fixtures/custom-event-edge-case.html b/tracker/test/fixtures/custom-event-edge-case.htmlindex 0461ce87cd..245f33e331 100644--- a/tracker/test/fixtures/custom-event-edge-case.html+++ b/tracker/test/fixtures/custom-event-edge-case.html@@ -16,6 +16,8 @@<a id="local-download" href="file.csv">Local download</a><a id="tagged-outbound-download-link" class="plausible-event-name=Foo" href="https://awesome.website.com/file.pdf">Outbound Download</a>++ <a id="non-interactive-custom-event" onclick="plausible('non-interactive custom event', { interactive: false })">Non-interactive custom event</a></body></html>