@uxf/analytics 11.80.4 → 11.82.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/analytics",
3
- "version": "11.80.4",
3
+ "version": "11.82.0",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "build": "tsc -P tsconfig.json",
@@ -1,19 +1,27 @@
1
1
  import type { ExperimentConfig, ExperimentVariant, GetExpVariantString } from "./types";
2
+ type ResponseCookie = {
3
+ name: string;
4
+ value: string;
5
+ domain?: string | undefined;
6
+ httpOnly?: boolean | undefined;
7
+ maxAge?: number | undefined;
8
+ partitioned?: boolean | undefined;
9
+ path?: string | undefined;
10
+ priority?: "low" | "medium" | "high" | undefined;
11
+ sameSite?: true | false | "lax" | "strict" | "none" | undefined;
12
+ secure?: boolean | undefined;
13
+ expires?: number;
14
+ };
2
15
  type NextRequest = {
3
16
  cookies: {
4
- get: (cookieName: string) => {
5
- name: string;
6
- value: string;
7
- } | undefined;
17
+ get: (cookieName: string) => ResponseCookie | undefined;
18
+ getAll: () => ResponseCookie[];
8
19
  };
9
20
  };
10
21
  type NextResponse = {
11
22
  cookies: {
12
- set: (cookie: {
13
- name: string;
14
- value: string;
15
- maxAge: number;
16
- }) => void;
23
+ delete: (name: string) => void;
24
+ set: (cookie: ResponseCookie) => void;
17
25
  };
18
26
  };
19
27
  type GetServerSidePropsContext = {
@@ -27,7 +27,7 @@ function getExperimentVariant(config, randomNumberForTesting = null) {
27
27
  function getCookieName(experimentId) {
28
28
  return `${constants_1.EXPERIMENT_COOKIE_PREFIX}${experimentId}`;
29
29
  }
30
- function handleExperimentCookie(request, response, experimentConfig) {
30
+ function handleExperimentCookie(request, response, experimentConfig, options = {}) {
31
31
  var _a;
32
32
  const cookieValue = request.cookies.get(getCookieName(experimentConfig.id));
33
33
  if (cookieValue) {
@@ -35,13 +35,23 @@ function handleExperimentCookie(request, response, experimentConfig) {
35
35
  }
36
36
  const experimentVariant = getExperimentVariant(experimentConfig);
37
37
  response.cookies.set({
38
+ domain: options.domain,
38
39
  name: getCookieName(experimentConfig.id),
39
40
  value: (_a = experimentVariant === null || experimentVariant === void 0 ? void 0 : experimentVariant.name) !== null && _a !== void 0 ? _a : "not-participate",
40
41
  maxAge: 60 * 60 * 24 * 365, // 365 days
41
42
  });
42
43
  }
44
+ function removeOldExperimentsCookies(request, response, experiments) {
45
+ request.cookies.getAll().forEach((cookie) => {
46
+ if (cookie.name.startsWith(constants_1.EXPERIMENT_COOKIE_PREFIX) &&
47
+ !experiments.some((experiment) => experiment.id === cookie.name.replace(constants_1.EXPERIMENT_COOKIE_PREFIX, ""))) {
48
+ response.cookies.delete(cookie.name);
49
+ }
50
+ });
51
+ }
43
52
  function handleABTesting(request, response, experiments) {
44
53
  experiments.forEach((experimentConfig) => handleExperimentCookie(request, response, experimentConfig));
54
+ removeOldExperimentsCookies(request, response, experiments);
45
55
  }
46
56
  function getExperimentsFromContext(cookies) {
47
57
  return Object.entries(cookies)