Merge branch 'plausible:master' into master

pull/1160/head
Ru Singh 6 years ago
commit 93c4fd5fbc

@ -7,7 +7,10 @@ All notable changes to this project will be documented in this file.
- New parameter `metrics` for the `/api/v1/stats/timeseries` endpoint plausible/analytics#952
- CSV export now includes pageviews, bounce rate and visit duration in addition to visitors plausible/analytics#952
- Send stats to multiple dashboards by configuring a comma-separated list of domains plausible/analytics#968
- To authenticate against a local postgresql via socket authentication, the environment-variables
`DATABASE_SOCKET_DIR` & `DATABASE_NAME` were added.
- Time on Page metric available in detailed Top Pages report plausible/analytics#1007
- Added `CLICKHOUSE_FLUSH_INTERVAL_MS` and `CLICKHOUSE_MAX_BUFFER_SIZE` configuration parameters
### Fixed
- Fix weekly report time range plausible/analytics#951

@ -63,7 +63,7 @@ class ReferrerDrilldownModal extends React.Component {
<Link className="block truncate hover:underline dark:text-gray-200" to={{search: query.toString(), pathname: '/' + this.props.site.domain}} title={referrer.name}>
{referrer.name}
</Link>
{ this.renderExternalLink(name) }
{ this.renderExternalLink(referrer.name) }
</span>
)
}

@ -1,6 +1,6 @@
import React from 'react';
import { withRouter } from 'react-router-dom'
import Chart from 'chart.js'
import Chart from 'chart.js/auto';
import { eventName, navigateToQuery } from '../query'
import numberFormatter, {durationFormatter} from '../number-formatter'
import * as api from '../api'
@ -26,6 +26,7 @@ function buildDataSet(plot, present_index, ctx, label) {
borderColor: 'rgba(101,116,205)',
pointBackgroundColor: 'rgba(101,116,205)',
backgroundColor: gradient,
fill: true
},
{
label: label,
@ -35,6 +36,7 @@ function buildDataSet(plot, present_index, ctx, label) {
borderColor: 'rgba(101,116,205)',
pointBackgroundColor: 'rgba(101,116,205)',
backgroundColor: gradient,
fill: true
}]
} else {
return [{
@ -44,6 +46,7 @@ function buildDataSet(plot, present_index, ctx, label) {
borderColor: 'rgba(101,116,205)',
pointBackgroundColor: 'rgba(101,116,205)',
backgroundColor: gradient,
fill: true
}]
}
}
@ -64,7 +67,7 @@ const DAYS_ABBREV = [
]
function dateFormatter(interval, longForm) {
return function(isoDate) {
return function(isoDate, index, ticks) {
let date = new Date(isoDate)
if (interval === 'month') {
@ -113,75 +116,70 @@ class LineGraph extends React.Component {
},
options: {
animation: false,
legend: {display: false},
responsive: true,
elements: {line: {tension: 0}, point: {radius: 0}},
onClick: this.onClick.bind(this),
tooltips: {
mode: 'index',
intersect: false,
xPadding: 10,
yPadding: 10,
titleFontSize: 18,
footerFontSize: 14,
bodyFontSize: 14,
backgroundColor: 'rgba(25, 30, 56)',
titleMarginBottom: 8,
bodySpacing: 6,
footerMarginTop: 8,
xPadding: 16,
yPadding: 12,
multiKeyBackground: 'none',
callbacks: {
title: function(dataPoints) {
const data = dataPoints[0]
return dateFormatter(graphData.interval, true)(data.xLabel)
},
beforeBody: function() {
this.drawnLabels = {}
},
label: function(item) {
const dataset = this._data.datasets[item.datasetIndex]
if (!this.drawnLabels[dataset.label]) {
this.drawnLabels[dataset.label] = true
const pluralizedLabel = item.yLabel === 1 ? dataset.label.slice(0, -1) : dataset.label
return ` ${item.yLabel} ${pluralizedLabel}`
}
},
footer: function(dataPoints) {
if (graphData.interval === 'month') {
return 'Click to view month'
} else if (graphData.interval === 'date') {
return 'Click to view day'
plugins: {
legend: {display: false},
tooltip: {
mode: 'index',
intersect: false,
titleFont: {size: 18},
footerFont: {size: 14},
bodyFont: {size: 14},
backgroundColor: 'rgba(25, 30, 56)',
titleMarginBottom: 8,
bodySpacing: 6,
footerMarginTop: 8,
padding: {x: 10, y: 10},
multiKeyBackground: 'none',
callbacks: {
title: function(dataPoints) {
const data = dataPoints[0]
return dateFormatter(graphData.interval, true)(data.label)
},
beforeBody: function() {
this.drawnLabels = {}
},
label: function(item) {
const dataset = item.dataset
if (!this.drawnLabels[dataset.label]) {
this.drawnLabels[dataset.label] = true
const pluralizedLabel = item.formattedValue === "1" ? dataset.label.slice(0, -1) : dataset.label
return ` ${item.formattedValue} ${pluralizedLabel}`
}
},
footer: function(dataPoints) {
if (graphData.interval === 'month') {
return 'Click to view month'
} else if (graphData.interval === 'date') {
return 'Click to view day'
}
}
}
}
},
},
responsive: true,
elements: {line: {tension: 0}, point: {radius: 0}},
onClick: this.onClick.bind(this),
scales: {
yAxes: [{
y: {
beginAtZero: true,
ticks: {
callback: numberFormatter,
beginAtZero: true,
autoSkip: true,
maxTicksLimit: 8,
fontColor: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
},
gridLines: {
grid: {
zeroLineColor: 'transparent',
drawBorder: false,
}
}],
xAxes: [{
gridLines: {
display: false,
},
},
x: {
grid: {display: false},
ticks: {
autoSkip: true,
maxTicksLimit: 8,
callback: dateFormatter(graphData.interval),
fontColor: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
callback: function(val, index, ticks) { return dateFormatter(graphData.interval)(this.getLabelForValue(val)) },
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
}
}]
}
}
}
});
@ -211,7 +209,8 @@ class LineGraph extends React.Component {
onClick(e) {
const element = this.chart.getElementsAtEventForMode(e, 'index', {intersect: false})[0]
const date = element._chart.config.data.labels[element._index]
const date = this.chart.data.labels[element.index]
if (this.props.graphData.interval === 'month') {
navigateToQuery(
this.props.history,

31170
assets/package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -6,38 +6,38 @@
"watch": "$(npm bin)/webpack --mode development --watch"
},
"dependencies": {
"@babel/core": "^7.11.1",
"@babel/preset-env": "^7.11.0",
"@babel/preset-react": "^7.10.4",
"@tailwindcss/aspect-ratio": "^0.2.0",
"@tailwindcss/forms": "^0.2.1",
"@tailwindcss/typography": "^0.3.1",
"abortcontroller-polyfill": "^1.5.0",
"alpinejs": "^2.7.3",
"autoprefixer": "^9.8.6",
"babel-loader": "^8.1.0",
"chart.js": "^2.9.3",
"copy-webpack-plugin": "^6.0.3",
"css-loader": "^3.6.0",
"@babel/core": "^7.14.3",
"@babel/preset-env": "^7.14.4",
"@babel/preset-react": "^7.13.13",
"@tailwindcss/aspect-ratio": "^0.2.1",
"@tailwindcss/forms": "^0.3.2",
"@tailwindcss/typography": "^0.4.1",
"abortcontroller-polyfill": "^1.7.3",
"alpinejs": "^2.8.2",
"autoprefixer": "^10.2.6",
"babel-loader": "^8.2.2",
"chart.js": "^3.3.2",
"copy-webpack-plugin": "^9.0.0",
"css-loader": "^5.2.6",
"css-minimizer-webpack-plugin": "^3.0.0",
"datamaps": "^0.5.9",
"iframe-resizer": "^4.3.1",
"mini-css-extract-plugin": "^0.8.2",
"optimize-css-assets-webpack-plugin": "^5.0.3",
"iframe-resizer": "^4.3.2",
"mini-css-extract-plugin": "^1.6.0",
"phoenix": "file:../deps/phoenix",
"phoenix_html": "file:../deps/phoenix_html",
"postcss": "^7.0.35",
"postcss-loader": "^4.0.4",
"postcss": "^8.3.0",
"postcss-loader": "^5.3.0",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-flatpickr": "^3.10.5",
"react-flatpickr": "3.10.5",
"react-flip-move": "^3.0.4",
"react-router-dom": "^5.2.0",
"react-transition-group": "^4.4.1",
"tailwindcss": "^2.0.1-compat",
"terser-webpack-plugin": "^4.2.3",
"url-search-params-polyfill": "^8.0.0",
"webpack": "4.39.2",
"webpack-cli": "^3.3.12"
"react-transition-group": "^4.4.2",
"tailwindcss": "^2.1.2",
"terser-webpack-plugin": "^5.1.2",
"url-search-params-polyfill": "^8.1.1",
"webpack": "5.38.1",
"webpack-cli": "^4.7.0"
},
"devDependencies": {
"eslint": "^7.2.0",
@ -50,7 +50,8 @@
"eslint-plugin-react-hooks": "^4.2.0",
"stylelint": "^13.8.0",
"stylelint-config-prettier": "^8.0.2",
"stylelint-config-standard": "^20.0.0"
"stylelint-config-standard": "^20.0.0",
"webpack-bundle-analyzer": "^4.4.2"
},
"name": "assets"
}

@ -2,14 +2,14 @@ const path = require('path');
const glob = require('glob');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = (env, options) => ({
optimization: {
minimizer: [
new TerserPlugin(),
new OptimizeCSSAssetsPlugin({})
new CssMinimizerWebpackPlugin()
]
},
entry: {

@ -14,7 +14,8 @@ config :plausible, PlausibleWeb.Endpoint,
"node_modules/webpack/bin/webpack.js",
"--mode",
"development",
"--watch-stdin",
"--watch",
"--watch-options-stdin",
cd: Path.expand("../assets", __DIR__)
]
],

@ -50,6 +50,9 @@ app_version = System.get_env("APP_VERSION", "0.0.1")
ch_db_url =
System.get_env("CLICKHOUSE_DATABASE_URL", "http://plausible_events_db:8123/plausible_events_db")
{ch_flush_interval_ms, ""} = Integer.parse(System.get_env("CLICKHOUSE_FLUSH_INTERVAL_MS", "5000"))
{ch_max_buffer_size, ""} = Integer.parse(System.get_env("CLICKHOUSE_MAX_BUFFER_SIZE", "10000"))
### Mandatory params End
sentry_dsn = System.get_env("SENTRY_DSN")
@ -78,9 +81,12 @@ disable_registration = String.to_existing_atom(System.get_env("DISABLE_REGISTRAT
hcaptcha_sitekey = System.get_env("HCAPTCHA_SITEKEY")
hcaptcha_secret = System.get_env("HCAPTCHA_SECRET")
log_level = String.to_existing_atom(System.get_env("LOG_LEVEL", "warn"))
log_format = System.get_env("LOG_FORMAT", "elixir")
is_selfhost = String.to_existing_atom(System.get_env("SELFHOST", "true"))
{site_limit, ""} = Integer.parse(System.get_env("SITE_LIMIT", "20"))
site_limit_exempt =
System.get_env("SITE_LIMIT_EXEMPT", "") |> String.split(",") |> Enum.map(&String.trim/1)
disable_cron = String.to_existing_atom(System.get_env("DISABLE_CRON", "false"))
{user_agent_cache_limit, ""} = Integer.parse(System.get_env("USER_AGENT_CACHE_LIMIT", "1000"))
@ -96,6 +102,7 @@ config :plausible,
mailer_email: mailer_email,
admin_emails: admin_emails,
site_limit: site_limit,
site_limit_exempt: site_limit_exempt,
is_selfhost: is_selfhost
config :plausible, :selfhost,
@ -107,7 +114,15 @@ config :plausible, PlausibleWeb.Endpoint,
http: [port: port],
secret_key_base: secret_key_base
config :plausible, Plausible.Repo, url: db_url
case System.get_env("DATABASE_SOCKET_DIR", "") do
"" ->
config :plausible, Plausible.Repo, url: db_url
x ->
config :plausible, Plausible.Repo,
socket_dir: x,
database: System.get_env("DATABASE_NAME")
end
config :sentry,
dsn: sentry_dsn,
@ -130,7 +145,9 @@ config :plausible, Plausible.ClickhouseRepo,
loggers: [Ecto.LogEntry],
queue_target: 500,
queue_interval: 2000,
url: ch_db_url
url: ch_db_url,
flush_interval_ms: ch_flush_interval_ms,
max_buffer_size: ch_max_buffer_size
case mailer_adapter do
"Bamboo.PostmarkAdapter" ->
@ -251,6 +268,9 @@ config :plausible, :user_agent_cache,
limit: user_agent_cache_limit,
stats: user_agent_cache_stats
config :hammer,
backend: {Hammer.Backend.ETS, [expiry_ms: 60_000 * 60 * 4, cleanup_interval_ms: 60_000 * 10]}
config :kaffy,
otp_app: :plausible,
ecto_repo: Plausible.Repo,
@ -280,22 +300,13 @@ if config_env() != :test && geolite2_country_db do
]
end
logger_backends = %{
"elixir" => [:console],
"json" => [Ink]
}
config :logger,
level: log_level,
backends: logger_backends[log_format]
backends: [:console]
config :logger, Sentry.LoggerBackend,
capture_log_messages: true,
level: :error,
excluded_domains: []
if log_format == "json" do
config :logger, Ink,
name: "plausible",
level: log_level
end
config :tzdata, :data_dir, System.get_env("STORAGE_DIR", Application.app_dir(:tzdata, "priv"))

@ -3,10 +3,11 @@ defmodule Plausible.Auth.ApiKey do
import Ecto.Changeset
@required [:user_id, :key, :name]
@optional [:scopes]
@optional [:scopes, :hourly_request_limit]
schema "api_keys" do
field :name, :string
field :scopes, {:array, :string}, default: ["stats:read:*"]
field :hourly_request_limit, :integer
field :key, :string, virtual: true
field :key_hash, :string

@ -198,6 +198,7 @@ defmodule Plausible.Billing do
cond do
Timex.before?(user.inserted_at, @limit_accounts_since) -> nil
Application.get_env(:plausible, :is_selfhost) -> nil
user.email in Application.get_env(:plausible, :site_limit_exempt) -> nil
true -> Application.get_env(:plausible, :site_limit)
end
end

@ -1,8 +1,6 @@
defmodule Plausible.Event.WriteBuffer do
use GenServer
require Logger
@flush_interval_ms 5_000
@max_buffer_size 10_000
def start_link(_opts) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
@ -10,7 +8,7 @@ defmodule Plausible.Event.WriteBuffer do
def init(buffer) do
Process.flag(:trap_exit, true)
timer = Process.send_after(self(), :tick, @flush_interval_ms)
timer = Process.send_after(self(), :tick, flush_interval_ms())
{:ok, %{buffer: buffer, timer: timer}}
end
@ -27,11 +25,11 @@ defmodule Plausible.Event.WriteBuffer do
def handle_cast({:insert, event}, %{buffer: buffer} = state) do
new_buffer = [event | buffer]
if length(new_buffer) >= @max_buffer_size do
if length(new_buffer) >= max_buffer_size() do
Logger.info("Buffer full, flushing to disk")
Process.cancel_timer(state[:timer])
do_flush(new_buffer)
new_timer = Process.send_after(self(), :tick, @flush_interval_ms)
new_timer = Process.send_after(self(), :tick, flush_interval_ms())
{:noreply, %{buffer: [], timer: new_timer}}
else
{:noreply, %{state | buffer: new_buffer}}
@ -40,14 +38,14 @@ defmodule Plausible.Event.WriteBuffer do
def handle_info(:tick, %{buffer: buffer}) do
do_flush(buffer)
timer = Process.send_after(self(), :tick, @flush_interval_ms)
timer = Process.send_after(self(), :tick, flush_interval_ms())
{:noreply, %{buffer: [], timer: timer}}
end
def handle_call(:flush, _from, %{buffer: buffer} = state) do
Process.cancel_timer(state[:timer])
do_flush(buffer)
new_timer = Process.send_after(self(), :tick, @flush_interval_ms)
new_timer = Process.send_after(self(), :tick, flush_interval_ms())
{:reply, nil, %{buffer: [], timer: new_timer}}
end
@ -67,4 +65,12 @@ defmodule Plausible.Event.WriteBuffer do
Plausible.ClickhouseRepo.insert_all(Plausible.ClickhouseEvent, events)
end
end
defp flush_interval_ms() do
Keyword.fetch!(Application.get_env(:plausible, Plausible.ClickhouseRepo), :flush_interval_ms)
end
defp max_buffer_size() do
Keyword.fetch!(Application.get_env(:plausible, Plausible.ClickhouseRepo), :max_buffer_size)
end
end

@ -1,8 +1,6 @@
defmodule Plausible.Session.WriteBuffer do
use GenServer
require Logger
@flush_interval_ms 1000
@max_buffer_size 10_000
def start_link(_opts) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
@ -10,7 +8,7 @@ defmodule Plausible.Session.WriteBuffer do
def init(buffer) do
Process.flag(:trap_exit, true)
timer = Process.send_after(self(), :tick, @flush_interval_ms)
timer = Process.send_after(self(), :tick, flush_interval_ms())
{:ok, %{buffer: buffer, timer: timer}}
end
@ -22,11 +20,11 @@ defmodule Plausible.Session.WriteBuffer do
def handle_cast({:insert, sessions}, %{buffer: buffer} = state) do
new_buffer = sessions ++ buffer
if length(new_buffer) >= @max_buffer_size do
if length(new_buffer) >= max_buffer_size() do
Logger.info("Buffer full, flushing to disk")
Process.cancel_timer(state[:timer])
flush(new_buffer)
new_timer = Process.send_after(self(), :tick, @flush_interval_ms)
new_timer = Process.send_after(self(), :tick, flush_interval_ms())
{:noreply, %{buffer: [], timer: new_timer}}
else
{:noreply, %{state | buffer: new_buffer}}
@ -35,7 +33,7 @@ defmodule Plausible.Session.WriteBuffer do
def handle_info(:tick, %{buffer: buffer}) do
flush(buffer)
timer = Process.send_after(self(), :tick, @flush_interval_ms)
timer = Process.send_after(self(), :tick, flush_interval_ms())
{:noreply, %{buffer: [], timer: timer}}
end
@ -60,4 +58,12 @@ defmodule Plausible.Session.WriteBuffer do
Plausible.ClickhouseRepo.insert_all(Plausible.ClickhouseSession, sessions)
end
end
defp flush_interval_ms() do
Keyword.fetch!(Application.get_env(:plausible, Plausible.ClickhouseRepo), :flush_interval_ms)
end
defp max_buffer_size() do
Keyword.fetch!(Application.get_env(:plausible, Plausible.ClickhouseRepo), :max_buffer_size)
end
end

@ -47,24 +47,24 @@ defmodule Plausible.Stats.Base do
)
q =
if query.filters["event:page"] do
page = query.filters["event:page"]
from(e in q, where: e.pathname == ^page)
else
q
case query.filters["event:page"] do
{:is, page} -> from(e in q, where: e.pathname == ^page)
{:member, list} -> from(e in q, where: e.pathname in ^list)
_ -> q
end
q =
if query.filters["event:name"] do
name = query.filters["event:name"]
from(e in q, where: e.name == ^name)
else
q
case query.filters["event:name"] do
{:is, name} -> from(e in q, where: e.name == ^name)
{:member, list} -> from(e in q, where: e.name in ^list)
_ -> q
end
Enum.reduce(query.filters, q, fn {filter_key, filter_value}, query ->
case filter_key do
"event:props:" <> prop_name ->
filter_value = elem(filter_value, 1)
if filter_value == "(none)" do
from(
e in query,
@ -85,6 +85,14 @@ defmodule Plausible.Stats.Base do
end)
end
@api_prop_name_to_db %{
"source" => "referrer_source",
"device" => "screen_size",
"os" => "operating_system",
"os_version" => "operating_system_version",
"country" => "country_code"
}
def query_sessions(site, query) do
{first_datetime, last_datetime} = utc_boundaries(query, site.timezone)
@ -96,36 +104,40 @@ defmodule Plausible.Stats.Base do
sessions_q =
if query.filters["event:page"] do
page = query.filters["event:page"]
from(e in sessions_q, where: e.entry_page == ^page)
case query.filters["event:page"] do
{:is, page} ->
from(e in sessions_q, where: e.entry_page == ^page)
{:member, list} ->
from(e in sessions_q, where: e.entry_page in ^list)
end
else
sessions_q
end
Enum.reduce(@session_props, sessions_q, fn prop_name, sessions_q ->
prop_val = query.filters["visit:" <> prop_name]
prop_name = if prop_name == "source", do: "referrer_source", else: prop_name
prop_name = if prop_name == "device", do: "screen_size", else: prop_name
prop_name = if prop_name == "os", do: "operating_system", else: prop_name
prop_name = if prop_name == "os_version", do: "operating_system_version", else: prop_name
prop_name = if prop_name == "country", do: "country_code", else: prop_name
prop_val =
if prop_name == "referrer_source" && prop_val == @no_ref do
""
else
prop_val
end
filter = query.filters["visit:" <> prop_name]
prop_name = Map.get(@api_prop_name_to_db, prop_name, prop_name)
if prop_val do
where_target = [{String.to_existing_atom(prop_name), prop_val}]
from(s in sessions_q, where: ^where_target)
else
sessions_q
case filter do
{:is, value} ->
where_target = [{String.to_existing_atom(prop_name), db_prop_val(prop_name, value)}]
from(s in sessions_q, where: ^where_target)
{:member, values} ->
list = Enum.map(values, &db_prop_val(prop_name, &1))
fragment_data = [{String.to_existing_atom(prop_name), {:in, list}}]
from(s in sessions_q, where: fragment(^fragment_data))
_ ->
sessions_q
end
end)
end
defp db_prop_val("referrer_source", @no_ref), do: ""
defp db_prop_val(_, val), do: val
defp utc_boundaries(%Query{date_range: date_range}, timezone) do
{:ok, first} = NaiveDateTime.new(date_range.first, ~T[00:00:00])

@ -71,9 +71,7 @@ defmodule Plausible.Stats.Breakdown do
end
defp filter_converted_sessions(db_query, site, query) do
event = query.filters["event:name"]
if is_binary(event) do
if query.filters["event:name"] do
converted_sessions =
from(e in query_events(site, query),
select: %{session_id: fragment("DISTINCT ?", e.session_id)}

@ -1180,7 +1180,7 @@ defmodule Plausible.Stats.Clickhouse do
)
q =
if query.filters["source"] || query.filters['referrer'] || query.filters["utm_medium"] ||
if query.filters["source"] || query.filters["referrer"] || query.filters["utm_medium"] ||
query.filters["utm_source"] || query.filters["utm_campaign"] || query.filters["screen"] ||
query.filters["browser"] || query.filters["browser_version"] || query.filters["os"] ||
query.filters["os_version"] || query.filters["country"] || query.filters["entry_page"] ||
@ -1240,7 +1240,7 @@ defmodule Plausible.Stats.Clickhouse do
where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime
)
if query.filters["source"] || query.filters['referrer'] || query.filters["utm_medium"] ||
if query.filters["source"] || query.filters["referrer"] || query.filters["utm_medium"] ||
query.filters["utm_source"] || query.filters["utm_campaign"] || query.filters["screen"] ||
query.filters["browser"] || query.filters["browser_version"] || query.filters["os"] ||
query.filters["os_version"] || query.filters["country"] || query.filters["entry_page"] ||

@ -182,9 +182,14 @@ defmodule Plausible.Stats.Query do
end
defp parse_single_filter(str) do
String.trim(str)
|> String.split("==")
|> Enum.map(&String.trim/1)
|> List.to_tuple()
[key, val] =
String.trim(str)
|> String.split("==")
|> Enum.map(&String.trim/1)
case String.split(val, "|") do
[single_value] -> {key, {:is, single_value}}
list -> {key, {:member, list}}
end
end
end

@ -220,8 +220,14 @@ defmodule PlausibleWeb.Api.ExternalController do
end
defp parse_body(conn) do
{:ok, body, _conn} = Plug.Conn.read_body(conn)
Jason.decode!(body)
case conn.body_params do
%Plug.Conn.Unfetched{} ->
{:ok, body, _conn} = Plug.Conn.read_body(conn)
Jason.decode!(body)
params ->
params
end
end
defp strip_www(nil), do: nil

@ -21,4 +21,11 @@ defmodule PlausibleWeb.Api.Helpers do
|> Phoenix.Controller.json(%{error: msg})
|> halt()
end
def too_many_requests(conn, msg) do
conn
|> put_status(429)
|> Phoenix.Controller.json(%{error: msg})
|> halt()
end
end

@ -227,41 +227,77 @@ defmodule PlausibleWeb.AuthController do
end
def login(conn, %{"email" => email, "password" => password}) do
alias Plausible.Auth.Password
with :ok <- check_ip_rate_limit(conn),
{:ok, user} <- find_user(email),
:ok <- check_user_rate_limit(user),
:ok <- check_password(user, password) do
login_dest = get_session(conn, :login_dest) || "/sites"
user =
Repo.one(
from u in Plausible.Auth.User,
where: u.email == ^email
conn
|> put_session(:current_user_id, user.id)
|> put_resp_cookie("logged_in", "true",
http_only: false,
max_age: 60 * 60 * 24 * 365 * 5000
)
|> put_session(:login_dest, nil)
|> redirect(to: login_dest)
else
:wrong_password ->
render(conn, "login_form.html",
error: "Wrong email or password. Please try again.",
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
if user do
if Password.match?(password, user.password_hash || "") do
login_dest = get_session(conn, :login_dest) || "/sites"
:user_not_found ->
Plausible.Auth.Password.dummy_calculation()
conn
|> put_session(:current_user_id, user.id)
|> put_resp_cookie("logged_in", "true",
http_only: false,
max_age: 60 * 60 * 24 * 365 * 5000
)
|> put_session(:login_dest, nil)
|> redirect(to: login_dest)
else
conn
|> render("login_form.html",
render(conn, "login_form.html",
error: "Wrong email or password. Please try again.",
layout: {PlausibleWeb.LayoutView, "focus.html"}
)
end
else
Password.dummy_calculation()
conn
|> render("login_form.html",
error: "Wrong email or password. Please try again.",
layout: {PlausibleWeb.LayoutView, "focus.html"}
{:rate_limit, _} ->
render_error(
conn,
429,
"Too many login attempts. Wait a minute before trying again."
)
end
end
@login_interval 60_000
@login_limit 5
defp check_ip_rate_limit(conn) do
ip_address = PlausibleWeb.RemoteIp.get(conn)
case Hammer.check_rate("login:ip:#{ip_address}", @login_interval, @login_limit) do
{:allow, _} -> :ok
{:deny, _} -> {:rate_limit, :ip_address}
end
end
defp find_user(email) do
user =
Repo.one(
from u in Plausible.Auth.User,
where: u.email == ^email
)
if user, do: {:ok, user}, else: :user_not_found
end
defp check_user_rate_limit(user) do
case Hammer.check_rate("login:user:#{user.id}", @login_interval, @login_limit) do
{:allow, _} -> :ok
{:deny, _} -> {:rate_limit, :user}
end
end
defp check_password(user, password) do
if Plausible.Auth.Password.match?(password, user.password_hash || "") do
:ok
else
:wrong_password
end
end

@ -9,7 +9,9 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
end
def call(conn, _opts) do
with {:ok, api_key} <- get_bearer_token(conn),
with {:ok, token} <- get_bearer_token(conn),
{:ok, api_key} <- find_api_key(token),
:ok <- check_api_key_rate_limit(api_key),
{:ok, site} <- verify_access(api_key, conn.params["site_id"]) do
assign(conn, :site, site)
else
@ -25,6 +27,12 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
"Missing site ID. Please provide the required site_id parameter with your request."
)
{:error, :rate_limit, limit} ->
H.too_many_requests(
conn,
"Too many API requests. Your API key is limited to #{limit} requests per hour."
)
{:error, :invalid_api_key} ->
H.unauthorized(
conn,
@ -36,13 +44,11 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
defp verify_access(_api_key, nil), do: {:error, :missing_site_id}
defp verify_access(api_key, site_id) do
hashed_key = ApiKey.do_hash(api_key)
found_key = Repo.get_by(ApiKey, key_hash: hashed_key)
site = Repo.get_by(Plausible.Site, domain: site_id)
is_owner = site && found_key && Plausible.Sites.is_owner?(found_key.user_id, site)
is_owner = site && Plausible.Sites.is_owner?(api_key.user_id, site)
cond do
found_key && site && is_owner -> {:ok, site}
site && is_owner -> {:ok, site}
true -> {:error, :invalid_api_key}
end
end
@ -57,4 +63,18 @@ defmodule PlausibleWeb.AuthorizeStatsApiPlug do
_ -> {:error, :missing_api_key}
end
end
defp find_api_key(token) do
hashed_key = ApiKey.do_hash(token)
found_key = Repo.get_by(ApiKey, key_hash: hashed_key)
if found_key, do: {:ok, found_key}, else: {:error, :invalid_api_key}
end
@one_hour 60 * 60 * 1000
defp check_api_key_rate_limit(api_key) do
case Hammer.check_rate("api_request:#{api_key.id}", @one_hour, api_key.hourly_request_limit) do
{:allow, _} -> :ok
{:deny, _} -> {:error, :rate_limit, api_key.hourly_request_limit}
end
end
end

@ -38,9 +38,6 @@ defmodule PlausibleWeb.Tracker do
@templates files_available
@aliases aliases_available
#  1 hour
@max_age 3600
def init(_) do
all_files =
Enum.reduce(@templates, %{}, fn template_filename, all_files ->
@ -64,7 +61,6 @@ defmodule PlausibleWeb.Tracker do
location = Application.app_dir(:plausible, "priv/tracker/js/" <> found)
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_file(200, location)

@ -7,22 +7,38 @@ defmodule PlausibleWeb.RemoteIp do
cond do
cf_connecting_ip ->
cf_connecting_ip
|> clean_ip
forwarded_for ->
String.split(forwarded_for, ",")
|> Enum.map(&String.trim/1)
|> List.first()
|> clean_ip
forwarded ->
Regex.named_captures(~r/for=(?<for>[^;,]+).*$/, forwarded)
|> Map.get("for")
# IPv6 addresses are enclosed in quote marks and square brackets: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
|> String.trim("\"")
|> String.trim_leading("[")
|> String.trim_trailing("]")
|> clean_ip
true ->
to_string(:inet_parse.ntoa(conn.remote_ip))
end
end
# Removes port from both IPv4 and IPv6 addresses. From https://regexr.com/3hpvt
# Removes surrounding [] of an IPv6 address
@port_regex ~r/((\.\d+)|(\]))(?<port>:[0-9]+)$/
defp clean_ip(ip_and_port) do
ip =
case Regex.named_captures(@port_regex, ip_and_port) do
%{"port" => port} -> String.trim_trailing(ip_and_port, port)
_ -> ip_and_port
end
ip
|> String.trim_leading("[")
|> String.trim_trailing("]")
end
end

@ -35,7 +35,7 @@ defmodule PlausibleWeb.SiteView do
end
"""
<script async defer data-domain="#{site.domain}" src="#{tracker}"></script>
<script defer data-domain="#{site.domain}" src="#{tracker}"></script>
"""
end
end

@ -95,8 +95,8 @@ defmodule Plausible.MixProject do
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},
{:kaffy, "~> 0.9.0"},
{:envy, "~> 1.1.1"},
{:ink, "~> 1.0"},
{:phoenix_pagination, "~> 0.7.0"}
{:phoenix_pagination, "~> 0.7.0"},
{:hammer, "~> 6.0"}
]
end

@ -12,7 +12,7 @@
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
"cachex": {:hex, :cachex, "3.3.0", "6f2ebb8f27491fe39121bd207c78badc499214d76c695658b19d6079beeca5c2", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "d90e5ee1dde14cef33f6b187af4335b88748b72b30c038969176cd4e6ccc31a1"},
"certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"},
"clickhouse_ecto": {:git, "https://github.com/plausible/clickhouse_ecto.git", "1969f14ecef7c357b2bd8bdc3e566234269de58c", []},
"clickhouse_ecto": {:git, "https://github.com/plausible/clickhouse_ecto.git", "07adb8da725346e4de6d376069192e9942fe7a5b", []},
"clickhousex": {:git, "https://github.com/plausible/clickhousex", "0832dd4b1af1f0eba1d1018c231bf0d8d281f031", []},
"combination": {:hex, :combination, "0.0.3", "746aedca63d833293ec6e835aa1f34974868829b1486b1e1cb0685f0b2ae1f41", [:mix], [], "hexpm", "72b099f463df42ef7dc6371d250c7070b57b6c5902853f69deb894f79eda18ca"},
"combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"},
@ -44,6 +44,7 @@
"geolix_adapter_mmdb2": {:hex, :geolix_adapter_mmdb2, "0.5.0", "5912723d9538ecddc6b29b1d8041b917b735a78fd3c122bfea8c44aa782e3369", [:mix], [{:geolix, "~> 1.1", [hex: :geolix, repo: "hexpm", optional: false]}, {:mmdb2_decoder, "~> 3.0", [hex: :mmdb2_decoder, repo: "hexpm", optional: false]}], "hexpm", "cb1485b6a0a2d3e541949207428a245718dbf1258453a0df0e5fdd925bcecd3e"},
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
"hackney": {:hex, :hackney, "1.17.4", "99da4674592504d3fb0cfef0db84c3ba02b4508bae2dff8c0108baa0d6e0977c", [:rebar3], [{:certifi, "~>2.6.1", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "de16ff4996556c8548d512f4dbe22dd58a587bf3332e7fd362430a7ef3986b16"},
"hammer": {:hex, :hammer, "6.0.0", "72ec6fff10e9d63856968988a22ee04c4d6d5248071ddccfbda50aa6c455c1d7", [:mix], [{:poolboy, "~> 1.5", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "d8e1ec2e534c4aae508b906759e077c3c1eb3e2b9425235d4b7bbab0b016210a"},
"httpoison": {:hex, :httpoison, "1.8.0", "6b85dea15820b7804ef607ff78406ab449dd78bed923a49c7160e1886e987a3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "28089eaa98cf90c66265b6b5ad87c59a3729bea2e74e9d08f9b51eb9729b3c3a"},
"hut": {:hex, :hut, "1.3.0", "71f2f054e657c03f959cf1acc43f436ea87580696528ca2a55c8afb1b06c85e7", [:"erlang.mk", :rebar, :rebar3], [], "hexpm", "7e15d28555d8a1f2b5a3a931ec120af0753e4853a4c66053db354f35bf9ab563"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},

@ -0,0 +1,9 @@
defmodule Plausible.Repo.Migrations.AddRateLimitToApiKeys do
use Ecto.Migration
def change do
alter table(:api_keys) do
add :hourly_request_limit, :integer, null: false, default: 1000
end
end
end

@ -1 +1 @@
!function(r,a){"use strict";function o(e){console.warn("[Plausible] Ignoring event because "+e)}function l(){var e,i,t=JSON.parse((e="plausible_user",(i=document.cookie.match(new RegExp("(?:^|; )"+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")+"=([^;]*)")))?decodeURIComponent(i[1]):null));return t?{initial_referrer:t.initial_referrer&&decodeURIComponent(t.initial_referrer),initial_source:t.initial_source&&decodeURIComponent(t.initial_source)}:(t={initial_referrer:r.document.referrer||null,initial_source:getSourceFromQueryParam()},function(e,i){var t=new Date;t.setTime(t.getTime()+94608e6);var n="; expires="+t.toUTCString();document.cookie=e+"="+(i||"")+n+"; samesite=strict; path=/"}("plausible_user",JSON.stringify({initial_referrer:t.initial_referrer&&encodeURIComponent(t.initial_referrer),initial_source:t.initial_source&&encodeURIComponent(t.initial_source)})),t)}function i(e,i){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(r.location.hostname))return o("website is running locally");if("file:"===r.location.protocol)return o("website is running locally");if("prerender"===r.document.visibilityState)return o("document is prerendering");var t=s.trackAcquisition?l():{};t.n=e,t.u=r.location.href,t.d=s.domain,t.r=r.document.referrer||null,t.w=r.innerWidth;var n=new XMLHttpRequest;n.open("POST",a+"/api/event",!0),n.setRequestHeader("Content-Type","text/plain"),n.send(JSON.stringify(t)),n.onreadystatechange=function(){n.readyState==XMLHttpRequest.DONE&&i&&i.callback&&i.callback()}}function t(e){i("pageview",e)}try{var s={domain:r.location.hostname},n={page:t,trigger:i,trackPushState:function(){var e,i=r.history;i.pushState&&(e=i.pushState,i.pushState=function(){e.apply(this,arguments),t()}),r.addEventListener("popstate",t)},configure:function(e,i){s[e]=i}},e=r.plausible.q||[];r.plausible=function(){var e=[].slice.call(arguments),i=e.shift();n[i].apply(this,e)};for(var c=0;c<e.length;c++)r.plausible.apply(this,e[c])}catch(e){(new Image).src=a+"/api/error?message="+encodeURIComponent(e.message)}}(window,"<%= base_url %>");
!function(){"use strict";var e=window.document.currentScript,r=new URL(e.src).origin;function o(e){console.warn("[Plausible] Ignoring event because "+e)}function a(){var e,i,n=JSON.parse((e="plausible_user",(i=document.cookie.match(new RegExp("(?:^|; )"+e.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g,"\\$1")+"=([^;]*)")))?decodeURIComponent(i[1]):null));return n?{initial_referrer:n.initial_referrer&&decodeURIComponent(n.initial_referrer),initial_source:n.initial_source&&decodeURIComponent(n.initial_source)}:(n={initial_referrer:window.document.referrer||null,initial_source:getSourceFromQueryParam()},function(e,i){var n=new Date;n.setTime(n.getTime()+94608e6);var t="; expires="+n.toUTCString();document.cookie=e+"="+(i||"")+t+"; samesite=strict; path=/"}("plausible_user",JSON.stringify({initial_referrer:n.initial_referrer&&encodeURIComponent(n.initial_referrer),initial_source:n.initial_source&&encodeURIComponent(n.initial_source)})),n)}function i(e,i){if(/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(window.location.hostname))return o("website is running locally");if("file:"===window.location.protocol)return o("website is running locally");if("prerender"===window.document.visibilityState)return o("document is prerendering");var n=c.trackAcquisition?a():{};n.n=e,n.u=window.location.href,n.d=c.domain,n.r=window.document.referrer||null,n.w=window.innerWidth;var t=new XMLHttpRequest;t.open("POST",r+"/api/event",!0),t.setRequestHeader("Content-Type","text/plain"),t.send(JSON.stringify(n)),t.onreadystatechange=function(){t.readyState==XMLHttpRequest.DONE&&i&&i.callback&&i.callback()}}function n(e){i("pageview",e)}try{var c={domain:window.location.hostname},t={page:n,trigger:i,trackPushState:function(){var e,i=window.history;i.pushState&&(e=i.pushState,i.pushState=function(){e.apply(this,arguments),n()}),window.addEventListener("popstate",n)},configure:function(e,i){c[e]=i}},s=window.plausible.q||[];window.plausible=function(){var e=[].slice.call(arguments),i=e.shift();t[i].apply(this,e)};for(var l=0;l<s.length;l++)window.plausible.apply(this,s[l])}catch(e){(new Image).src=r+"/api/error?message="+encodeURIComponent(e.message)}}();

@ -1,5 +0,0 @@
defmodule Plausible.Billing.PlansTest do
use Plausible.DataCase
use Bamboo.Test, shared: true
alias Plausible.Billing.Plans
end

@ -26,9 +26,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-1.com")
@ -38,6 +37,27 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
assert pageview.pathname == "/"
end
test "works with Content-Type: text/plain", %{conn: conn} do
params = %{
domain: "external-controller-test-text-plain.com",
name: "pageview",
url: "http://gigride.live/",
screen_width: 1440
}
conn =
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
pageview = get_event("external-controller-test-text-plain.com")
assert response(conn, 202) == ""
assert pageview.hostname == "gigride.live"
assert pageview.domain == "external-controller-test-text-plain.com"
assert pageview.pathname == "/"
end
test "can send to multiple dashboards by listing multiple domains", %{conn: conn} do
params = %{
name: "pageview",
@ -49,9 +69,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
assert response(conn, 202) == ""
assert get_event("test-domain1.com")
@ -66,8 +85,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-2.com")
@ -82,8 +100,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-3.com")
@ -98,8 +115,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-4.com")
@ -114,9 +130,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", "generic crawler")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
assert get_event("external-controller-test-5.com") == nil
end
@ -129,12 +144,11 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header(
"user-agent",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/85.0.4183.83 Safari/537.36"
)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
assert get_event("headless-chrome-test.com") == nil
end
@ -148,9 +162,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-6.com")
@ -171,9 +184,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-7.com")
@ -191,9 +203,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-8.com")
@ -212,9 +223,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-9.com")
@ -232,9 +242,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-10.com")
@ -252,9 +261,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-11.com")
@ -271,8 +279,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-12.com")
@ -288,8 +295,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-13.com")
@ -305,8 +311,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-utm-tags.com")
@ -325,9 +330,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-14.com")
@ -345,9 +349,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-15.com")
@ -366,9 +369,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-16.com")
@ -385,9 +387,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-17.com")
@ -404,9 +405,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
event = get_event("external-controller-test-18.com")
@ -427,8 +427,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
event = get_event("custom-prop-test.com")
@ -445,8 +444,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
event = get_event("custom-prop-test-2.com")
@ -464,9 +462,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
event = get_event("external-controller-test-19.com")
@ -483,15 +480,62 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-20.com")
assert pageview.country_code == "US"
end
test "scrubs port from x-forwarded-for", %{conn: conn} do
params = %{
name: "pageview",
domain: "external-controller-test-x-forwarded-for-port.com",
url: "http://gigride.live/"
}
conn
|> put_req_header("x-forwarded-for", "1.1.1.1:123")
|> post("/api/event", params)
pageview = get_event("external-controller-test-x-forwarded-for-port.com")
assert pageview.country_code == "US"
end
test "works with ipv6 without port in x-forwarded-for", %{conn: conn} do
params = %{
name: "pageview",
domain: "external-controller-test-x-forwarded-for-ipv6.com",
url: "http://gigride.live/"
}
conn
|> put_req_header("x-forwarded-for", "1:1:1:1:1:1:1:1")
|> post("/api/event", params)
pageview = get_event("external-controller-test-x-forwarded-for-ipv6.com")
assert pageview.country_code == "US"
end
test "works with ipv6 with a port number in x-forwarded-for", %{conn: conn} do
params = %{
name: "pageview",
domain: "external-controller-test-x-forwarded-for-ipv6-port.com",
url: "http://gigride.live/"
}
conn
|> put_req_header("x-forwarded-for", "[1:1:1:1:1:1:1:1]:123")
|> post("/api/event", params)
pageview = get_event("external-controller-test-x-forwarded-for-ipv6-port.com")
assert pageview.country_code == "US"
end
test "uses cloudflare's special header for client IP address if present", %{conn: conn} do
params = %{
name: "pageview",
@ -500,10 +544,9 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("x-forwarded-for", "0.0.0.0")
|> put_req_header("cf-connecting-ip", "1.1.1.1")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-cloudflare.com")
@ -520,9 +563,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("forwarded", "by=0.0.0.0;for=1.1.1.1;host=somehost.com;proto=https")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-forwarded.com")
@ -537,12 +579,11 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header(
"forwarded",
"by=0.0.0.0;for=\"[1:1:1:1:1:1:1:1]\",for=0.0.0.0;host=somehost.com;proto=https"
)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-forwarded-ipv6.com")
@ -558,8 +599,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-21.com")
@ -576,8 +616,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-22.com")
@ -596,8 +635,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("external-controller-test-23.com")
@ -614,8 +652,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("url-decode-test.com")
@ -635,8 +672,7 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
}
conn
|> put_req_header("content-type", "text/plain")
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
pageview = get_event("quote-encode-test.com")
@ -651,9 +687,8 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do
conn =
conn
|> put_req_header("content-type", "text/plain")
|> put_req_header("user-agent", @user_agent)
|> post("/api/event", Jason.encode!(params))
|> post("/api/event", params)
assert response(conn, 400) == ""
end

@ -51,4 +51,29 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do
"Missing site ID. Please provide the required site_id parameter with your request."
}
end
test "limits the rate of API requests", %{user: user} do
api_key = insert(:api_key, user_id: user.id, hourly_request_limit: 3)
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> get("/api/v1/stats/aggregate")
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> get("/api/v1/stats/aggregate")
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> get("/api/v1/stats/aggregate")
conn =
build_conn()
|> Plug.Conn.put_req_header("authorization", "Bearer #{api_key.key}")
|> get("/api/v1/stats/aggregate")
assert json_response(conn, 429) == %{
"error" => "Too many API requests. Your API key is limited to 3 requests per hour."
}
end
end

@ -776,6 +776,175 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do
]
}
end
test "IN filter for event:page", %{conn: conn, site: site} do
populate_stats([
build(:pageview,
pathname: "/ignore",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/important-page",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
)
])
conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:page",
"filters" => "event:page == /plausible.io|/important-page"
})
assert json_response(conn, 200) == %{
"results" => [
%{"page" => "/plausible.io", "visitors" => 2},
%{"page" => "/important-page", "visitors" => 1}
]
}
end
test "IN filter for visit:browser", %{conn: conn, site: site} do
populate_stats([
build(:pageview,
pathname: "/ignore",
browser: "Firefox",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/plausible.io",
browser: "Chrome",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/plausible.io",
browser: "Safari",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/important-page",
browser: "Safari",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
)
])
conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:page",
"filters" => "visit:browser == Chrome|Safari"
})
assert json_response(conn, 200) == %{
"results" => [
%{"page" => "/plausible.io", "visitors" => 2},
%{"page" => "/important-page", "visitors" => 1}
]
}
end
test "IN filter for visit:entry_page", %{conn: conn, site: site} do
populate_stats([
build(:pageview,
pathname: "/ignore",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/plausible.io",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:pageview,
pathname: "/important-page",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
)
])
conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:page",
"filters" => "event:page == /plausible.io|/important-page",
"metrics" => "bounce_rate"
})
assert json_response(conn, 200) == %{
"results" => [
%{"page" => "/plausible.io", "bounce_rate" => 100},
%{"page" => "/important-page", "bounce_rate" => 100}
]
}
end
test "IN filter for event:name", %{conn: conn, site: site} do
populate_stats([
build(:event,
name: "Signup",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:event,
name: "Signup",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:event,
name: "Login",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
),
build(:event,
name: "Irrelevant",
domain: site.domain,
timestamp: ~N[2021-01-01 00:00:00]
)
])
conn =
get(conn, "/api/v1/stats/breakdown", %{
"site_id" => site.domain,
"period" => "day",
"date" => "2021-01-01",
"property" => "event:name",
"filters" => "event:name == Signup|Login"
})
assert json_response(conn, 200) == %{
"results" => [
%{"name" => "Signup", "visitors" => 2},
%{"name" => "Login", "visitors" => 1}
]
}
end
end
describe "pagination" do

@ -203,6 +203,38 @@ defmodule PlausibleWeb.AuthControllerTest do
assert get_session(conn, :current_user_id) == nil
assert html_response(conn, 200) =~ "Enter your email and password"
end
test "limits login attempts to 5 per minute" do
user = insert(:user, password: "password")
build_conn()
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/login", email: user.email, password: "wrong")
build_conn()
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/login", email: user.email, password: "wrong")
build_conn()
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/login", email: user.email, password: "wrong")
build_conn()
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/login", email: user.email, password: "wrong")
build_conn()
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/login", email: user.email, password: "wrong")
conn =
build_conn()
|> put_req_header("x-forwarded-for", "1.1.1.1")
|> post("/login", email: user.email, password: "wrong")
assert get_session(conn, :current_user_id) == nil
assert html_response(conn, 429) =~ "Too many login attempts"
end
end
describe "GET /password/request-reset" do

@ -5,6 +5,7 @@
(function(){
'use strict';
var scriptEl = window.document.currentScript;
var plausibleHost = new URL(scriptEl.src).origin
function setCookie(name,value) {

Loading…
Cancel
Save