From db6cbc3ab710b2e6f5f3e26aeb7e31a1c61b3ea1 Mon Sep 17 00:00:00 2001 From: Sergey Semenov Date: Thu, 7 Nov 2024 10:33:20 +0100 Subject: [PATCH 1/2] feat: analyticsParams --- priv/tracker/js/plausible.live-view.js | 2 +- tracker/src/plausible.js | 2 +- tracker/test/live-view.spec.js | 13 +++++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/priv/tracker/js/plausible.live-view.js b/priv/tracker/js/plausible.live-view.js index f6de8a99ab1e..f62d43ddc415 100644 --- a/priv/tracker/js/plausible.live-view.js +++ b/priv/tracker/js/plausible.live-view.js @@ -1 +1 @@ -!function(){"use strict";var a=window.location,o=window.document,r=o.currentScript,s=r.getAttribute("data-api")||new URL(r.src).origin+"/api/event";function l(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function n(e,t){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(a.hostname)||"file:"===a.protocol)return l("localhost",t);if((window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)&&!window.__plausible)return l(null,t);try{if("true"===window.localStorage.plausible_ignore)return l("localStorage flag",t)}catch(e){}var i={},n=(i.n=e,i.u=a.href,i.d=r.getAttribute("data-domain"),i.r=o.referrer||null,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=t.props),new XMLHttpRequest);n.open("POST",s,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4===n.readyState&&t&&t.callback&&t.callback({status:n.status})}}var e=window.plausible&&window.plausible.q||[];window.plausible=n;for(var t,i=0;in("pageview",{u:e.detail.href})),["phx:page-loading-start","phx:page-loading-stop"].map(t=>{window.addEventListener(t,e=>n("phx-event",{props:{event:t,detail:new URLSearchParams(e.detail||{}).toString()}}))}),window.addEventListener("submit",e=>n("js-submit",{props:{dom_id:e.target.id,...Object.fromEntries(new FormData(e.target).entries())}})),window.liveSocket?window.liveSocket.socket.logger=(e,t,i)=>{"push"!==e||t.includes("phoenix heartbeat")||n("phx-push",{props:{msg:t,...i}})}:console&&console.error("No liveSocket initialized")}(); \ No newline at end of file +!function(){"use strict";var a=window.location,o=window.document,r=o.currentScript,s=r.getAttribute("data-api")||new URL(r.src).origin+"/api/event";function l(e,t){e&&console.warn("Ignoring Event: "+e),t&&t.callback&&t.callback()}function n(e,t){if(/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(a.hostname)||"file:"===a.protocol)return l("localhost",t);if((window._phantom||window.__nightmare||window.navigator.webdriver||window.Cypress)&&!window.__plausible)return l(null,t);try{if("true"===window.localStorage.plausible_ignore)return l("localStorage flag",t)}catch(e){}var i={},n=(i.n=e,i.u=a.href,i.d=r.getAttribute("data-domain"),i.r=o.referrer||null,t&&t.meta&&(i.m=JSON.stringify(t.meta)),t&&t.props&&(i.p=t.props),new XMLHttpRequest);n.open("POST",s,!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(i)),n.onreadystatechange=function(){4===n.readyState&&t&&t.callback&&t.callback({status:n.status})}}var e=window.plausible&&window.plausible.q||[];window.plausible=n;for(var t,i=0;in("pageview",{u:e.detail.href})),["phx:page-loading-start","phx:page-loading-stop"].map(t=>{window.addEventListener(t,e=>n("phx-event",{props:{event:t,detail:new URLSearchParams(e.detail||{}).toString()}}))}),window.addEventListener("submit",e=>n("js-submit",{props:{dom_id:e.target.id,...Object.fromEntries(new FormData(e.target).entries())}})),window.liveSocket?window.liveSocket.socket.logger=(e,t,i)=>{"push"!==e||t.includes("phoenix heartbeat")||n("phx-push",{props:{msg:t,...window.analyticsParams||{},...i}})}:console&&console.error("No liveSocket initialized")}(); \ No newline at end of file diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js index b328133b23e1..f42dd6e06918 100644 --- a/tracker/src/plausible.js +++ b/tracker/src/plausible.js @@ -182,7 +182,7 @@ if (window.liveSocket) window.liveSocket.socket.logger = (kind, msg, data) => { if ((kind === 'push') && !msg.includes("phoenix heartbeat")){ - trigger('phx-push', {props: {msg, ...data}}); + trigger('phx-push', {props: {msg, ...(window.analyticsParams || {}), ...data}}); } } else diff --git a/tracker/test/live-view.spec.js b/tracker/test/live-view.spec.js index 963ed0a26768..b1882472618c 100644 --- a/tracker/test/live-view.spec.js +++ b/tracker/test/live-view.spec.js @@ -10,10 +10,23 @@ test.describe('script.live-view.js events', () => { }); test('Sends phx-event', async ({ page }) => { + await page.evaluate(() => window.dispatchEvent(new CustomEvent("phx:page-loading-start", { }))) + expectCustomEvent(await plausibleRequestMock, 'phx-event', { }) + }); + + test('Sends phx-push', async ({ page }) => { await page.evaluate(() => window.liveSocket.socket.logger('push', '_message', { a: 1 })) expectCustomEvent(await plausibleRequestMock, 'phx-push', { a: 1 }) }); + test('Sends analyticsParams', async ({ page }) => { + await page.evaluate(() => { + window.analyticsParams = { b: 2 }; + window.liveSocket.socket.logger('push', '_message', { a: 1 }); + }) + expectCustomEvent(await plausibleRequestMock, 'phx-push', { a: 1, b: 2 }) + }); + test('Sends submit event', async ({ page }) => { await (await page.locator("#main-form-btn")).click() expectCustomEvent(await plausibleRequestMock, 'js-submit', { 'user[name]': "name", dom_id: "main-form" }) From 88870cdf5f6b20db64a9045138b5fe05de675ba7 Mon Sep 17 00:00:00 2001 From: Sergey Semenov Date: Thu, 7 Nov 2024 11:24:08 +0100 Subject: [PATCH 2/2] fix: bump tzdata version --- mix.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.lock b/mix.lock index 9474037a6057..71330195092e 100644 --- a/mix.lock +++ b/mix.lock @@ -145,7 +145,7 @@ "telemetry_registry": {:hex, :telemetry_registry, "0.3.1", "14a3319a7d9027bdbff7ebcacf1a438f5f5c903057b93aee484cca26f05bdcba", [:mix, :rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "6d0ca77b691cf854ed074b459a93b87f4c7f5512f8f7743c635ca83da81f939e"}, "timex": {:hex, :timex, "3.7.11", "bb95cb4eb1d06e27346325de506bcc6c30f9c6dea40d1ebe390b262fad1862d1", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.20", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "8b9024f7efbabaf9bd7aa04f65cf8dcd7c9818ca5737677c7b76acbc6a94d1aa"}, "tls_certificate_check": {:hex, :tls_certificate_check, "1.21.0", "042ab2c0c860652bc5cf69c94e3a31f96676d14682e22ec7813bd173ceff1788", [:rebar3], [{:ssl_verify_fun, "~> 1.1", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "6cee6cffc35a390840d48d463541d50746a7b0e421acaadb833cfc7961e490e7"}, - "tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"}, + "tzdata": {:hex, :tzdata, "1.1.2", "45e5f1fcf8729525ec27c65e163be5b3d247ab1702581a94674e008413eef50b", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "cec7b286e608371602318c414f344941d5eb0375e14cfdab605cca2fe66cba8b"}, "ua_inspector": {:hex, :ua_inspector, "3.10.0", "eb2a889039c82855c4638227bae8434e37fe16a7430eb3dab323a80b4372f41d", [:mix], [{:hackney, "~> 1.0", [hex: :hackney, repo: "hexpm", optional: false]}, {:yamerl, "~> 0.7", [hex: :yamerl, repo: "hexpm", optional: false]}], "hexpm", "ec619c3742bd26b27934c9f89448ea23cc252717b8fd3630ec1e24fac18a17e4"}, "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, "websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},