@pack/hydrogen 1.0.5 → 1.0.6
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/dist/session/cookies-utils.js +1 -1
- package/package.json +1 -1
- package/dist/tests/hooks.d.ts +0 -8
- package/dist/tests/hooks.d.ts.map +0 -1
- package/dist/tests/hooks.js +0 -33
- package/dist/tests/index.d.ts +0 -6
- package/dist/tests/index.d.ts.map +0 -1
- package/dist/tests/index.js +0 -5
- package/dist/tests/pack-test-context.d.ts +0 -15
- package/dist/tests/pack-test-context.d.ts.map +0 -1
- package/dist/tests/pack-test-context.js +0 -3
- package/dist/tests/pack-test-provider.d.ts +0 -8
- package/dist/tests/pack-test-provider.d.ts.map +0 -1
- package/dist/tests/pack-test-provider.js +0 -9
- package/dist/tests/pack-test-route.d.ts +0 -2
- package/dist/tests/pack-test-route.d.ts.map +0 -1
- package/dist/tests/pack-test-route.js +0 -36
- package/dist/tests/test.d.ts +0 -45
- package/dist/tests/test.d.ts.map +0 -1
- package/dist/tests/test.js +0 -226
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { PACK_USER_CONSENT_COOKIE_ID } from "../constants";
|
|
2
|
-
|
|
2
|
+
import cookie from "cookie";
|
|
3
3
|
const tokenHash = "xxxx-4xxx-xxxx-xxxxxxxxxxxx";
|
|
4
4
|
export function hasUserConsent(request) {
|
|
5
5
|
const cookies = cookie.parse(request.headers.get("Cookie"));
|
package/package.json
CHANGED
package/dist/tests/hooks.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Test } from "./test";
|
|
2
|
-
export declare function useAbTest(): Test | null;
|
|
3
|
-
export declare function useAbTestSessionId(): string;
|
|
4
|
-
export declare function useAbTestId(): string | undefined;
|
|
5
|
-
export declare function useAbTestHandle(): string | undefined;
|
|
6
|
-
export declare function useAbTestVariantId(): string | undefined;
|
|
7
|
-
export declare function useAbTestVariantHandle(): string | undefined;
|
|
8
|
-
//# sourceMappingURL=hooks.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../../src/tests/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAG9B,wBAAgB,SAAS,IAAI,IAAI,GAAG,IAAI,CAWvC;AAED,wBAAgB,kBAAkB,IAAI,MAAM,CAW3C;AAED,wBAAgB,WAAW,IAAI,MAAM,GAAG,SAAS,CAIhD;AAED,wBAAgB,eAAe,IAAI,MAAM,GAAG,SAAS,CAIpD;AAED,wBAAgB,kBAAkB,IAAI,MAAM,GAAG,SAAS,CAIvD;AAED,wBAAgB,sBAAsB,IAAI,MAAM,GAAG,SAAS,CAI3D"}
|
package/dist/tests/hooks.js
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useRouteLoaderData } from "@remix-run/react";
|
|
2
|
-
export function useAbTest() {
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
const { abTest, packAbTest } = useRouteLoaderData("root");
|
|
5
|
-
if (abTest === undefined && packAbTest === undefined) {
|
|
6
|
-
throw new Error("No A/B test found, are you sure you have call ...pack.getPackContextData() on root loader return?");
|
|
7
|
-
}
|
|
8
|
-
return abTest || packAbTest;
|
|
9
|
-
}
|
|
10
|
-
export function useAbTestSessionId() {
|
|
11
|
-
// @ts-ignore
|
|
12
|
-
const { sessionId, packSessionId } = useRouteLoaderData("root");
|
|
13
|
-
if (!sessionId && !packSessionId) {
|
|
14
|
-
throw new Error("No Session ID found, are you sure you have call ...pack.getPackContextData() on root loader return?");
|
|
15
|
-
}
|
|
16
|
-
return sessionId || packSessionId;
|
|
17
|
-
}
|
|
18
|
-
export function useAbTestId() {
|
|
19
|
-
const test = useAbTest();
|
|
20
|
-
return test?.id;
|
|
21
|
-
}
|
|
22
|
-
export function useAbTestHandle() {
|
|
23
|
-
const test = useAbTest();
|
|
24
|
-
return test?.handle;
|
|
25
|
-
}
|
|
26
|
-
export function useAbTestVariantId() {
|
|
27
|
-
const test = useAbTest();
|
|
28
|
-
return test?.testVariant?.id;
|
|
29
|
-
}
|
|
30
|
-
export function useAbTestVariantHandle() {
|
|
31
|
-
const test = useAbTest();
|
|
32
|
-
return test?.testVariant?.handle;
|
|
33
|
-
}
|
package/dist/tests/index.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { PackTestRoute } from "./pack-test-route";
|
|
2
|
-
import { PackTestContext, usePackTestContext } from "./pack-test-context";
|
|
3
|
-
import { PackTestProvider } from "./pack-test-provider";
|
|
4
|
-
import { useAbTest, useAbTestHandle, useAbTestId, useAbTestSessionId, useAbTestVariantHandle, useAbTestVariantId } from "./hooks";
|
|
5
|
-
export { PackTestContext, usePackTestContext, PackTestProvider, PackTestRoute, useAbTest, useAbTestId, useAbTestHandle, useAbTestSessionId, useAbTestVariantId, useAbTestVariantHandle, };
|
|
6
|
-
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tests/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,EACL,SAAS,EACT,eAAe,EACf,WAAW,EACX,kBAAkB,EAClB,sBAAsB,EACtB,kBAAkB,EACnB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,aAAa,EACb,SAAS,EACT,WAAW,EACX,eAAe,EACf,kBAAkB,EAClB,kBAAkB,EAClB,sBAAsB,GACvB,CAAC"}
|
package/dist/tests/index.js
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
import { PackTestRoute } from "./pack-test-route";
|
|
2
|
-
import { PackTestContext, usePackTestContext } from "./pack-test-context";
|
|
3
|
-
import { PackTestProvider } from "./pack-test-provider";
|
|
4
|
-
import { useAbTest, useAbTestHandle, useAbTestId, useAbTestSessionId, useAbTestVariantHandle, useAbTestVariantId, } from "./hooks";
|
|
5
|
-
export { PackTestContext, usePackTestContext, PackTestProvider, PackTestRoute, useAbTest, useAbTestId, useAbTestHandle, useAbTestSessionId, useAbTestVariantId, useAbTestVariantHandle, };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export interface Test {
|
|
2
|
-
id: string;
|
|
3
|
-
handle: string;
|
|
4
|
-
testVariant: {
|
|
5
|
-
id: string;
|
|
6
|
-
handle: string;
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
type PackTestContextValue = {
|
|
10
|
-
testExposureCallback?: (test: Test) => void;
|
|
11
|
-
};
|
|
12
|
-
export declare const PackTestContext: import("react").Context<PackTestContextValue>;
|
|
13
|
-
export declare const usePackTestContext: () => PackTestContextValue;
|
|
14
|
-
export {};
|
|
15
|
-
//# sourceMappingURL=pack-test-context.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pack-test-context.d.ts","sourceRoot":"","sources":["../../src/tests/pack-test-context.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE;QACX,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAED,KAAK,oBAAoB,GAAG;IAC1B,oBAAoB,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;CAC7C,CAAC;AAEF,eAAO,MAAM,eAAe,+CAA0C,CAAC;AAEvE,eAAO,MAAM,kBAAkB,4BAAoC,CAAC"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import React, { PropsWithChildren } from "react";
|
|
2
|
-
import { Test } from "./pack-test-context";
|
|
3
|
-
interface PackContentProps {
|
|
4
|
-
testExposureCallback?: (test: Test) => void;
|
|
5
|
-
}
|
|
6
|
-
export declare function PackTestProvider({ children, testExposureCallback, }: PropsWithChildren<PackContentProps>): React.JSX.Element;
|
|
7
|
-
export {};
|
|
8
|
-
//# sourceMappingURL=pack-test-provider.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pack-test-provider.d.ts","sourceRoot":"","sources":["../../src/tests/pack-test-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAmB,IAAI,EAAE,MAAM,qBAAqB,CAAC;AAE5D,UAAU,gBAAgB;IACxB,oBAAoB,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,CAAC;CAC7C;AAED,wBAAgB,gBAAgB,CAAC,EAC/B,QAAQ,EACR,oBAAoB,GACrB,EAAE,iBAAiB,CAAC,gBAAgB,CAAC,qBAYrC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { PackTestContext } from "./pack-test-context";
|
|
3
|
-
export function PackTestProvider({ children, testExposureCallback, }) {
|
|
4
|
-
return (React.createElement(PackTestContext.Provider, { value: {
|
|
5
|
-
testExposureCallback: (test) => {
|
|
6
|
-
testExposureCallback && testExposureCallback(test);
|
|
7
|
-
},
|
|
8
|
-
} }, children));
|
|
9
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pack-test-route.d.ts","sourceRoot":"","sources":["../../src/tests/pack-test-route.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,aAAa,YAIzB,CAAC"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { useLoaderData, useMatches } from "@remix-run/react";
|
|
2
|
-
import Cookies from "js-cookie";
|
|
3
|
-
import { useEffect, useState } from "react";
|
|
4
|
-
import { useRevalidator } from "react-router-dom";
|
|
5
|
-
import { usePackTestContext } from "./pack-test-context";
|
|
6
|
-
const usePackLoaderData = () => {
|
|
7
|
-
const [exposedTestInfo, setExposedTestInfo] = useState();
|
|
8
|
-
const { packTestInfo } = useLoaderData();
|
|
9
|
-
const [root] = useMatches();
|
|
10
|
-
const { packIsPreviewMode } = root?.data;
|
|
11
|
-
const { testExposureCallback } = usePackTestContext();
|
|
12
|
-
const revalidator = useRevalidator();
|
|
13
|
-
const exposedTestCookieString = Cookies.get("exposedTest");
|
|
14
|
-
useEffect(() => {
|
|
15
|
-
if (packTestInfo &&
|
|
16
|
-
!exposedTestCookieString &&
|
|
17
|
-
!packIsPreviewMode &&
|
|
18
|
-
!exposedTestInfo) {
|
|
19
|
-
// Set exposedTest cookie used by server to determine if session has been exposed to test
|
|
20
|
-
const expires = new Date();
|
|
21
|
-
expires.setHours(expires.getHours() + 24);
|
|
22
|
-
Cookies.set("exposedTest", JSON.stringify(packTestInfo), {
|
|
23
|
-
expires,
|
|
24
|
-
});
|
|
25
|
-
setExposedTestInfo(packTestInfo);
|
|
26
|
-
testExposureCallback?.(packTestInfo);
|
|
27
|
-
// TODO: This results in flashing of UI driven by site settings data.
|
|
28
|
-
// Need non-flashing solution.
|
|
29
|
-
revalidator.revalidate();
|
|
30
|
-
}
|
|
31
|
-
}, [packTestInfo, testExposureCallback, exposedTestInfo]);
|
|
32
|
-
};
|
|
33
|
-
export const PackTestRoute = () => {
|
|
34
|
-
usePackLoaderData();
|
|
35
|
-
return null;
|
|
36
|
-
};
|
package/dist/tests/test.d.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { PackClient } from "@pack/client";
|
|
2
|
-
import { PackSession } from "../session/session";
|
|
3
|
-
export interface TestInput {
|
|
4
|
-
testId?: string;
|
|
5
|
-
testHandle?: string;
|
|
6
|
-
testVariantId?: string;
|
|
7
|
-
testVariantHandle?: string;
|
|
8
|
-
}
|
|
9
|
-
export interface Test {
|
|
10
|
-
id: string;
|
|
11
|
-
handle: string;
|
|
12
|
-
testVariant: {
|
|
13
|
-
id: string;
|
|
14
|
-
handle: string;
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
export interface TestTargetAudienceAttributes {
|
|
18
|
-
url?: string;
|
|
19
|
-
query?: string;
|
|
20
|
-
path?: string;
|
|
21
|
-
utmCampaign?: string;
|
|
22
|
-
utmContent?: string;
|
|
23
|
-
utmMedium?: string;
|
|
24
|
-
utmSource?: string;
|
|
25
|
-
utmTerm?: string;
|
|
26
|
-
}
|
|
27
|
-
export interface GetTestInfoOptions {
|
|
28
|
-
request: Request;
|
|
29
|
-
testTargetAudienceAttributes: TestTargetAudienceAttributes | null;
|
|
30
|
-
packClient: PackClient;
|
|
31
|
-
session: PackSession | undefined;
|
|
32
|
-
}
|
|
33
|
-
export interface TestInfo extends Test {
|
|
34
|
-
isFirstExposure?: boolean;
|
|
35
|
-
}
|
|
36
|
-
export declare function getTestInfo({ request, testTargetAudienceAttributes, packClient, session, }: GetTestInfoOptions): Promise<TestInfo | undefined>;
|
|
37
|
-
export declare function getTestSession(session: PackSession | undefined, testFromQueryParams: TestInput | null, previewEnabled: boolean): Test | null | undefined;
|
|
38
|
-
export declare function getTestTargetingAttributesFromRequest(request: Request): TestTargetAudienceAttributes;
|
|
39
|
-
export declare function getTestFromQueryParams(request: Request): TestInput | null;
|
|
40
|
-
export declare function setTestHeaders(headers: any, options: {
|
|
41
|
-
previewEnabled: boolean;
|
|
42
|
-
testInfoForRequest?: TestInfo;
|
|
43
|
-
testFromQueryParams?: TestInput;
|
|
44
|
-
}): any;
|
|
45
|
-
//# sourceMappingURL=test.d.ts.map
|
package/dist/tests/test.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/tests/test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAGjD,MAAM,WAAW,SAAS;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,WAAW,EAAE;QACX,EAAE,EAAE,MAAM,CAAC;QACX,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAED,MAAM,WAAW,4BAA4B;IAC3C,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAqBD,MAAM,WAAW,kBAAkB;IACjC,OAAO,EAAE,OAAO,CAAC;IACjB,4BAA4B,EAAE,4BAA4B,GAAG,IAAI,CAAC;IAClE,UAAU,EAAE,UAAU,CAAC;IACvB,OAAO,EAAE,WAAW,GAAG,SAAS,CAAC;CAClC;AAED,MAAM,WAAW,QAAS,SAAQ,IAAI;IACpC,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAED,wBAAsB,WAAW,CAAC,EAChC,OAAO,EACP,4BAA4B,EAC5B,UAAU,EACV,OAAO,GACR,EAAE,kBAAkB,GAAG,OAAO,CAAC,QAAQ,GAAG,SAAS,CAAC,CAuHpD;AAED,wBAAgB,cAAc,CAC5B,OAAO,EAAE,WAAW,GAAG,SAAS,EAChC,mBAAmB,EAAE,SAAS,GAAG,IAAI,EACrC,cAAc,EAAE,OAAO,GACtB,IAAI,GAAG,IAAI,GAAG,SAAS,CA8BzB;AAED,wBAAgB,qCAAqC,CACnD,OAAO,EAAE,OAAO,GACf,4BAA4B,CA0B9B;AAED,wBAAgB,sBAAsB,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CA2CzE;AAED,wBAAgB,cAAc,CAC5B,OAAO,EAAE,GAAG,EACZ,OAAO,EAAE;IACP,cAAc,EAAE,OAAO,CAAC;IACxB,kBAAkB,CAAC,EAAE,QAAQ,CAAC;IAC9B,mBAAmB,CAAC,EAAE,SAAS,CAAC;CACjC,OAgCF"}
|
package/dist/tests/test.js
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
1
|
-
import cookie from "cookie";
|
|
2
|
-
const QUERY_TEST_BY_RULES = `#graphql
|
|
3
|
-
query TestByRules($testTargetAudienceAttributes: JSON!) {
|
|
4
|
-
testByRules(attributes: $testTargetAudienceAttributes) {
|
|
5
|
-
id
|
|
6
|
-
handle
|
|
7
|
-
testVariant {
|
|
8
|
-
id
|
|
9
|
-
handle
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
|
-
`;
|
|
14
|
-
const QUERY_TEST_VARIANT_IS_RUNNING = `#graphql
|
|
15
|
-
query TestVariantIsRunning($handle: String!, $testId: ID!) {
|
|
16
|
-
testVariantIsRunning(handle: $handle, testId: $testId)
|
|
17
|
-
}
|
|
18
|
-
`;
|
|
19
|
-
export async function getTestInfo({ request, testTargetAudienceAttributes, packClient, session, }) {
|
|
20
|
-
let testInfo = undefined;
|
|
21
|
-
if (session) {
|
|
22
|
-
// Request directly to API if it is using CDN to reduce latency
|
|
23
|
-
let isUsingCdn = false;
|
|
24
|
-
if (packClient.apiUrl === "https://apicdn.packdigital.com/graphql") {
|
|
25
|
-
packClient.apiUrl = "https://app.packdigital.com/graphql";
|
|
26
|
-
isUsingCdn = true;
|
|
27
|
-
}
|
|
28
|
-
const packClientFetchTestByRules = packClient.fetch(QUERY_TEST_BY_RULES, {
|
|
29
|
-
variables: {
|
|
30
|
-
testTargetAudienceAttributes,
|
|
31
|
-
},
|
|
32
|
-
});
|
|
33
|
-
const fetchTestByRulesSetTestInfoAsFirstExposure = async () => {
|
|
34
|
-
const resp = await packClientFetchTestByRules;
|
|
35
|
-
const responseTest = resp.data?.testByRules;
|
|
36
|
-
if (responseTest) {
|
|
37
|
-
testInfo = {
|
|
38
|
-
...responseTest,
|
|
39
|
-
isFirstExposure: true,
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
session.set("test", undefined);
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const testSession = session.get("test");
|
|
47
|
-
let exposedTest = undefined;
|
|
48
|
-
const exposedTestCookieString = cookie.parse(request.headers.get("cookie") || "")?.exposedTest;
|
|
49
|
-
if (exposedTestCookieString) {
|
|
50
|
-
exposedTest = JSON.parse(exposedTestCookieString);
|
|
51
|
-
}
|
|
52
|
-
// If there is no assigned test on the session and an exposed test in the
|
|
53
|
-
// incoming request cookie it means that the user was exposed to a test
|
|
54
|
-
// for the first time previously.
|
|
55
|
-
if (!testSession && exposedTest) {
|
|
56
|
-
// Verify test is still running
|
|
57
|
-
const resp = await packClient.fetch(QUERY_TEST_VARIANT_IS_RUNNING, {
|
|
58
|
-
variables: {
|
|
59
|
-
handle: exposedTest.testVariant.handle,
|
|
60
|
-
testId: exposedTest.id,
|
|
61
|
-
},
|
|
62
|
-
});
|
|
63
|
-
// If the test is still running, set testInfo to the exposed test
|
|
64
|
-
if (!!resp.data.testVariantIsRunning) {
|
|
65
|
-
const { id, handle, testVariant } = exposedTest;
|
|
66
|
-
testInfo = {
|
|
67
|
-
id,
|
|
68
|
-
handle,
|
|
69
|
-
testVariant,
|
|
70
|
-
};
|
|
71
|
-
session.set("test", {
|
|
72
|
-
data: testInfo,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
else {
|
|
76
|
-
// If the test is not running anymore, fetch a new test for the user
|
|
77
|
-
await fetchTestByRulesSetTestInfoAsFirstExposure();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
if (!testSession ||
|
|
82
|
-
!(testSession.data?.id && testSession.data?.testVariant)) {
|
|
83
|
-
// If there is no assigned test on the session, check if there is a test
|
|
84
|
-
// for attributes, then assign to session and return value.
|
|
85
|
-
await fetchTestByRulesSetTestInfoAsFirstExposure();
|
|
86
|
-
}
|
|
87
|
-
else {
|
|
88
|
-
// If there is test on session, check if the test on session is still running.
|
|
89
|
-
const packClientFetchTestVariantIsRunning = packClient.fetch(QUERY_TEST_VARIANT_IS_RUNNING, {
|
|
90
|
-
variables: {
|
|
91
|
-
handle: testSession.data.testVariant.handle,
|
|
92
|
-
testId: testSession.data.id,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
95
|
-
const resp = await packClientFetchTestVariantIsRunning;
|
|
96
|
-
if (!!resp.data.testVariantIsRunning) {
|
|
97
|
-
// If true, set testInfo to the testSession assigned test.
|
|
98
|
-
testInfo = testSession.data;
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
// If not running, clear test session and fetch a new test for the user
|
|
102
|
-
session.set("test", undefined);
|
|
103
|
-
await fetchTestByRulesSetTestInfoAsFirstExposure();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
if (isUsingCdn) {
|
|
108
|
-
// Return to CDN
|
|
109
|
-
packClient.apiUrl = "https://apicdn.packdigital.com/graphql";
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return testInfo;
|
|
113
|
-
}
|
|
114
|
-
export function getTestSession(session, testFromQueryParams, previewEnabled) {
|
|
115
|
-
if (testFromQueryParams) {
|
|
116
|
-
return {
|
|
117
|
-
id: testFromQueryParams.testId || "",
|
|
118
|
-
handle: testFromQueryParams.testHandle || "",
|
|
119
|
-
testVariant: {
|
|
120
|
-
id: testFromQueryParams.testVariantId || "",
|
|
121
|
-
handle: testFromQueryParams.testVariantHandle || "",
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
if (previewEnabled) {
|
|
126
|
-
return {
|
|
127
|
-
id: "",
|
|
128
|
-
handle: "isPreview",
|
|
129
|
-
testVariant: {
|
|
130
|
-
id: "",
|
|
131
|
-
handle: "isPreview",
|
|
132
|
-
},
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
if (!session) {
|
|
136
|
-
return undefined;
|
|
137
|
-
}
|
|
138
|
-
const testSession = session.get("test");
|
|
139
|
-
return testSession?.data || null;
|
|
140
|
-
}
|
|
141
|
-
export function getTestTargetingAttributesFromRequest(request) {
|
|
142
|
-
let testTargetingAttributes = {};
|
|
143
|
-
const requestUrl = new URL(request.url);
|
|
144
|
-
const searchParams = requestUrl.searchParams;
|
|
145
|
-
testTargetingAttributes.url = requestUrl.origin + requestUrl.pathname;
|
|
146
|
-
testTargetingAttributes.query = requestUrl.search;
|
|
147
|
-
testTargetingAttributes.path = requestUrl.pathname;
|
|
148
|
-
searchParams.has("utm_campaign") &&
|
|
149
|
-
(testTargetingAttributes.utmCampaign =
|
|
150
|
-
searchParams.get("utm_campaign") || undefined);
|
|
151
|
-
searchParams.has("utm_content") &&
|
|
152
|
-
(testTargetingAttributes.utmContent =
|
|
153
|
-
searchParams.get("utm_content") || undefined);
|
|
154
|
-
searchParams.has("utm_medium") &&
|
|
155
|
-
(testTargetingAttributes.utmMedium =
|
|
156
|
-
searchParams.get("utm_medium") || undefined);
|
|
157
|
-
searchParams.has("utm_source") &&
|
|
158
|
-
(testTargetingAttributes.utmSource =
|
|
159
|
-
searchParams.get("utm_source") || undefined);
|
|
160
|
-
searchParams.has("utm_term") &&
|
|
161
|
-
(testTargetingAttributes.utmTerm =
|
|
162
|
-
searchParams.get("utm_term") || undefined);
|
|
163
|
-
return testTargetingAttributes;
|
|
164
|
-
}
|
|
165
|
-
export function getTestFromQueryParams(request) {
|
|
166
|
-
const requestUrl = new URL(request.url);
|
|
167
|
-
const params = requestUrl.searchParams;
|
|
168
|
-
if (params.has("testId") ||
|
|
169
|
-
params.has("test_id") ||
|
|
170
|
-
params.has("test-id") ||
|
|
171
|
-
params.has("testHandle") ||
|
|
172
|
-
params.has("test_handle") ||
|
|
173
|
-
params.has("test-handle") ||
|
|
174
|
-
params.has("testVariantId") ||
|
|
175
|
-
params.has("test_variant_id") ||
|
|
176
|
-
params.has("test-variant-id") ||
|
|
177
|
-
params.has("testVariantHandle") ||
|
|
178
|
-
params.has("test_variant_handle") ||
|
|
179
|
-
params.has("test-variant-handle")) {
|
|
180
|
-
return {
|
|
181
|
-
testId: params.get("testId") ||
|
|
182
|
-
params.get("test_id") ||
|
|
183
|
-
params.get("test-id") ||
|
|
184
|
-
"",
|
|
185
|
-
testHandle: params.get("testHandle") ||
|
|
186
|
-
params.get("test_handle") ||
|
|
187
|
-
params.get("test-handle") ||
|
|
188
|
-
"",
|
|
189
|
-
testVariantId: params.get("testVariantId") ||
|
|
190
|
-
params.get("test_variant_id") ||
|
|
191
|
-
params.get("test-variant-id") ||
|
|
192
|
-
"",
|
|
193
|
-
testVariantHandle: params.get("testVariantHandle") ||
|
|
194
|
-
params.get("test_variant_handle") ||
|
|
195
|
-
params.get("test-variant-handle") ||
|
|
196
|
-
"",
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
export function setTestHeaders(headers, options) {
|
|
202
|
-
const { previewEnabled, testFromQueryParams, testInfoForRequest } = options;
|
|
203
|
-
if (testFromQueryParams) {
|
|
204
|
-
headers["X-Pack-Test-Ignore-Test-Status"] = true;
|
|
205
|
-
if (testFromQueryParams.testId) {
|
|
206
|
-
headers["X-Pack-Test-Id"] = testFromQueryParams.testId;
|
|
207
|
-
}
|
|
208
|
-
if (testFromQueryParams.testHandle) {
|
|
209
|
-
headers["X-Pack-Test-Handle"] = testFromQueryParams.testHandle;
|
|
210
|
-
}
|
|
211
|
-
if (testFromQueryParams.testVariantId) {
|
|
212
|
-
headers["X-Pack-Test-Variant-Id"] = testFromQueryParams.testVariantId;
|
|
213
|
-
}
|
|
214
|
-
if (testFromQueryParams.testVariantHandle) {
|
|
215
|
-
headers["X-Pack-Test-Variant-Handle"] =
|
|
216
|
-
testFromQueryParams.testVariantHandle;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
else if (testInfoForRequest) {
|
|
220
|
-
headers = {
|
|
221
|
-
"X-Pack-Test-Id": testInfoForRequest?.id,
|
|
222
|
-
"X-Pack-Test-Variant-Id": testInfoForRequest?.testVariant?.id,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
return headers;
|
|
226
|
-
}
|