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
Tracker: Send `engagement` events on visibilitychange (if pageleave extension) (#5023)
Karl-Aksel Puulmann334f8bbdfce0
diff --git a/lib/plausible/ingestion/request.ex b/lib/plausible/ingestion/request.exindex a04b682304..b0ec08147b 100644--- a/lib/plausible/ingestion/request.ex+++ b/lib/plausible/ingestion/request.ex@@ -250,7 +250,7 @@ defmodule Plausible.Ingestion.Request doenddefp put_scroll_depth(changeset, %{} = request_body) do- if Changeset.get_field(changeset, :event_name) == "pageleave" do+ if Changeset.get_field(changeset, :event_name) in ["pageleave", "engagement"] doscroll_depth =case request_body["sd"] dosd when is_integer(sd) and sd >= 0 and sd <= 100 -> sddiff --git a/lib/plausible/session/cache_store.ex b/lib/plausible/session/cache_store.exindex 9ade82ea24..98d5595ead 100644--- a/lib/plausible/session/cache_store.ex+++ b/lib/plausible/session/cache_store.ex@@ -10,7 +10,8 @@ defmodule Plausible.Session.CacheStore dodef on_event(event, session_attributes, prev_user_id, buffer_insert \\ &WriteBuffer.insert/1)- def on_event(%{name: "pageleave"} = event, _, prev_user_id, _) do+ def on_event(%{name: name} = event, _, prev_user_id, _)+ when name in ["pageleave", "engagement"] do# The `pageleave` event is currently experimental. In a real use case we would# probably want to update the session as well (e.g. `is_bounce` or `duration`).diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exsindex 27599c68f6..c4d0e52b23 100644--- a/test/plausible_web/controllers/api/external_controller_test.exs+++ b/test/plausible_web/controllers/api/external_controller_test.exs@@ -1335,6 +1335,15 @@ defmodule PlausibleWeb.Api.ExternalControllerTest doassert pageleave.scroll_depth == 255end++ test "ingests valid scroll_depth for a engagement event", %{conn: conn, site: site} do+ post(conn, "/api/event", %{n: "pageview", u: "https://test.com", d: site.domain})+ post(conn, "/api/event", %{n: "engagement", u: "https://test.com", d: site.domain, sd: 25})++ event = get_events(site) |> Enum.find(&(&1.name == "engagement"))++ assert event.scroll_depth == 25+ endenddescribe "acquisition channel tests" dodiff --git a/tracker/src/plausible.js b/tracker/src/plausible.jsindex 7db08cb9fb..80ff4eca9c 100644--- a/tracker/src/plausible.js+++ b/tracker/src/plausible.js@@ -41,6 +41,7 @@var currentPageLeaveIgnoredvar currentPageLeaveURL = location.hrefvar currentPageLeaveProps = {}+ var currentPageMaxEngagementScrollDepth = -1// Multiple pageviews might be sent by the same script when the page// uses client-side routing (e.g. hash or history-based). This flag@@ -86,6 +87,7 @@currentDocumentHeight = getDocumentHeight()if (++count === 15) {clearInterval(interval)}}, 200)+})document.addEventListener('scroll', function() {@@ -98,12 +100,27 @@})function triggerPageLeave() {- if (pageLeaveSending || currentPageLeaveIgnored) {return}- pageLeaveSending = true- setTimeout(function () {pageLeaveSending = false}, 500)+ if (!pageLeaveSending && !currentPageLeaveIgnored) {+ pageLeaveSending = true+ setTimeout(function () {pageLeaveSending = false}, 500)+ triggerEngagementEvent('pageleave')+ }+ }++ function triggerEngagement() {+ // Avoid sending redundant engagement events if user has not scrolled the page+ // Note that `currentPageMaxEngagementScrollDepth` default of -1 ensures that at least one+ // engagement event is sent+ if (!currentPageLeaveIgnored && currentPageMaxEngagementScrollDepth < maxScrollDepthPx) {+ currentPageMaxEngagementScrollDepth = maxScrollDepthPx+ triggerEngagementEvent('engagement')+ }+ }++ function triggerEngagementEvent(name) {var payload = {- n: 'pageleave',+ n: name,sd: Math.round((maxScrollDepthPx / currentDocumentHeight) * 100),d: dataDomain,u: currentPageLeaveURL,@@ -120,6 +137,13 @@function registerPageLeaveListener() {if (!listeningPageLeave) {window.addEventListener('pagehide', triggerPageLeave)++ // Only register visibilitychange listener only after initial page load and pageview+ document.addEventListener('visibilitychange', function() {+ if (document.visibilityState === 'hidden') {+ triggerEngagement()+ }+ })listeningPageLeave = true}}@@ -215,6 +239,7 @@currentPageLeaveIgnored = falsecurrentPageLeaveURL = payload.ucurrentPageLeaveProps = payload.p+ currentPageMaxEngagementScrollDepth = -1registerPageLeaveListener()}{{/if}}@@ -270,7 +295,8 @@{{#if pageleave}}if (isSPANavigation && listeningPageLeave) {- triggerPageLeave();+ triggerPageLeave()+ triggerEngagement()currentDocumentHeight = getDocumentHeight()maxScrollDepthPx = getCurrentScrollDepthPx()}diff --git a/tracker/test/pageleave.spec.js b/tracker/test/pageleave.spec.jsindex b6cba45cd8..81e3e46c7b 100644--- a/tracker/test/pageleave.spec.js+++ b/tracker/test/pageleave.spec.js@@ -1,89 +1,105 @@/* eslint-disable playwright/no-skipped-test */-const { expectPlausibleInAction, pageleaveCooldown } = require('./support/test-utils')+const { expectPlausibleInAction, pageleaveCooldown, ignoreEngagementRequests, ignorePageleaveRequests } = require('./support/test-utils')const { test } = require('@playwright/test')const { LOCAL_SERVER_ADDR } = require('./support/server')-test.describe('pageleave extension', () => {+test.describe('pageleave extension (pageleave events)', () => {test.skip(({browserName}) => browserName === 'webkit', 'Not testable on Webkit')+ sharedTests('pageleave', ignoreEngagementRequests)+})++test.describe('pageleave extension (engagement events)', () => {+ test.skip(({browserName}) => browserName === 'webkit', 'Not testable on Webkit')++ sharedTests('engagement', ignorePageleaveRequests)+})++function sharedTests(expectedEvent, ignoreRequests) {test('sends a pageleave when navigating to the next page', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/pageleave.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/pageleave.html`}]+ expectedRequests: [{n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/pageleave.html`}],+ shouldIgnoreRequest: ignoreRequests})})- test('sends pageleave and pageview on hash-based SPA navigation', async ({ page }) => {+ test('sends an event and a pageview on hash-based SPA navigation', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/pageleave-hash.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})await expectPlausibleInAction(page, {action: () => page.click('#hash-nav'),expectedRequests: [- {n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/pageleave-hash.html`},+ {n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/pageleave-hash.html`},{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/pageleave-hash.html#some-hash`}- ]+ ],+ shouldIgnoreRequest: ignoreRequests})})- test('sends pageleave and pageview on history-based SPA navigation', async ({ page }) => {+ test('sends an event and a pageview on history-based SPA navigation', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/pageleave.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})await expectPlausibleInAction(page, {action: () => page.click('#history-nav'),expectedRequests: [- {n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/pageleave.html`},+ {n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/pageleave.html`},{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/another-page`}- ]+ ],+ shouldIgnoreRequest: ignoreRequests})})- test('sends pageleave with the manually overridden URL', async ({ page }) => {+ test('sends an event with the manually overridden URL', async ({ page }) => {await page.goto('/pageleave-manual.html')await expectPlausibleInAction(page, {action: () => page.click('#pageview-trigger-custom-url'),- expectedRequests: [{n: 'pageview', u: 'https://example.com/custom/location'}]+ expectedRequests: [{n: 'pageview', u: 'https://example.com/custom/location'}],})await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', u: 'https://example.com/custom/location'}]+ expectedRequests: [{n: expectedEvent, u: 'https://example.com/custom/location'}],+ shouldIgnoreRequest: ignoreRequests})})- test('does not send pageleave when pageview was not sent in manual mode', async ({ page }) => {+ test('does not send an event when pageview was not sent in manual mode', async ({ page }) => {await page.goto('/pageleave-manual.html')await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- refutedRequests: [{n: 'pageleave'}],+ refutedRequests: [{n: expectedEvent}],+ shouldIgnoreRequest: ignoreRequests})})- test('script.exclusions.hash.pageleave.js sends pageleave only from URLs where a pageview was sent', async ({ page }) => {+ test('script.exclusions.hash.pageleave.js sends an event only from URLs where a pageview was sent', async ({ page }) => {const pageBaseURL = `${LOCAL_SERVER_ADDR}/pageleave-hash-exclusions.html`await expectPlausibleInAction(page, {action: () => page.goto('/pageleave-hash-exclusions.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})// After the initial pageview is sent, navigate to ignored page ->// pageleave event is sent from the initial page URLawait expectPlausibleInAction(page, {action: () => page.click('#ignored-hash-link'),- expectedRequests: [{n: 'pageleave', u: pageBaseURL, h: 1}]+ expectedRequests: [{n: expectedEvent, u: pageBaseURL, h: 1}],+ shouldIgnoreRequest: ignoreRequests})await pageleaveCooldown(page)@@ -93,7 +109,8 @@ test.describe('pageleave extension', () => {await expectPlausibleInAction(page, {action: () => page.click('#hash-link-1'),expectedRequests: [{n: 'pageview', u: `${pageBaseURL}#hash1`, h: 1}],- refutedRequests: [{n: 'pageleave'}]+ refutedRequests: [{n: expectedEvent}],+ shouldIgnoreRequest: ignoreRequests})await pageleaveCooldown(page)@@ -103,50 +120,54 @@ test.describe('pageleave extension', () => {await expectPlausibleInAction(page, {action: () => page.click('#hash-link-2'),expectedRequests: [- {n: 'pageleave', u: `${pageBaseURL}#hash1`, h: 1},+ {n: expectedEvent, u: `${pageBaseURL}#hash1`, h: 1},{n: 'pageview', u: `${pageBaseURL}#hash2`, h: 1}],+ shouldIgnoreRequest: ignoreRequests})})- test('sends pageleave with the same props as pageview (manual extension)', async ({ page }) => {+ test('sends an event with the same props as pageview (manual extension)', async ({ page }) => {await page.goto('/pageleave-manual.html')await expectPlausibleInAction(page, {action: () => page.click('#pageview-trigger-custom-props'),- expectedRequests: [{n: 'pageview', p: {author: 'John'}}]+ expectedRequests: [{n: 'pageview', p: {author: 'John'}}],})await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', p: {author: 'John'}}]+ expectedRequests: [{n: expectedEvent, p: {author: 'John'}}],+ shouldIgnoreRequest: ignoreRequests})})- test('sends pageleave with the same props as pageview (pageview-props extension)', async ({ page }) => {+ test('sends an event with the same props as pageview (pageview-props extension)', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/pageleave-pageview-props.html'),- expectedRequests: [{n: 'pageview', p: {author: 'John'}}]+ expectedRequests: [{n: 'pageview', p: {author: 'John'}}],})await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', p: {author: 'John'}}]+ expectedRequests: [{n: expectedEvent, p: {author: 'John'}}],+ shouldIgnoreRequest: ignoreRequests})})- test('sends pageleave with the same props as pageview (hash navigation / pageview-props extension)', async ({ page }) => {+ test('sends an event with the same props as pageview (hash navigation / pageview-props extension)', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/pageleave-hash-pageview-props.html'),- expectedRequests: [{n: 'pageview', p: {}}]+ expectedRequests: [{n: 'pageview', p: {}}],})await expectPlausibleInAction(page, {action: () => page.click('#john-post'),expectedRequests: [- {n: 'pageleave', p: {}},+ {n: expectedEvent, p: {}},{n: 'pageview', p: {author: 'john'}}- ]+ ],+ shouldIgnoreRequest: ignoreRequests})await pageleaveCooldown(page)@@ -154,9 +175,10 @@ test.describe('pageleave extension', () => {await expectPlausibleInAction(page, {action: () => page.click('#jane-post'),expectedRequests: [- {n: 'pageleave', p: {author: 'john'}},+ {n: expectedEvent, p: {author: 'john'}},{n: 'pageview', p: {author: 'jane'}}- ]+ ],+ shouldIgnoreRequest: ignoreRequests})await pageleaveCooldown(page)@@ -164,16 +186,17 @@ test.describe('pageleave extension', () => {await expectPlausibleInAction(page, {action: () => page.click('#home'),expectedRequests: [- {n: 'pageleave', p: {author: 'jane'}},+ {n: expectedEvent, p: {author: 'jane'}},{n: 'pageview', p: {}}- ]+ ],+ shouldIgnoreRequest: ignoreRequests})})- test('sends a pageleave when plausible API is slow and user navigates away before response is received', async ({ page }) => {+ test('sends an event when plausible API is slow and user navigates away before response is received', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/pageleave.html'),- expectedRequests: [{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/pageleave.html`}]+ expectedRequests: [{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/pageleave.html`}],})await expectPlausibleInAction(page, {@@ -182,12 +205,13 @@ test.describe('pageleave extension', () => {await page.click('#back-button-trigger')},expectedRequests: [- {n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/pageleave.html`},+ {n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/pageleave.html`},{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/pageleave-pageview-props.html`, p: {author: 'John'}},- {n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/pageleave-pageview-props.html`, p: {author: 'John'}},+ {n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/pageleave-pageview-props.html`, p: {author: 'John'}},{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/pageleave.html`}],- responseDelay: 1000+ responseDelay: 1000,+ shouldIgnoreRequest: ignoreRequests})})-})+}diff --git a/tracker/test/scroll-depth.spec.js b/tracker/test/scroll-depth.spec.jsindex edea3c3237..b0e785db52 100644--- a/tracker/test/scroll-depth.spec.js+++ b/tracker/test/scroll-depth.spec.js@@ -1,15 +1,65 @@/* eslint-disable playwright/no-skipped-test */-const { pageleaveCooldown, expectPlausibleInAction } = require('./support/test-utils')+const { pageleaveCooldown, expectPlausibleInAction, ignoreEngagementRequests, ignorePageleaveRequests, hideCurrentTab, hideAndShowCurrentTab } = require('./support/test-utils')const { test } = require('@playwright/test')const { LOCAL_SERVER_ADDR } = require('./support/server')-test.describe('scroll depth', () => {+test.describe('scroll depth (pageleave events)', () => {test.skip(({browserName}) => browserName === 'webkit', 'Not testable on Webkit')+ sharedTests('pageleave', ignoreEngagementRequests)+})++test.describe('scroll depth (engagement events)', () => {+ test.skip(({browserName}) => browserName === 'webkit', 'Not testable on Webkit')++ sharedTests('engagement', ignorePageleaveRequests)++ test('sends scroll depth when minimizing the tab', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => page.goto('/scroll-depth.html'),+ expectedRequests: [{n: 'pageview'}],+ })++ await page.evaluate(() => window.scrollBy(0, 300))+ await page.waitForTimeout(100) // Wait for the scroll event to be processed++ await expectPlausibleInAction(page, {+ action: () => hideCurrentTab(page),+ expectedRequests: [{n: 'engagement', u: `${LOCAL_SERVER_ADDR}/scroll-depth.html`, sd: 20}],+ })+ })++ test('re-sends engagement events only when user has scrolled in-between', async ({ page, context }) => {+ await expectPlausibleInAction(page, {+ action: async () => {+ await page.goto('/scroll-depth.html')+ await hideAndShowCurrentTab(page)+ },+ expectedRequests: [+ {n: 'pageview'},+ {n: 'engagement', u: `${LOCAL_SERVER_ADDR}/scroll-depth.html`, sd: 14}+ ],+ })++ await expectPlausibleInAction(page, {+ action: () => hideAndShowCurrentTab(page),+ expectedRequests: [],+ })++ await page.evaluate(() => window.scrollBy(0, 300))++ await expectPlausibleInAction(page, {+ action: () => hideCurrentTab(page),+ expectedRequests: [{n: 'engagement', u: `${LOCAL_SERVER_ADDR}/scroll-depth.html`, sd: 20}],+ })+ })+})++function sharedTests(expectedEvent, ignoreRequests) {test('sends scroll_depth in the pageleave payload when navigating to the next page', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/scroll-depth.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})await page.evaluate(() => window.scrollBy(0, 300))@@ -17,14 +67,16 @@ test.describe('scroll depth', () => {await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/scroll-depth.html`, sd: 20}]+ expectedRequests: [{n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/scroll-depth.html`, sd: 20}],+ shouldIgnoreRequest: ignoreRequests})})test('sends scroll depth on hash navigation', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/scroll-depth-hash.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],+ shouldIgnoreRequest: ignoreEngagementRequests})await expectPlausibleInAction(page, {@@ -32,7 +84,8 @@ test.describe('scroll depth', () => {expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/scroll-depth-hash.html`, sd: 100},{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/scroll-depth-hash.html#about`}- ]+ ],+ shouldIgnoreRequest: ignoreEngagementRequests})await pageleaveCooldown(page)@@ -42,14 +95,15 @@ test.describe('scroll depth', () => {expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/scroll-depth-hash.html#about`, sd: 34},{n: 'pageview', u: `${LOCAL_SERVER_ADDR}/scroll-depth-hash.html#home`}- ]+ ],+ shouldIgnoreRequest: ignoreEngagementRequests})})test('document height gets reevaluated after window load', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/scroll-depth-slow-window-load.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})// Wait for the image to be loaded@@ -59,27 +113,29 @@ test.describe('scroll depth', () => {await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/scroll-depth-slow-window-load.html`, sd: 24}]+ expectedRequests: [{n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/scroll-depth-slow-window-load.html`, sd: 24}],+ shouldIgnoreRequest: ignoreRequests})})test('dynamically loaded content affects documentHeight', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/scroll-depth-dynamic-content-load.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})-+// The link appears dynamically after 500ms.await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/scroll-depth-dynamic-content-load.html`, sd: 14}]+ expectedRequests: [{n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/scroll-depth-dynamic-content-load.html`, sd: 14}],+ shouldIgnoreRequest: ignoreRequests})})test('document height gets reevaluated on scroll', async ({ page }) => {await expectPlausibleInAction(page, {action: () => page.goto('/scroll-depth-content-onscroll.html'),- expectedRequests: [{n: 'pageview'}]+ expectedRequests: [{n: 'pageview'}],})// During the first 3 seconds, the script periodically updates document height@@ -89,7 +145,7 @@ test.describe('scroll depth', () => {// scroll to the bottom of the pageawait page.evaluate(() => window.scrollBy(0, document.body.scrollHeight))-+// Wait until documentHeight gets increased by the fixture JSawait page.waitForSelector('#more-content')@@ -97,7 +153,8 @@ test.describe('scroll depth', () => {await expectPlausibleInAction(page, {action: () => page.click('#navigate-away'),- expectedRequests: [{n: 'pageleave', u: `${LOCAL_SERVER_ADDR}/scroll-depth-content-onscroll.html`, sd: 80}]+ expectedRequests: [{n: expectedEvent, u: `${LOCAL_SERVER_ADDR}/scroll-depth-content-onscroll.html`, sd: 80}],+ shouldIgnoreRequest: ignoreRequests})})-})\ No newline at end of file+}diff --git a/tracker/test/support/test-utils.js b/tracker/test/support/test-utils.jsindex d0220237f2..26f0daf26c 100644--- a/tracker/test/support/test-utils.js+++ b/tracker/test/support/test-utils.js@@ -32,13 +32,16 @@ exports.metaKey = function() {// Mocks a specified number of HTTP requests with given path. Returns a promise that resolves to a// list of requests as soon as the specified number of requests is made, or 3 seconds has passed.-const mockManyRequests = function({ page, path, numberOfRequests, responseDelay }) {+const mockManyRequests = function({ page, path, numberOfRequests, responseDelay, shouldIgnoreRequest }) {return new Promise((resolve, _reject) => {let requestList = []const requestTimeoutTimer = setTimeout(() => resolve(requestList), 3000)page.route(path, async (route, request) => {- requestList.push(request)+ const postData = request.postDataJSON()+ if (!shouldIgnoreRequest || !shouldIgnoreRequest(postData)) {+ requestList.push(postData)+ }if (responseDelay) {await delay(responseDelay)}@@ -85,7 +88,8 @@ exports.expectPlausibleInAction = async function (page, {refutedRequests = [],awaitedRequestCount,expectedRequestCount,- responseDelay+ responseDelay,+ shouldIgnoreRequest}) {const requestsToExpect = expectedRequestCount ? expectedRequestCount : expectedRequests.lengthconst requestsToAwait = awaitedRequestCount ? awaitedRequestCount : requestsToExpect + refutedRequests.length@@ -93,11 +97,12 @@ exports.expectPlausibleInAction = async function (page, {const plausibleRequestMockList = mockManyRequests({page,path: '/api/event',- numberOfRequests: requestsToAwait,- responseDelay: responseDelay+ responseDelay,+ shouldIgnoreRequest,+ numberOfRequests: requestsToAwait})await action()- const requestBodies = (await plausibleRequestMockList).map(r => r.postDataJSON())+ const requestBodies = await plausibleRequestMockListconst expectedButNotFoundBodySubsets = []@@ -128,6 +133,35 @@ exports.expectPlausibleInAction = async function (page, {expect(requestBodies.length).toBe(requestsToExpect)}+exports.ignoreEngagementRequests = function(requestPostData) {+ return requestPostData.n === 'engagement'+}++exports.ignorePageleaveRequests = function(requestPostData) {+ return requestPostData.n === 'pageleave'+}++async function toggleTabVisibility(page, hide) {+ await page.evaluate((hide) => {+ Object.defineProperty(document, 'visibilityState', { value: hide ? 'hidden' : 'visible', writable: true })+ Object.defineProperty(document, 'hidden', { value: hide, writable: true })+ document.dispatchEvent(new Event('visibilitychange'))+ }, hide)+}++exports.hideCurrentTab = async function(page) {+ return toggleTabVisibility(page, true)+}++exports.showCurrentTab = async function(page) {+ return toggleTabVisibility(page, false)+}++exports.hideAndShowCurrentTab = async function(page) {+ await exports.hideCurrentTab(page)+ await exports.showCurrentTab(page)+}+function includesSubset(body, subset) {return Object.keys(subset).every((key) => {if (typeof subset[key] === 'object') {