-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
tracker.ex
78 lines (66 loc) · 1.97 KB
/
tracker.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
defmodule PlausibleWeb.Tracker do
import Plug.Conn
use Agent
base_variants = [
"hash",
"outbound-links",
"exclusions",
"compat",
"local",
"manual",
"file-downloads",
"pageview-props",
"tagged-events",
"revenue",
"pageleave"
]
# 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()
@base_filenames ["plausible", "script", "analytics"]
@files_available ["plausible.js", "p.js"] ++ Enum.map(variants, fn v -> "plausible.#{v}.js" end)
def init(opts) do
Keyword.merge(opts, files_available: MapSet.new(@files_available))
end
def call(conn, files_available: files_available) do
filename =
case conn.request_path do
"/js/p.js" ->
"p.js"
"/js/" <> requested_filename ->
sorted_script_variant(requested_filename)
_ ->
nil
end
if filename && MapSet.member?(files_available, filename) do
location = Application.app_dir(:plausible, "priv/tracker/js/" <> filename)
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", "*")
|> put_resp_header("cache-control", "public, max-age=86400, must-revalidate")
|> send_file(200, location)
|> halt()
else
conn
end
end
defp sorted_script_variant(requested_filename) do
case String.split(requested_filename, ".") do
[base_filename | rest] when base_filename in @base_filenames ->
sorted_variants =
rest
|> List.delete("js")
|> Enum.sort()
Enum.join(["plausible"] ++ sorted_variants ++ ["js"], ".")
_ ->
nil
end
end
end