Skip to content

Commit

Permalink
Changes from static tracker script name assignments to dynamic (#786)
Browse files Browse the repository at this point in the history
Co-authored-by: Uku Taht <[email protected]>
  • Loading branch information
Vigasaurus and ukutaht authored May 18, 2021
1 parent 820357f commit 7f3e554
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 39 deletions.
59 changes: 35 additions & 24 deletions lib/plausible_web/plugs/tracker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,41 @@ defmodule PlausibleWeb.Tracker do
import Plug.Conn
use Agent

@templates [
"plausible.js",
"plausible.exclusions.js",
"plausible.hash.js",
"plausible.outbound-links.js",
"plausible.hash.exclusions.js",
"plausible.hash.outbound-links.js",
"plausible.hash.exclusions.outbound-links.js",
"plausible.exclusions.outbound-links.js",
"p.js"
]
@aliases %{
"plausible.js" => ["analytics.js"],
"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"
]
}
base_variants = ["hash", "outbound-links", "exclusions"]

# 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.filter(fn permutation -> permutation != x end)
|> Enum.map(fn v -> "plausible.#{v}.js" end)

if Enum.count(variants) > 0 do
{"plausible.#{x}.js", variants}
end
end)
|> Enum.reject(fn x -> x == nil end)
|> Enum.into(%{})
|> Map.put("plausible.js", ["analytics.js"])

@templates files_available
@aliases aliases_available

#  1 hour
@max_age 3600
Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule Plausible.MixProject do
defp deps do
[
{:bcrypt_elixir, "~> 2.0"},
{:combination, "~> 0.0.3"},
{:cors_plug, "~> 1.5"},
{:ecto_sql, "~> 3.0"},
{:elixir_uuid, "~> 1.2"},
Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"},
"clickhouse_ecto": {:git, "https://github.com/plausible/clickhouse_ecto.git", "1969f14ecef7c357b2bd8bdc3e566234269de58c", []},
"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"},
"comeonin": {:hex, :comeonin, "5.3.2", "5c2f893d05c56ae3f5e24c1b983c2d5dfb88c6d979c9287a76a7feb1e1d8d646", [:mix], [], "hexpm", "d0993402844c49539aeadb3fe46a3c9bd190f1ecf86b6f9ebd71957534c95f04"},
"connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"},
Expand Down
1 change: 1 addition & 0 deletions priv/tracker/js/plausible.exclusions.hash.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions tracker/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const uglify = require("uglify-js");
const fs = require('fs')
const path = require('path')
const Handlebars = require("handlebars");
const g = require("generatorics");

function relPath(segment) {
return path.join(__dirname, segment)
Expand All @@ -15,13 +16,14 @@ function compilefile(input, output, templateVars = {}) {
fs.writeFileSync(output, result.code)
}

const base_variants = ["hash", "outbound-links", "exclusions"]
const variants = [...g.clone.powerSet(base_variants)].filter(a => a.length > 0).map(a => a.sort());

compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.js'))
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.exclusions.js'), {exclusionMode: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.js'), {hashMode: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.exclusions.js'), {hashMode: true, exclusionMode: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.outbound-links.js'), {outboundLinks: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.exclusions.outbound-links.js'), {outboundLinks: true, exclusionMode: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.outbound-links.js'), {hashMode: true, outboundLinks: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.hash.exclusions.outbound-links.js'), {hashMode: true, outboundLinks: true, exclusionMode: true})
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/analytics.js'))
compilefile(relPath('src/p.js'), relPath('../priv/tracker/js/p.js'))
fs.copyFileSync(relPath('../priv/tracker/js/plausible.js'), relPath('../priv/tracker/js/analytics.js'))

variants.map(variant => {
const options = variant.map(variant => variant.replace('-', '_')).reduce((acc, curr) => (acc[curr] = true, acc), {})
compilefile(relPath('src/plausible.js'), relPath(`../priv/tracker/js/plausible.${variant.join('.')}.js`), options)
})
14 changes: 14 additions & 0 deletions tracker/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tracker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
},
"license": "MIT",
"dependencies": {
"generatorics": "^1.1.0",
"handlebars": "^4.7.7",
"uglify-js": "^3.9.4"
}
Expand Down
14 changes: 7 additions & 7 deletions tracker/src/plausible.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var scriptEl = document.querySelector('[src*="' + plausibleHost +'"]')
var domain = scriptEl && scriptEl.getAttribute('data-domain')
var plausible_ignore = window.localStorage.plausible_ignore;
{{#if exclusionMode}}
{{#if exclusions}}
var excludedPaths = scriptEl && scriptEl.getAttribute('data-exclude').split(',');
{{/if}}
var lastPage;
Expand All @@ -20,7 +20,7 @@
if (/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/.test(location.hostname) || location.protocol === 'file:') return warn('localhost');
if (window.phantom || window._phantom || window.__nightmare || window.navigator.webdriver || window.Cypress) return;
if (plausible_ignore=="true") return warn('localStorage flag')
{{#if exclusionMode}}
{{#if exclusions}}
if (excludedPaths)
for (var i = 0; i < excludedPaths.length; i++)
if (eventName == "pageview" && location.pathname.match(new RegExp('^' + excludedPaths[i].trim().replace(/\*\*/g, '.*').replace(/([^\.])\*/g, '$1[^\\s\/]*') + '\/?$')))
Expand All @@ -39,7 +39,7 @@
if (options && options.props) {
payload.p = JSON.stringify(options.props)
}
{{#if hashMode}}
{{#if hash}}
payload.h = 1
{{/if}}

Expand All @@ -57,7 +57,7 @@
}

function page() {
{{#unless hashMode}}
{{#unless hash}}
if (lastPage === location.pathname) return;
{{/unless}}
lastPage = location.pathname
Expand All @@ -70,7 +70,7 @@
}
}

{{#if outboundLinks}}
{{#if outbound_links}}
function handleOutbound(event) {
var link = event.target;
var middle = event.type == "auxclick" && event.which == 2;
Expand Down Expand Up @@ -102,7 +102,7 @@
{{/if}}

try {
{{#if hashMode}}
{{#if hash}}
window.addEventListener('hashchange', page)
{{else}}
var his = window.history
Expand All @@ -116,7 +116,7 @@
}

{{/if}}
{{#if outboundLinks}}
{{#if outbound_links}}
registerOutboundLinkEvents()
{{/if}}

Expand Down

0 comments on commit 7f3e554

Please sign in to comment.