feat: add uuid package

This commit is contained in:
Britney Wang
2025-07-07 16:24:50 +08:00
parent e096261879
commit aca833aaf9
5 changed files with 7 additions and 76 deletions

View File

@ -12,6 +12,7 @@ let entries = {
"homepage_animations": "./static/js/homepage/animations.js",
"homepage_carousel": "./static/js/homepage/carousel.js",
"scroll-section": "./static/js/scroll-section.js",
"cookie-policy-with-callback": "./static/js/cookie-policy-with-callback.js",
};
const isDev = process && process.env && process.env.NODE_ENV === "development";

View File

@ -39,6 +39,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"sass": "1.89.2",
"uuid": "^11.1.0",
"vanilla-framework": "4.24.1"
},
"devDependencies": {

View File

@ -4,9 +4,6 @@ rm -rf static/js/modules
mkdir -p static/js/modules/cookie-policy
cp node_modules/@canonical/cookie-policy/build/js/cookie-policy.js static/js/modules/cookie-policy
mkdir -p static/js/modules/cookie-policy-with-callback
cp node_modules/@canonical/cookie-policy/build/js/cookie-policy-with-callback.js static/js/modules/cookie-policy-with-callback
mkdir -p static/js/modules/fuse
cp node_modules/fuse.js/dist/fuse.js static/js/modules/fuse

View File

@ -1,73 +0,0 @@
/*
Set a unique user_id if the user has accepted cookies.
If the user has not set their cookie preference, wait for the cookie policy to run first.
*/
import { v4 as uuidv4 } from "https://jspm.dev/uuid";
const getCookie = (targetCookie) =>
document.cookie.match(new RegExp("(^| )" + targetCookie + "=([^;]+)"));
let cookieAcceptanceValue = getCookie("_cookies_accepted");
if (!cookieAcceptanceValue) {
cpNs.cookiePolicy(setUserId);
} else {
setUserId();
cpNs.cookiePolicy();
setUtms();
}
function setUserId() {
cookieAcceptanceValue = getCookie("_cookies_accepted");
if (
cookieAcceptanceValue?.[2] === "all" ||
cookieAcceptanceValue?.[2] === "performance"
) {
// Check if user doesn't already have a user_id
if (!getCookie("user_id")) {
// Generate a universally unique identifier
const user_id = uuidv4();
document.cookie = "user_id=" + user_id + ";max-age=31536000;";
dataLayer.push({
user_id: user_id,
});
}
}
}
function setUtmCookies(urlParams) {
let utmParams = "";
urlParams.forEach((value, key) => {
if (key.startsWith("utm_")) {
utmParams += key + ":" + value + "&";
}
});
if (utmParams.length > 0) {
if (utmParams.endsWith("&")) {
utmParams = utmParams.slice(0, -1);
}
document.cookie =
"utms=" + encodeURIComponent(utmParams) + ";max-age=86400;path=/;";
}
}
function setUtms() {
cookieAcceptanceValue = getCookie("_cookies_accepted");
if (
cookieAcceptanceValue?.[2] === "all" ||
cookieAcceptanceValue?.[2] === "performance"
) {
let utmCookies = getCookie("utms");
const urlParams = new URLSearchParams(window.location.search);
if (urlParams.size > 0) {
setUtmCookies(urlParams);
} else if (utmCookies) {
const referrer = document.referrer;
const currentHost = window.location.host;
if (!referrer.includes(currentHost)) {
document.cookie = "utms=;max-age=0;path=/;";
}
}
}
}

View File

@ -4941,6 +4941,11 @@ util-deprecate@^1.0.2:
resolved "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==
uuid@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-11.1.0.tgz#9549028be1753bb934fc96e2bca09bb4105ae912"
integrity sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==
v8-compile-cache@^2.3.0:
version "2.3.0"
resolved "https://registry.npmjs.org/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz"