site: every sponsor link goes through usemagpie.ai/go, so its clicks are counted per place
The home page's "Sponsored by" cards and the READMEs' Sponsors logo linked the sponsors directly, so nothing told the owner how many clicks each seat brought. They now link /go/<id>/home and https://usemagpie.ai/go/<id>/readme. site/sponsors.js holds where each sends the browser (the sponsor's own ref link, unchanged: APINEBULA has one per place). The worker counts a GET as "magpie sponsor go" {id, where, country} in PostHog, beside the in-app partners' "magpie partner go" (whose /go links were already counted), and a HEAD goes on uncounted. A partner's keys/site links are tried first; a sponsor link takes no region. A test fails if index.html or either README links a sponsor's own URL directly, or names a /go link sponsors.js doesn't have. Run: node --test site/worker.test.mjs (20 pass) and site/install.test.mjs (10 pass). Broke it by hand: README.zh-CN.md back on the direct link turns the guard red. Deployed (version e4f40cff); curl on usemagpie.ai: /go/apinebula/readme → 302 apinebula.ai/ref/qmXqFGpr, /go/apinebula/home → 302 apinebula.ai/ref/Gs54HwyB, /go/yylx/home → 302 yylx.io/?ref=magpie, /go/yylx/readme and /go/apinebula/home/x → 404; / and /zh/ serve the /go hrefs. Not checked: the events arriving in PostHog (the worker's key can't read).
This commit is contained in:
@@ -14,7 +14,7 @@ and when a quota runs out, it quietly moves on to the next account.
|
||||
    
|
||||
|
||||
<sub>Sponsors</sub><br>
|
||||
<a href="https://apinebula.ai/ref/qmXqFGpr"><picture><source media="(prefers-color-scheme: dark)" srcset="site/public/img/sponsor-apinebula-dark.svg"><img src="site/public/img/sponsor-apinebula.svg" height="36" alt="APINEBULA"></picture></a>
|
||||
<a href="https://usemagpie.ai/go/apinebula/readme"><picture><source media="(prefers-color-scheme: dark)" srcset="site/public/img/sponsor-apinebula-dark.svg"><img src="site/public/img/sponsor-apinebula.svg" height="36" alt="APINEBULA"></picture></a>
|
||||
|
||||
**[Download](https://usemagpie.ai)** · **[Docs](https://usemagpie.ai/docs/start)** · **[Reference](docs/reference.md)** · **[Plugins](https://github.com/magpie-community/plugins)** · **[Discord](https://discord.gg/vGSnD3ZKQF)** · **English** · [简体中文](README.zh-CN.md)
|
||||
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
    
|
||||
|
||||
<sub>赞助商</sub><br>
|
||||
<a href="https://apinebula.ai/ref/qmXqFGpr"><picture><source media="(prefers-color-scheme: dark)" srcset="site/public/img/sponsor-apinebula-dark.svg"><img src="site/public/img/sponsor-apinebula.svg" height="36" alt="APINEBULA"></picture></a>
|
||||
<a href="https://usemagpie.ai/go/apinebula/readme"><picture><source media="(prefers-color-scheme: dark)" srcset="site/public/img/sponsor-apinebula-dark.svg"><img src="site/public/img/sponsor-apinebula.svg" height="36" alt="APINEBULA"></picture></a>
|
||||
|
||||
**[下载](https://usemagpie.ai/zh/)** · **[文档](https://usemagpie.ai/docs/zh/start)** · **[完整参考(英文)](docs/reference.md)** · **[插件](https://github.com/magpie-community/plugins)** · **[Discord](https://discord.gg/vGSnD3ZKQF)** · [English](README.md) · **简体中文**
|
||||
|
||||
|
||||
@@ -441,13 +441,13 @@ footer a:hover { color: var(--ink); }
|
||||
<aside class="sponsor" aria-label="Sponsor">
|
||||
<p data-i18n="sponsor.by">Sponsored by</p>
|
||||
<div class="cards">
|
||||
<a class="card" href="https://yylx.io/?ref=magpie" target="_blank" rel="noopener sponsored">
|
||||
<a class="card" href="/go/yylx/home" target="_blank" rel="noopener sponsored">
|
||||
<img class="bird" src="/img/sponsor-yylx.png" width="640" height="184" alt="鱼鱼连线 YYLX.IO AI Router">
|
||||
<hr>
|
||||
<span data-i18n="sponsor.yylx">One API gateway for Claude and GPT, tuned for Claude Code.</span>
|
||||
<i aria-hidden="true">→</i>
|
||||
</a>
|
||||
<a class="card" href="https://apinebula.ai/ref/Gs54HwyB" target="_blank" rel="noopener sponsored">
|
||||
<a class="card" href="/go/apinebula/home" target="_blank" rel="noopener sponsored">
|
||||
<img class="for-light" src="/img/sponsor-apinebula.svg" width="143" height="37" alt="APINEBULA">
|
||||
<img class="for-dark" src="/img/sponsor-apinebula-dark.svg" width="143" height="37" alt="APINEBULA">
|
||||
<hr>
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// The sponsors' own links on usemagpie.ai's "Sponsored by" and in the
|
||||
// READMEs' Sponsors row. Neither page links a sponsor directly: each goes
|
||||
// to usemagpie.ai/go/<id>/<where>, which counts the click and sends the
|
||||
// browser here (worker.js), so a sponsor's clicks can be told per place.
|
||||
//
|
||||
// id a-z, 0-9 and "-"; the same as the sponsor's partners.js id when
|
||||
// it has an in-app seat too (whose /go links are keys and site)
|
||||
// home where the home page's card sends the browser
|
||||
// readme where the READMEs' logo sends it (README.md, README.zh-CN.md)
|
||||
//
|
||||
// A link stays here after its seat ends: a copy of the README or the page
|
||||
// may still point at it.
|
||||
export const SPONSORS = [
|
||||
{ id: "yylx", home: "https://yylx.io/?ref=magpie" },
|
||||
{ id: "apinebula", home: "https://apinebula.ai/ref/Gs54HwyB", readme: "https://apinebula.ai/ref/qmXqFGpr" },
|
||||
];
|
||||
+17
-3
@@ -12,6 +12,9 @@
|
||||
// their website and key links through /go
|
||||
// /go/<id>/keys[/<region>], /go/<id>/site[/<region>]
|
||||
// a partner's key page or website, counted
|
||||
// /go/<id>/home, /go/<id>/readme
|
||||
// a sponsor's link from the home page or the
|
||||
// READMEs (sponsors.js), counted
|
||||
// /api/icon?url=<https URL>
|
||||
// a picture a plugin, market or import link names,
|
||||
// fetched here so its host sees Cloudflare, not the
|
||||
@@ -31,6 +34,7 @@
|
||||
|
||||
import { LANGS } from "./i18n.js";
|
||||
import { PARTNERS } from "./partners.js";
|
||||
import { SPONSORS } from "./sponsors.js";
|
||||
|
||||
const REPO = "yetone/magpie-releases";
|
||||
const TTL = 300; // seconds the newest release is remembered
|
||||
@@ -72,13 +76,14 @@ export default {
|
||||
return json({ partners: served(PARTNERS, now) }, 200, { "Cache-Control": "public, max-age=600" });
|
||||
}
|
||||
if (url.pathname.startsWith("/go/")) {
|
||||
let id, what, region, to;
|
||||
let id, what, region, to, counted;
|
||||
try {
|
||||
[, , id, what, region] = url.pathname.split("/").map(decodeURIComponent);
|
||||
to = goTarget(PARTNERS, id, what, region);
|
||||
if ((to = goTarget(PARTNERS, id, what, region))) counted = ["magpie partner go", { id, what, region: region || "" }];
|
||||
else if (region === undefined && (to = sponsorTarget(SPONSORS, id, what))) counted = ["magpie sponsor go", { id, where: what }];
|
||||
} catch {}
|
||||
if (!to) return new Response("not found\n", { status: 404 });
|
||||
if (req.method === "GET") ctx.waitUntil(capture("magpie partner go", { id, what, region: region || "", country: country(req) }));
|
||||
if (req.method === "GET") ctx.waitUntil(capture(counted[0], { ...counted[1], country: country(req) }));
|
||||
return new Response(null, { status: 302, headers: { Location: to, "Cache-Control": "no-store" } });
|
||||
}
|
||||
if (url.pathname === "/api/notes") {
|
||||
@@ -183,6 +188,15 @@ export function goTarget(list, id, what, region) {
|
||||
return r ? fromMagpie(r[field] || p[field] || "") : "";
|
||||
}
|
||||
|
||||
// sponsorTarget is where a sponsor's /go link sends the browser: the link
|
||||
// sponsors.js has for that place, as it is (its own ref is how the sponsor
|
||||
// counts us).
|
||||
export function sponsorTarget(list, id, where) {
|
||||
const s = list.find((x) => x.id === id);
|
||||
if (!s || (where !== "home" && where !== "readme")) return "";
|
||||
return s[where] || "";
|
||||
}
|
||||
|
||||
function fromMagpie(to) {
|
||||
if (!to) return "";
|
||||
try {
|
||||
|
||||
+49
-1
@@ -2,7 +2,7 @@
|
||||
// (freecss on Discord). GitHub and the edge cache are stood in for.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import worker, { served, goTarget } from "./worker.js";
|
||||
import worker, { served, goTarget, sponsorTarget } from "./worker.js";
|
||||
|
||||
const EN = "### Features\n\n- One thing (#1)\n\n### Install\n\nDownload it.";
|
||||
const ZH = "### 新功能\n\n- 一件事 (#1)";
|
||||
@@ -442,3 +442,51 @@ test("icon proxy passes on a picture and nothing of the asker", async () => {
|
||||
globalThis.fetch = was;
|
||||
}
|
||||
});
|
||||
|
||||
test("sponsors: /go/<id>/home and /readme send the browser to the sponsor's own link, counted per place", async () => {
|
||||
const { SPONSORS } = await import("./sponsors.js");
|
||||
const list = [{ id: "acme", home: "https://acme.example/r/HOME", readme: "https://acme.example/r/README?x=1" }, { id: "solo", home: "https://solo.example/" }];
|
||||
assert.equal(sponsorTarget(list, "acme", "home"), "https://acme.example/r/HOME");
|
||||
assert.equal(sponsorTarget(list, "acme", "readme"), "https://acme.example/r/README?x=1");
|
||||
for (const [id, where] of [["solo", "readme"], ["acme", "keys"], ["acme", "id"], ["nobody", "home"]])
|
||||
assert.equal(sponsorTarget(list, id, where), "", id + " " + where);
|
||||
for (const s of SPONSORS) {
|
||||
for (const where of ["home", "readme"]) {
|
||||
if (!s[where]) continue;
|
||||
assert.equal(new URL(s[where]).protocol, "https:");
|
||||
captured.length = 0;
|
||||
const res = await worker.fetch(new Request(`https://usemagpie.ai/go/${s.id}/${where}`), {}, ctx);
|
||||
assert.equal(res.status, 302);
|
||||
assert.equal(res.headers.get("Location"), s[where]);
|
||||
await Promise.resolve();
|
||||
assert.equal(captured.length, 1);
|
||||
assert.equal(captured[0].event, "magpie sponsor go");
|
||||
assert.equal(captured[0].properties.id, s.id);
|
||||
assert.equal(captured[0].properties.where, where);
|
||||
// a HEAD goes on but isn't counted
|
||||
captured.length = 0;
|
||||
assert.equal((await worker.fetch(new Request(`https://usemagpie.ai/go/${s.id}/${where}`, { method: "HEAD" }), {}, ctx)).status, 302);
|
||||
assert.deepEqual(captured, []);
|
||||
}
|
||||
assert.equal((await ask(`/go/${s.id}/home/x`)).status, 404);
|
||||
}
|
||||
assert.equal((await ask("/go/nobody-at-all/home")).status, 404);
|
||||
});
|
||||
|
||||
test("sponsors: the home page and the READMEs link every sponsor through /go", async () => {
|
||||
const { readFileSync } = await import("node:fs");
|
||||
const { SPONSORS } = await import("./sponsors.js");
|
||||
const pages = { home: [new URL("./public/index.html", import.meta.url)], readme: [new URL("../README.md", import.meta.url), new URL("../README.zh-CN.md", import.meta.url)] };
|
||||
for (const [where, files] of Object.entries(pages))
|
||||
for (const f of files) {
|
||||
const text = readFileSync(f, "utf8");
|
||||
for (const s of SPONSORS) {
|
||||
if (s[where]) assert.equal(text.includes(s[where]), false, `${f.pathname} links ${s.id} directly`);
|
||||
// the home page links its own /go; a README, on GitHub, the site's
|
||||
const go = (where === "home" ? "" : "https://usemagpie.ai") + `/go/${s.id}/${where}`;
|
||||
if (text.includes(`/go/${s.id}/`)) assert.ok(s[where] && text.includes(`href="${go}"`), `${f.pathname}: ${go}`);
|
||||
}
|
||||
}
|
||||
const home = readFileSync(pages.home[0], "utf8");
|
||||
for (const s of SPONSORS.filter((s) => s.home)) assert.ok(home.includes(`href="/go/${s.id}/home"`), s.id);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user