analytics/lib/plausible_web/plugs/tracker.ex

74 lines
2.1 KiB
Elixir
Raw Normal View History

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
2021-10-10 14:07:37 +03:00
base_variants = ["hash", "outbound-links", "exclusions", "compat", "local", "manual"]
2021-09-23 12:57:48 +03:00
base_filenames = ["plausible", "script"]
# Generates Power Set of all variants
variants =
1..Enum.count(base_variants)
|> Enum.map(fn x ->
Combination.combine(base_variants, x)
|> Enum.map(fn y -> Enum.sort(y) |> Enum.join(".") end)
end)
|> List.flatten()
# Formats power set into filenames
files_available =
["plausible.js", "p.js"] ++ Enum.map(variants, fn v -> "plausible.#{v}.js" end)
# Computes permutations for every power set elements, formats them as alias filenames
aliases_available =
Enum.map(variants, fn x ->
variants =
String.split(x, ".")
|> Combination.permutate()
|> Enum.map(fn p -> Enum.join(p, ".") end)
|> Enum.map(fn v -> Enum.map(base_filenames, fn filename -> "#{filename}.#{v}.js" end) end)
|> List.flatten()
if Enum.count(variants) > 0 do
{"plausible.#{x}.js", variants}
end
end)
|> Enum.reject(fn x -> x == nil end)
|> Enum.into(%{})
2021-09-23 12:57:48 +03:00
|> Map.put("plausible.js", ["analytics.js", "script.js"])
@templates files_available
@aliases aliases_available
def init(_) do
all_files =
Enum.reduce(@templates, %{}, fn template_filename, all_files ->
2020-11-03 12:20:11 +03:00
aliases = Map.get(@aliases, template_filename, [])
[template_filename | aliases]
|> Enum.map(fn filename -> {"/js/" <> filename, template_filename} end)
2020-11-03 12:20:11 +03:00
|> Enum.into(%{})
|> Map.merge(all_files)
2020-11-03 12:20:11 +03:00
end)
[files: all_files]
end
def call(conn, files: files) do
case files[conn.request_path] do
nil ->
conn
2020-11-03 12:35:08 +03:00
found ->
location = Application.app_dir(:plausible, "priv/tracker/js/" <> found)
conn
|> put_resp_header("content-type", "application/javascript")
|> put_resp_header("x-content-type-options", "nosniff")
|> put_resp_header("cross-origin-resource-policy", "cross-origin")
|> put_resp_header("access-control-allow-origin", "*")
|> send_file(200, location)
|> halt()
end
end
end