94 lines
2.3 KiB
TypeScript
94 lines
2.3 KiB
TypeScript
export const getQueryParam = (paramName: string) => {
|
|
const search = window.location.search;
|
|
const params = new URLSearchParams(search);
|
|
return params.get(paramName);
|
|
};
|
|
|
|
export const parseQueryParams = () => {
|
|
const params = new URLSearchParams(window.location.search);
|
|
const result: Record<string, string> = {};
|
|
|
|
for (const [key, value] of params.entries()) {
|
|
result[key] = value;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
// Params that should NOT be included in state (they are passed separately)
|
|
const EXCLUDED_STATE_PARAMS = [
|
|
"paywallId",
|
|
"placementId",
|
|
"productId",
|
|
"jwtToken",
|
|
"price",
|
|
"currency",
|
|
"fb_pixels",
|
|
"sessionId",
|
|
"state",
|
|
// Tracking cookies (passed separately)
|
|
"_fbc",
|
|
"_fbp",
|
|
"_ym_uid",
|
|
"_ym_d",
|
|
"_ym_isad",
|
|
"_ym_visorc",
|
|
"yandexuid",
|
|
"ymex",
|
|
];
|
|
|
|
/**
|
|
* Get current query params that should be passed between screens and to payment
|
|
* Includes ALL params except internal ones (productId, placementId, etc.)
|
|
* Works with utm_*, fbclid, gclid, and any other marketing params
|
|
*/
|
|
export const getCurrentQueryParams = (): Record<string, string> => {
|
|
if (typeof window === "undefined") return {};
|
|
|
|
const params = parseQueryParams();
|
|
const utmParams: Record<string, string> = {};
|
|
|
|
for (const [key, value] of Object.entries(params)) {
|
|
const isExcluded =
|
|
EXCLUDED_STATE_PARAMS.includes(key) ||
|
|
key.startsWith("_ga") ||
|
|
key.startsWith("_gid");
|
|
|
|
if (!isExcluded && value) {
|
|
utmParams[key] = value;
|
|
}
|
|
}
|
|
|
|
return utmParams;
|
|
};
|
|
|
|
/**
|
|
* Encode params as base64 JSON for state parameter
|
|
* Uses URL-safe base64 encoding
|
|
*/
|
|
export const encodeStateParam = (params: Record<string, string>): string => {
|
|
if (Object.keys(params).length === 0) return "";
|
|
|
|
try {
|
|
const json = JSON.stringify(params);
|
|
// Use btoa for base64, replace unsafe chars for URL
|
|
const base64 = btoa(json)
|
|
.replace(/\+/g, "-")
|
|
.replace(/\//g, "_")
|
|
.replace(/=+$/, "");
|
|
return base64;
|
|
} catch {
|
|
return "";
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Get base64-encoded state parameter with current query params
|
|
*/
|
|
export const getStateParamForRedirect = (): string => {
|
|
const params = getCurrentQueryParams();
|
|
return encodeStateParam(params);
|
|
};
|
|
|
|
// Backward compatibility alias
|
|
export const getCurrentUtmParams = getCurrentQueryParams; |