Skip to content

Commit 2760c0b

Browse files
saltedlollyclaude
andcommitted
Short links count where they were shown, from a tag like ?dnt (DigiNode Tools)
Known tags (dnt, bsky, tg, x) are counted by name; any other tag as itself; without one, the website the link was on, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6f787db commit 2760c0b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

‎build/build.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@
7474
# The preview cards shown when a DigiFacts link is shared (cards/: the PNGs, and the HTML they were drawn from).
7575
# Each DigiFact always uses the same one (chosen by its ID), so a feed of DigiFacts alternates between them.
7676
CARDS = ["cards/card-1.png", "cards/card-2.png"]
77+
# Tags that say where a short link was shown, e.g. https://go.diginode.tools/7ttm?dnt - counted as the click's source
78+
# ("DigiNode Tools"). Any other tag is counted as itself. QR codes can't contain "?" (they'd lose their compact
79+
# mode) - the capitals links count as "QR code" instead.
80+
SOURCE_TAGS = {"dnt": "DigiNode Tools", "bsky": "Bluesky", "tg": "Telegram", "x": "X"}
7781
USER_AGENT = f"DigiFacts builder ({REPO_URL})"
7882

7983
LANGUAGE_NAMES = {
@@ -580,7 +584,12 @@ def redirect_page(url, key, title, qr, number=0, text=""):
580584
u = html.escape(url, quote=True)
581585
count = urllib.parse.urlencode({"p": "/" + key, "t": title, "r": "QR code" if qr else ""})
582586
script = (f"try{{var c={json.dumps(STATS_URL + '/count?' + count)};"
583-
+ ("" if qr else "if(document.referrer)c=c.replace('&r=','&r='+encodeURIComponent(document.referrer));")
587+
+ ("" if qr else
588+
# Where the click came from: a tag on the link (e.g. ?dnt from DigiNode Tools - see SOURCE_TAGS), or
589+
# else the website it was on
590+
f"var v=(location.search.match(/^\\?([a-z0-9-]{{1,20}})/i)||[])[1],n={json.dumps(SOURCE_TAGS)};"
591+
"if(v)c=c.replace('&r=','&r='+encodeURIComponent(n[v.toLowerCase()]||v));"
592+
"else if(document.referrer)c=c.replace('&r=','&r='+encodeURIComponent(document.referrer));")
584593
+ "c+='&rnd='+Math.random().toString(36).slice(2);"
585594
"if(navigator.sendBeacon)navigator.sendBeacon(c);else(new Image()).src=c}catch(e){}"
586595
f"location.replace({json.dumps(url)})").replace("</", "<\\/")

0 commit comments

Comments
 (0)