smc-hub
Version:
CoCalc: Backend webserver component
63 lines • 2.04 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.
var maxage = 7 * 24 * 60 * 60; // 7 days
document.cookie = NAME + "=" + ID + "; path=/; domain=" + DOMAIN + "; max-age=" + maxage;
var _a = window.location, href = _a.href, protocol = _a.protocol, host = _a.host, pathname = _a.pathname;
// TODO: use the array defined in smc-util/misc.js
var UTM_KEYS = Object.freeze([
"source",
"medium",
"campaign",
"term",
"content",
]);
var response = {};
var UTM = {};
var params = href.slice(href.indexOf("?") + 1).split("&");
var have_utm = false;
for (var i = 0; i < params.length; i++) {
var part = params[i];
var k_v = part.split("=");
var k = k_v[0];
var 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;
// send back a beacon (token is in an http-only cookie)
window
.fetch(PREFIX + "/analytics.js", {
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(function (error) { return console.error("Error:", error); });
//# sourceMappingURL=analytics-script.js.map