diff --git a/CHANGELOG.md b/CHANGELOG.md index 97d560c01..0ae6931f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,12 +12,16 @@ All notable changes to this project will be documented in this file. ### Changed - Use alpine as base image to decrease Docker image size plausible/analytics#353 - Ignore automated browsers (Phantom, Selenium, Headless Chrome, etc) +- Display domain's favicon on the home page +- Ignore consecutive pageviews on same pathname plausible/analytics#417 +- Validate domain format on site creation plausible/analytics#427 ### Fixed - Do not error when activating an already activated account plausible/analytics#370 - Ignore arrow keys when modifier keys are pressed plausible/analytics#363 - Show correct stats when goal filter is combined with source plausible/analytics#374 - Going back in history now correctly resets the period filter plausible/analytics#408 +- Fix URL decoding in query parameters plausible/analytics#416 ### Security - Do not run the plausible Docker container as root plausible/analytics#362 diff --git a/lib/plausible/site/schema.ex b/lib/plausible/site/schema.ex index b438cf4a1..8fe6582c7 100644 --- a/lib/plausible/site/schema.ex +++ b/lib/plausible/site/schema.ex @@ -22,6 +22,7 @@ defmodule Plausible.Site do site |> cast(attrs, [:domain, :timezone]) |> validate_required([:domain, :timezone]) + |> validate_format(:domain, ~r/^[a-zA-z0-9\-\.\/\:]*$/, message: "only letters, numbers, slashes and period allowed") |> unique_constraint(:domain) |> clean_domain end diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex index f3c094633..91f5e26f9 100644 --- a/lib/plausible_web/controllers/api/external_controller.ex +++ b/lib/plausible_web/controllers/api/external_controller.ex @@ -71,7 +71,7 @@ defmodule PlausibleWeb.Api.ExternalController do if is_bot?(ua) do {:ok, nil} else - uri = params["url"] && URI.parse(URI.decode(params["url"])) + uri = params["url"] && URI.parse(params["url"]) query = if uri && uri.query, do: URI.decode_query(uri.query), else: %{} ref = parse_referrer(uri, params["referrer"]) @@ -97,7 +97,7 @@ defmodule PlausibleWeb.Api.ExternalController do browser_version: (ua && browser_version(ua)) || "", screen_size: calculate_screen_size(params["screen_width"]) || "", "meta.key": Map.keys(params["meta"]), - "meta.value": Map.values(params["meta"]) + "meta.value": Map.values(params["meta"]) |> Enum.map(&Kernel.to_string/1) } changeset = Plausible.ClickhouseEvent.changeset(%Plausible.ClickhouseEvent{}, event_attrs) @@ -133,10 +133,11 @@ defmodule PlausibleWeb.Api.ExternalController do defp get_pathname(nil, _), do: "/" defp get_pathname(uri, hash_mode) do - pathname = uri.path || "/" + pathname = (uri.path || "/") + |> URI.decode if hash_mode && uri.fragment do - pathname <> "#" <> uri.fragment + pathname <> "#" <> URI.decode(uri.fragment) else pathname end diff --git a/lib/plausible_web/plugs/tracker.ex b/lib/plausible_web/plugs/tracker.ex index 982f30869..553225ab7 100644 --- a/lib/plausible_web/plugs/tracker.ex +++ b/lib/plausible_web/plugs/tracker.ex @@ -47,6 +47,7 @@ defmodule PlausibleWeb.Tracker do conn |> put_resp_header("cache-control", "max-age=#{@max_age},public") |> put_resp_header("content-type", "application/javascript") + |> put_resp_header("cross-origin-resource-policy", "cross-origin") |> send_resp(200, file) |> halt() end diff --git a/lib/plausible_web/templates/site/index.html.eex b/lib/plausible_web/templates/site/index.html.eex index b4ec44af6..0c307a556 100644 --- a/lib/plausible_web/templates/site/index.html.eex +++ b/lib/plausible_web/templates/site/index.html.eex @@ -8,7 +8,8 @@
<%= for site <- @sites do %>
- + +

<%= site.domain %>

<%= link(to: "/#{URI.encode_www_form(site.domain)}/settings", class: "flex absolute hover:bg-gray-100 transition rounded py-2 px-5", style: "top: 12px; right: 6px;") do %> diff --git a/priv/tracker/js/analytics.js b/priv/tracker/js/analytics.js index 9fbea71c7..ad8f6d368 100644 --- a/priv/tracker/js/analytics.js +++ b/priv/tracker/js/analytics.js @@ -1 +1 @@ -!function(i,r){"use strict";var o=i.location,s=i.document,e=s.querySelector('[src*="'+r+'"]'),l=e&&e.getAttribute("data-domain"),t=!1;function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){n("pageview")}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a));var u=i.plausible&&i.plausible.q||[];i.plausible=n;for(var h=0;h"); \ No newline at end of file +!function(i,r){"use strict";var e,o=i.location,s=i.document,t=s.querySelector('[src*="'+r+'"]'),l=t&&t.getAttribute("data-domain");function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){e!==o.pathname&&(e=o.pathname,n("pageview"))}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a));var u=i.plausible&&i.plausible.q||[];i.plausible=n;for(var h=0;h"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.hash.js b/priv/tracker/js/plausible.hash.js index e0159acc5..d13164855 100644 --- a/priv/tracker/js/plausible.hash.js +++ b/priv/tracker/js/plausible.hash.js @@ -1 +1 @@ -!function(i,r){"use strict";var s=i.location,o=i.document,e=o.querySelector('[src*="'+r+'"]'),l=e&&e.getAttribute("data-domain"),t=!1;function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(s.hostname)||"file:"===s.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=s.href,n.d=l,n.r=o.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),n.h=1,(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){n("pageview")}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a)),i.addEventListener("hashchange",a);var h=i.plausible&&i.plausible.q||[];i.plausible=n;for(var u=0;u"); \ No newline at end of file +!function(i,r){"use strict";var e,o=i.location,s=i.document,t=s.querySelector('[src*="'+r+'"]'),l=t&&t.getAttribute("data-domain");function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),n.h=1,(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){e=o.pathname,n("pageview")}try{i.addEventListener("hashchange",a);var c=i.plausible&&i.plausible.q||[];i.plausible=n;for(var p=0;p"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.hash.outbound-links.js b/priv/tracker/js/plausible.hash.outbound-links.js index ed1f3b029..7f8536ded 100644 --- a/priv/tracker/js/plausible.hash.outbound-links.js +++ b/priv/tracker/js/plausible.hash.outbound-links.js @@ -1 +1 @@ -!function(r,i){"use strict";var o=r.location,s=r.document,e=s.querySelector('[src*="'+i+'"]'),l=e&&e.getAttribute("data-domain"),t=!1;function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var a,n;r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver||((a={}).n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),(n=new XMLHttpRequest).open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()})}function n(){a("pageview")}function p(e){for(var t=e.target;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&plausible("Outbound Link: Click",{meta:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||(setTimeout(function(){o.href=t.href},150),e.preventDefault())}try{var c,u=r.history;u.pushState&&(c=u.pushState,u.pushState=function(){c.apply(this,arguments),n()},r.addEventListener("popstate",n)),r.addEventListener("load",function(){for(var e=s.getElementsByTagName("a"),t=0;t"); \ No newline at end of file +!function(r,i){"use strict";var e,o=r.location,s=r.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain");function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var a,n;r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver||((a={}).n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),a.h=1,(n=new XMLHttpRequest).open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()})}function n(){e=o.pathname,a("pageview")}try{r.addEventListener("hashchange",n),s.addEventListener("click",function(e){for(var t=e.target;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==o.host&&(plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||(setTimeout(function(){o.href=t.href},150),e.preventDefault()))});var c=r.plausible&&r.plausible.q||[];r.plausible=a;for(var p=0;p"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.js b/priv/tracker/js/plausible.js index 9fbea71c7..ad8f6d368 100644 --- a/priv/tracker/js/plausible.js +++ b/priv/tracker/js/plausible.js @@ -1 +1 @@ -!function(i,r){"use strict";var o=i.location,s=i.document,e=s.querySelector('[src*="'+r+'"]'),l=e&&e.getAttribute("data-domain"),t=!1;function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){n("pageview")}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a));var u=i.plausible&&i.plausible.q||[];i.plausible=n;for(var h=0;h"); \ No newline at end of file +!function(i,r){"use strict";var e,o=i.location,s=i.document,t=s.querySelector('[src*="'+r+'"]'),l=t&&t.getAttribute("data-domain");function n(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var n,a;i.phantom||i._phantom||i.__nightmare||i.navigator.webdriver||((n={}).n=e,n.u=o.href,n.d=l,n.r=s.referrer||null,n.w=i.innerWidth,t&&t.meta&&(n.m=JSON.stringify(t.meta)),t&&t.props&&(n.p=JSON.stringify(t.props)),(a=new XMLHttpRequest).open("POST",r+"/api/event",!0),a.setRequestHeader("Content-Type","text/plain"),a.send(JSON.stringify(n)),a.onreadystatechange=function(){4==a.readyState&&t&&t.callback&&t.callback()})}function a(){e!==o.pathname&&(e=o.pathname,n("pageview"))}try{var p,c=i.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),a()},i.addEventListener("popstate",a));var u=i.plausible&&i.plausible.q||[];i.plausible=n;for(var h=0;h"); \ No newline at end of file diff --git a/priv/tracker/js/plausible.outbound-links.js b/priv/tracker/js/plausible.outbound-links.js index ed1f3b029..35dc7ee42 100644 --- a/priv/tracker/js/plausible.outbound-links.js +++ b/priv/tracker/js/plausible.outbound-links.js @@ -1 +1 @@ -!function(r,i){"use strict";var o=r.location,s=r.document,e=s.querySelector('[src*="'+i+'"]'),l=e&&e.getAttribute("data-domain"),t=!1;function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var a,n;r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver||((a={}).n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),(n=new XMLHttpRequest).open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()})}function n(){a("pageview")}function p(e){for(var t=e.target;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&plausible("Outbound Link: Click",{meta:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||(setTimeout(function(){o.href=t.href},150),e.preventDefault())}try{var c,u=r.history;u.pushState&&(c=u.pushState,u.pushState=function(){c.apply(this,arguments),n()},r.addEventListener("popstate",n)),r.addEventListener("load",function(){for(var e=s.getElementsByTagName("a"),t=0;t"); \ No newline at end of file +!function(r,i){"use strict";var e,o=r.location,s=r.document,t=s.querySelector('[src*="'+i+'"]'),l=t&&t.getAttribute("data-domain");function a(e,t){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(o.hostname)||"file:"===o.protocol)return console.warn("Ignoring event on localhost");var a,n;r.phantom||r._phantom||r.__nightmare||r.navigator.webdriver||((a={}).n=e,a.u=o.href,a.d=l,a.r=s.referrer||null,a.w=r.innerWidth,t&&t.meta&&(a.m=JSON.stringify(t.meta)),t&&t.props&&(a.p=JSON.stringify(t.props)),(n=new XMLHttpRequest).open("POST",i+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(a)),n.onreadystatechange=function(){4==n.readyState&&t&&t.callback&&t.callback()})}function n(){e!==o.pathname&&(e=o.pathname,a("pageview"))}try{var p,c=r.history;c.pushState&&(p=c.pushState,c.pushState=function(){p.apply(this,arguments),n()},r.addEventListener("popstate",n)),s.addEventListener("click",function(e){for(var t=e.target;t&&(void 0===t.tagName||"a"!=t.tagName.toLowerCase()||!t.href);)t=t.parentNode;t&&t.href&&t.host&&t.host!==o.host&&(plausible("Outbound Link: Click",{props:{url:t.href}}),t.target&&!t.target.match(/^_(self|parent|top)$/i)||(setTimeout(function(){o.href=t.href},150),e.preventDefault()))});var u=r.plausible&&r.plausible.q||[];r.plausible=a;for(var h=0;h"); \ No newline at end of file diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 14ea3c89e..3318574cf 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -391,6 +391,27 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert event.name == "custom event" end + test "casts custom props to string", %{conn: conn} do + params = %{ + name: "Signup", + url: "http://gigride.live/", + domain: "custom-prop-test.com", + props: Jason.encode!(%{ + bool_test: true, + number_test: 12 + }) + } + + conn + |> put_req_header("content-type", "text/plain") + |> post("/api/event", Jason.encode!(params)) + + event = get_event("custom-prop-test.com") + + assert Map.get(event, :"meta.key") == ["bool_test", "number_test"] + assert Map.get(event, :"meta.value") == ["true", "12"] + end + test "ignores a malformed referrer URL", %{conn: conn} do params = %{ name: "pageview", @@ -484,6 +505,25 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert pageview.pathname == "/#page-a" end + test "decodes URL pathname, fragment and search", %{conn: conn} do + params = %{ + n: "pageview", + u: "https://test.com/%EF%BA%9D%EF%BB%AD%EF%BA%8E%EF%BA%8B%EF%BA%AF-%EF%BB%AE%EF%BB%A4%EF%BA%B3%EF%BA%8E%EF%BA%92%EF%BB%97%EF%BA%8E%EF%BA%97?utm_source=%25balle%25", + d: "url-decode-test.com", + h: 1 + } + + conn + |> put_req_header("content-type", "text/plain") + |> post("/api/event", Jason.encode!(params)) + + pageview = get_event("url-decode-test.com") + + assert pageview.hostname == "test.com" + assert pageview.pathname == "/ﺝﻭﺎﺋﺯ-ﻮﻤﺳﺎﺒﻗﺎﺗ" + assert pageview.utm_source == "%balle%" + end + test "responds 400 when required fields are missing", %{conn: conn} do params = %{} diff --git a/test/plausible_web/controllers/api/stats_controller/browsers_test.exs b/test/plausible_web/controllers/api/stats_controller/browsers_test.exs index 291ced385..e00252d4a 100644 --- a/test/plausible_web/controllers/api/stats_controller/browsers_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/browsers_test.exs @@ -20,10 +20,10 @@ defmodule PlausibleWeb.Api.StatsController.BrowsersTest do test "returns top browser versions by unique visitors", %{conn: conn, site: site} do filters = Jason.encode!(%{browser: "Chrome"}) - conn = get(conn, "/api/stats/#{site.domain}/browser-versions?period=day&date=2019-01-01") + conn = get(conn, "/api/stats/#{site.domain}/browser-versions?period=day&date=2019-01-01&filters=#{filters}") assert json_response(conn, 200) == [ - %{"name" => "78.0", "count" => 2, "percentage" => 100} + %{"name" => "78.0", "count" => 1, "percentage" => 100} ] end end diff --git a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs index 55cc16a32..3499c43ed 100644 --- a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs @@ -167,7 +167,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do ) res = json_response(conn, 200) - assert %{"name" => "Unique visitors", "count" => 2, "change" => 100} in res["top_stats"] + assert %{"name" => "Unique visitors", "count" => 3, "change" => 100} in res["top_stats"] end test "returns only visitors with specific screen size", %{conn: conn, site: site} do @@ -180,7 +180,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do ) res = json_response(conn, 200) - assert %{"name" => "Unique visitors", "count" => 2, "change" => 100} in res["top_stats"] + assert %{"name" => "Unique visitors", "count" => 3, "change" => 100} in res["top_stats"] end test "returns only visitors with specific browser", %{conn: conn, site: site} do @@ -193,7 +193,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do ) res = json_response(conn, 200) - assert %{"name" => "Unique visitors", "count" => 2, "change" => 100} in res["top_stats"] + assert %{"name" => "Unique visitors", "count" => 3, "change" => 100} in res["top_stats"] end test "returns only visitors with specific operating system", %{conn: conn, site: site} do @@ -206,7 +206,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do ) res = json_response(conn, 200) - assert %{"name" => "Unique visitors", "count" => 2, "change" => 100} in res["top_stats"] + assert %{"name" => "Unique visitors", "count" => 3, "change" => 100} in res["top_stats"] end end end diff --git a/test/plausible_web/controllers/api/stats_controller/operating_systems_test.exs b/test/plausible_web/controllers/api/stats_controller/operating_systems_test.exs index 10392834a..71658d93f 100644 --- a/test/plausible_web/controllers/api/stats_controller/operating_systems_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/operating_systems_test.exs @@ -20,10 +20,10 @@ defmodule PlausibleWeb.Api.StatsController.OperatingSystemsTest do test "returns top OS versions by unique visitors", %{conn: conn, site: site} do filters = Jason.encode!(%{os: "Mac"}) - conn = get(conn, "/api/stats/#{site.domain}/browser-versions?period=day&date=2019-01-01") + conn = get(conn, "/api/stats/#{site.domain}/operating-system-versions?period=day&date=2019-01-01&filters=#{filters}") assert json_response(conn, 200) == [ - %{"name" => "10.15", "count" => 2, "percentage" => 100} + %{"name" => "10.15", "count" => 1, "percentage" => 100} ] end end diff --git a/test/plausible_web/controllers/api/stats_controller/sources_test.exs b/test/plausible_web/controllers/api/stats_controller/sources_test.exs index 9f688900a..3eb6dc0f0 100644 --- a/test/plausible_web/controllers/api/stats_controller/sources_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/sources_test.exs @@ -128,7 +128,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do ) assert json_response(conn, 200) == %{ - "total_visitors" => 3, + "total_visitors" => 4, "referrers" => [ %{"name" => "10words.com/page1", "url" => "10words.com", "count" => 2} ] @@ -147,7 +147,7 @@ defmodule PlausibleWeb.Api.StatsController.SourcesTest do ) assert json_response(conn, 200) == %{ - "total_visitors" => 3, + "total_visitors" => 4, "referrers" => [ %{ "name" => "10words.com/page1", diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index 8132cacf0..b3d674c35 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -52,6 +52,18 @@ defmodule PlausibleWeb.SiteControllerTest do assert html_response(conn, 200) =~ "can't be blank" end + test "only alphanumeric characters and slash allowed in domain", %{conn: conn} do + conn = + post(conn, "/sites", %{ + "site" => %{ + "timezone" => "Europe/London", + "domain" => "!@£.com" + } + }) + + assert html_response(conn, 200) =~ "only letters, numbers, slashes and period allowed" + end + test "renders form again when it is a duplicate domain", %{conn: conn} do insert(:site, domain: "example.com") diff --git a/test/support/clickhouse_setup.ex b/test/support/clickhouse_setup.ex index 4b171338d..73ba2e514 100644 --- a/test/support/clickhouse_setup.ex +++ b/test/support/clickhouse_setup.ex @@ -16,7 +16,8 @@ defmodule Plausible.Test.ClickhouseSetup do screen_size: "Desktop", referrer_source: "10words", referrer: "10words.com/page1", - timestamp: ~N[2019-01-01 00:00:00] + timestamp: ~N[2019-01-01 00:00:00], + session_id: @conversion_1_session_id, }, %{ name: "pageview", @@ -161,7 +162,9 @@ defmodule Plausible.Test.ClickhouseSetup do country_code: "US", screen_size: "Desktop", browser: "Chrome", + browser_version: "78.0", operating_system: "Mac", + operating_system_version: "10.15", session_id: @conversion_1_session_id, is_bounce: true, duration: 100, diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js index 7c6267f02..58ab112f5 100644 --- a/tracker/src/plausible.js +++ b/tracker/src/plausible.js @@ -6,7 +6,7 @@ var scriptEl = document.querySelector('[src*="' + plausibleHost +'"]') var domain = scriptEl && scriptEl.getAttribute('data-domain') - var pageVisible = false + var lastPage; function trigger(eventName, options) { if (/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(location.hostname) || location.protocol === 'file:') return console.warn('Ignoring event on localhost'); @@ -42,49 +42,44 @@ } function page() { + {{#unless hashMode}} + if (lastPage === location.pathname) return; + {{/unless}} + lastPage = location.pathname trigger('pageview') } function handleVisibilityChange() { - if (!pageVisible && document.visibilityState === 'visible') { - pageVisible = true + if (!lastPage && document.visibilityState === 'visible') { page() } } {{#if outboundLinks}} - function trackOutboundLink(event) { - var link = event.target; - while (link && (typeof link.tagName == 'undefined' || link.tagName.toLowerCase() != 'a' || !link.href)) { - link = link.parentNode; - } - - if (link && link.href) { - plausible('Outbound Link: Click', {meta: {url: link.href}}) - } - - // Delay navigation so that Plausible is notified of the click - if(!link.target || link.target.match(/^_(self|parent|top)$/i)) { - setTimeout(function() { location.href = link.href; }, 150); - event.preventDefault(); - } - } - function registerOutboundLinkEvents() { - window.addEventListener('load', function() { - var links = document.getElementsByTagName('a') + document.addEventListener('click', function (event) { + var link = event.target; + while(link && (typeof link.tagName == 'undefined' || link.tagName.toLowerCase() != 'a' || !link.href)) { + link = link.parentNode + } + + if (link && link.href && link.host && link.host !== location.host) { + plausible('Outbound Link: Click', {props: {url: link.href}}) - for (var i = 0; i < links.length; ++i) { - var link = links[i] - if (link.host !== location.host) { - link.addEventListener('click', trackOutboundLink); + // Delay navigation so that Plausible is notified of the click + if(!link.target || link.target.match(/^_(self|parent|top)$/i)) { + setTimeout(function() { location.href = link.href; }, 150); + event.preventDefault(); } } - }); + }) } {{/if}} try { + {{#if hashMode}} + window.addEventListener('hashchange', page) + {{else}} var his = window.history if (his.pushState) { var originalPushState = his['pushState'] @@ -95,8 +90,6 @@ window.addEventListener('popstate', page) } - {{#if hashMode}} - window.addEventListener('hashchange', page) {{/if}} {{#if outboundLinks}} registerOutboundLinkEvents()