Merge branch 'master' into fix/history-state

pull/408/head
Johan Fagerberg 6 years ago committed by GitHub
commit 6f5004ed25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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

@ -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

@ -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

@ -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

@ -8,7 +8,8 @@
<div class="overflow-hidden bg-white rounded max-w-xl w-full shadow-md leading-normal">
<%= for site <- @sites do %>
<div class="w-full relative">
<a href="/<%= URI.encode_www_form(site.domain) %>" class="block hover:bg-gray-100 group p-4 border-b no-underline flex justify-between transition">
<a href="/<%= URI.encode_www_form(site.domain) %>" class="block hover:bg-gray-100 group p-4 border-b no-underline flex justify-between transition items-center">
<img src="https://icons.duckduckgo.com/ip3/<%= site.domain %>.ico" referrerpolicy="no-referrer" class="inline mb-1 mr-2 w-4">
<p class="w-full font-bold text-lg mb-1 text-gray-800"><%= site.domain %></p>
</a>
<%= 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 %>

@ -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<u.length;h++)n.apply(this,u[h]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){t||"visible"!==s.visibilityState||(t=!0,a())}):a()}catch(e){console.error(e),(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");
!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<u.length;h++)n.apply(this,u[h]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){e||"visible"!==s.visibilityState||a()}):a()}catch(e){console.error(e),(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");

@ -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<h.length;u++)n.apply(this,h[u]);"prerender"===o.visibilityState?o.addEventListener("visibilitychange",function(){t||"visible"!==o.visibilityState||(t=!0,a())}):a()}catch(e){console.error(e),(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");
!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<c.length;p++)n.apply(this,c[p]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){e||"visible"!==s.visibilityState||a()}):a()}catch(e){console.error(e),(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");

@ -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<e.length;++t){var a=e[t];a.host!==o.host&&a.addEventListener("click",p)}});var h=r.plausible&&r.plausible.q||[];r.plausible=a;for(var f=0;f<h.length;f++)a.apply(this,h[f]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){t||"visible"!==s.visibilityState||(t=!0,n())}):n()}catch(e){console.error(e),(new Image).src=i+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");
!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<c.length;p++)a.apply(this,c[p]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){e||"visible"!==s.visibilityState||n()}):n()}catch(e){console.error(e),(new Image).src=i+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");

@ -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<u.length;h++)n.apply(this,u[h]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){t||"visible"!==s.visibilityState||(t=!0,a())}):a()}catch(e){console.error(e),(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");
!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<u.length;h++)n.apply(this,u[h]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){e||"visible"!==s.visibilityState||a()}):a()}catch(e){console.error(e),(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");

@ -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<e.length;++t){var a=e[t];a.host!==o.host&&a.addEventListener("click",p)}});var h=r.plausible&&r.plausible.q||[];r.plausible=a;for(var f=0;f<h.length;f++)a.apply(this,h[f]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){t||"visible"!==s.visibilityState||(t=!0,n())}):n()}catch(e){console.error(e),(new Image).src=i+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");
!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<u.length;h++)a.apply(this,u[h]);"prerender"===s.visibilityState?s.addEventListener("visibilitychange",function(){e||"visible"!==s.visibilityState||n()}):n()}catch(e){console.error(e),(new Image).src=i+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");

@ -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 = %{}

@ -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

@ -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

@ -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

@ -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",

@ -52,6 +52,18 @@ defmodule PlausibleWeb.SiteControllerTest do
assert html_response(conn, 200) =~ "can&#39;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")

@ -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,

@ -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()

Loading…
Cancel
Save