@cocalc/hub
Version:
CoCalc: Backend webserver component
68 lines • 2.33 kB
JavaScript
;
/*
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
* License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details
*/
Object.defineProperty(exports, "__esModule", { value: true });
// write cookie. it would be cool to set this via the http request itself,
// but for reasons I don't know it doesn't work across subdomains.
const maxage = 7 * 24 * 60 * 60; // 7 days
document.cookie = `${NAME}=${ID}; path=/; domain=${DOMAIN}; max-age=${maxage}`;
const { href, protocol, host, pathname } = window.location;
// TODO: use the array defined in packages/util/misc.js
const UTM_KEYS = Object.freeze([
"source",
"medium",
"campaign",
"term",
"content",
]);
const response = {};
const UTM = {};
const params = href.slice(href.indexOf("?") + 1).split("&");
let have_utm = false;
for (let i = 0; i < params.length; i++) {
const part = params[i];
const k_v = part.split("=");
let k = k_v[0];
const v = k_v[1];
if (k == null || v == null)
continue;
if (k.slice(0, 4) !== "utm_")
continue;
k = k.slice(4);
if (!UTM_KEYS.includes(k))
continue;
UTM[k] = window.decodeURIComponent(v.slice(0, 100));
have_utm = true;
}
if (have_utm) {
response["utm"] = UTM;
}
// do we have a referrer? (not just "")
if (document.referrer.length > 0) {
response["referrer"] = document.referrer;
}
// also keep a note about the very first landing page
response["landing"] = `${protocol}//${host}${pathname}`;
// PREFIX could be "/", "//{domain}/", "/some/path", or even "//{domain}/some/path"
// @see backend/base-path.ts + hub/analytics.ts
// Note: don't use double // in the URL, because that will redirect and CORS doesn't work with redirects -- #5506
const delim = PREFIX[PREFIX.length - 1] === "/" ? "" : "/";
const fetch_url = `${PREFIX}${delim}analytics.js`;
// send back a beacon (token is in an http-only cookie)
window
.fetch(fetch_url, {
method: "POST",
mode: "cors",
cache: "no-cache",
credentials: "include",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
body: JSON.stringify(response),
})
//.then(response => console.log("Success:", response))
.catch((error) => console.error("Error:", error));
//# sourceMappingURL=analytics-script.js.map