2020-11-03 12:09:50 +03:00
|
|
|
|
defmodule PlausibleWeb.Tracker do
|
|
|
|
|
import Plug.Conn
|
2020-11-03 12:35:08 +03:00
|
|
|
|
use Agent
|
2020-11-03 12:20:11 +03:00
|
|
|
|
|
2020-11-03 12:09:50 +03:00
|
|
|
|
@templates [
|
|
|
|
|
"plausible.js",
|
2021-01-22 14:32:40 +03:00
|
|
|
|
"plausible.exclusions.js",
|
2021-01-26 13:39:43 +03:00
|
|
|
|
"plausible.hash.js",
|
|
|
|
|
"plausible.outbound-links.js",
|
2021-01-22 14:32:40 +03:00
|
|
|
|
"plausible.hash.exclusions.js",
|
2020-11-03 12:09:50 +03:00
|
|
|
|
"plausible.hash.outbound-links.js",
|
2021-01-22 14:32:40 +03:00
|
|
|
|
"plausible.hash.exclusions.outbound-links.js",
|
|
|
|
|
"plausible.exclusions.outbound-links.js",
|
2020-11-03 12:09:50 +03:00
|
|
|
|
"p.js"
|
|
|
|
|
]
|
|
|
|
|
@aliases %{
|
|
|
|
|
"plausible.js" => ["analytics.js"],
|
2021-01-22 14:32:40 +03:00
|
|
|
|
"plausible.hash.outbound-links.js" => ["plausible.outbound-links.hash.js"],
|
|
|
|
|
"plausible.hash.exclusions.js" => ["plausible.exclusions.hash.js"],
|
|
|
|
|
"plausible.exclusions.outbound-links.js" => ["plausible.outbound-links.exclusions.js"],
|
|
|
|
|
"plausible.hash.exclusions.outbound-links.js" => [
|
|
|
|
|
"plausible.exclusions.hash.outbound-links.js",
|
|
|
|
|
"plausible.exclusions.outbound-links.hash.js",
|
|
|
|
|
"plausible.hash.outbound-links.exclusions.js",
|
|
|
|
|
"plausible.outbound-links.hash.exclusions.js",
|
|
|
|
|
"plausible.outbound-links.exclusions.hash.js"
|
|
|
|
|
]
|
2020-11-03 12:09:50 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 1 hour
|
|
|
|
|
@max_age 3600
|
|
|
|
|
|
|
|
|
|
def init(_) do
|
2020-11-03 12:20:11 +03:00
|
|
|
|
templates =
|
|
|
|
|
Enum.reduce(@templates, %{}, fn template_filename, rendered_templates ->
|
2020-12-29 16:17:27 +03:00
|
|
|
|
rendered = EEx.compile_file("priv/tracker/js/" <> template_filename)
|
2020-11-03 12:20:11 +03:00
|
|
|
|
|
|
|
|
|
aliases = Map.get(@aliases, template_filename, [])
|
|
|
|
|
|
|
|
|
|
[template_filename | aliases]
|
|
|
|
|
|> Enum.map(fn filename -> {"/js/" <> filename, rendered} end)
|
|
|
|
|
|> Enum.into(%{})
|
|
|
|
|
|> Map.merge(rendered_templates)
|
|
|
|
|
end)
|
2020-11-03 12:09:50 +03:00
|
|
|
|
|
|
|
|
|
[templates: templates]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call(conn, templates: templates) do
|
|
|
|
|
case templates[conn.request_path] do
|
2020-12-29 16:17:27 +03:00
|
|
|
|
nil ->
|
|
|
|
|
conn
|
|
|
|
|
|
2020-11-03 12:35:08 +03:00
|
|
|
|
found ->
|
|
|
|
|
{js, _} = Code.eval_quoted(found, base_url: PlausibleWeb.Endpoint.url())
|
|
|
|
|
send_js(conn, js)
|
2020-11-03 12:09:50 +03:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp send_js(conn, file) do
|
|
|
|
|
conn
|
|
|
|
|
|> put_resp_header("cache-control", "max-age=#{@max_age},public")
|
|
|
|
|
|> put_resp_header("content-type", "application/javascript")
|
2020-11-18 09:30:43 +03:00
|
|
|
|
|> put_resp_header("cross-origin-resource-policy", "cross-origin")
|
2020-11-03 12:09:50 +03:00
|
|
|
|
|> send_resp(200, file)
|
|
|
|
|
|> halt()
|
|
|
|
|
end
|
|
|
|
|
end
|