summaryrefslogtreecommitdiff
path: root/node_modules/@jet-app/app-store/tmp/src/foundation/metrics
diff options
context:
space:
mode:
authorrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
committerrxliuli <rxliuli@gmail.com>2025-11-04 05:03:50 +0800
commitbce557cc2dc767628bed6aac87301a1be7c5431b (patch)
treeb51a051228d01fe3306cd7626d4a96768aadb944 /node_modules/@jet-app/app-store/tmp/src/foundation/metrics
init commit
Diffstat (limited to 'node_modules/@jet-app/app-store/tmp/src/foundation/metrics')
-rw-r--r--node_modules/@jet-app/app-store/tmp/src/foundation/metrics/buy-parameters.js112
-rw-r--r--node_modules/@jet-app/app-store/tmp/src/foundation/metrics/cookies.js27
-rw-r--r--node_modules/@jet-app/app-store/tmp/src/foundation/metrics/metrics-identifiers-cache.js245
3 files changed, 384 insertions, 0 deletions
diff --git a/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/buy-parameters.js b/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/buy-parameters.js
new file mode 100644
index 0000000..a48e886
--- /dev/null
+++ b/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/buy-parameters.js
@@ -0,0 +1,112 @@
+import { isNothing, isSome } from "@jet/environment/types/optional";
+/**
+ * An object which allows buy parameters to be operated on like a hash map.
+ */
+export class BuyParameters {
+ /**
+ * Create a buy parameters object with an optional string representation.
+ *
+ * @param buyParams A string containing buy parameters.
+ */
+ constructor(buyParams) {
+ this._values = {};
+ if (isSome(buyParams) && buyParams.length > 0) {
+ const pairs = buyParams.split("&");
+ for (const pair of pairs) {
+ const [encodedKey, encodedValue] = pair.split("=");
+ const key = decodeURIComponent(encodedKey);
+ const value = isNothing(encodedValue) ? "" : decodeURIComponent(encodedValue);
+ this._values[key] = value;
+ }
+ }
+ }
+ /**
+ * Determine the search key to use for the given parameters.
+ *
+ * @param key The key to determine the search value for.
+ * @param prefix An optional prefix to prepend to `key`.
+ * @returns A key which can be used to store and retrieve
+ * values in this buy parameters object.
+ */
+ _searchKey(key, prefix) {
+ if (key.length === 0) {
+ throw new Error("key may not be zero length");
+ }
+ if (isNothing(prefix) || prefix.length === 0) {
+ return key;
+ }
+ else {
+ return `${prefix}${key.charAt(0).toUpperCase()}${key.slice(1)}`;
+ }
+ }
+ /**
+ * Returns the value associated with a given key.
+ *
+ * @param key The key to retrieve the associated value for.
+ * @param keyPrefix The prefix to add to the key. Defaults to `mt`.
+ * @returns The value associated with `key`, or `undefined` is none is found.
+ */
+ get(key, keyPrefix = "mt") {
+ const searchKey = this._searchKey(key, keyPrefix);
+ return this._values[searchKey];
+ }
+ /**
+ * Add a given key-value pair to this buy parameters.
+ *
+ * If a value was previously associated with `key`,
+ * it will be replaced with `value`.
+ *
+ * @param key The key to associate `value` with in these buy parameters.
+ * @param value The value to add to the buy parameters. If value is `undefined`
+ * or `null`, any values previously associated with `key` will be removed.
+ * @param keyPrefix The prefix to add to the key. Defaults to `mt`.
+ * @returns `this`
+ */
+ set(key, value, keyPrefix = "mt") {
+ const searchKey = this._searchKey(key, keyPrefix);
+ if (isNothing(value)) {
+ delete this._values[searchKey];
+ }
+ else {
+ this._values[searchKey] = value;
+ }
+ return this;
+ }
+ /**
+ * Convert this buy parameters to its string representation.
+ */
+ toString() {
+ let buyParams = "";
+ for (const key of Object.keys(this._values)) {
+ const value = this._values[key];
+ if (buyParams.length > 0) {
+ buyParams += "&";
+ }
+ buyParams += encodeURIComponent(key);
+ buyParams += "=";
+ buyParams += encodeURIComponent(value);
+ }
+ return buyParams;
+ }
+ /**
+ * Convert the buy params into a map representation, in which
+ * the keys and values are encoded.
+ */
+ toEncodedMap() {
+ const map = {};
+ for (const key of Object.keys(this._values)) {
+ const value = this._values[key];
+ const encodedKey = encodeURIComponent(key);
+ const encodedValue = encodeURIComponent(value);
+ map[encodedKey] = encodedValue;
+ }
+ return map;
+ }
+ /**
+ * Buy params in a raw (non url encoded) map representation.
+ */
+ toRawMap() {
+ return { ...this._values };
+ }
+}
+//# sourceMappingURL=buy-parameters.js.map \ No newline at end of file
diff --git a/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/cookies.js b/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/cookies.js
new file mode 100644
index 0000000..5005b72
--- /dev/null
+++ b/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/cookies.js
@@ -0,0 +1,27 @@
+import { isNothing } from "@jet/environment/types/optional";
+/**
+ * Iterate the cookies contained in a string.
+ *
+ * @param cookie A string containing zero or more cookies.
+ */
+export function* cookiesOf(cookie) {
+ if (isNothing(cookie)) {
+ return;
+ }
+ const rawEntries = cookie.split(";");
+ for (const rawEntry of rawEntries) {
+ const keyEndIndex = rawEntry.indexOf("=");
+ if (keyEndIndex === -1) {
+ // If there's no splitter, treat the whole raw
+ // entry as the key and provide an empty value.
+ const key = decodeURIComponent(rawEntry).trim();
+ yield { key, value: "" };
+ }
+ else {
+ const key = decodeURIComponent(rawEntry.substring(0, keyEndIndex)).trim();
+ const value = decodeURIComponent(rawEntry.substring(keyEndIndex + 1)).trim();
+ yield { key, value };
+ }
+ }
+}
+//# sourceMappingURL=cookies.js.map \ No newline at end of file
diff --git a/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/metrics-identifiers-cache.js b/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/metrics-identifiers-cache.js
new file mode 100644
index 0000000..96a487f
--- /dev/null
+++ b/node_modules/@jet-app/app-store/tmp/src/foundation/metrics/metrics-identifiers-cache.js
@@ -0,0 +1,245 @@
+import { isNothing, isSome } from "@jet/environment";
+import * as validation from "@jet/environment/json/validation";
+import { makeMetatype } from "@jet/environment/util/metatype";
+/**
+ * @public
+ * Type to define which type of metrics identifier key to use to fetch identifiers, fields, etc.
+ */
+export var MetricsIdentifierType;
+(function (MetricsIdentifierType) {
+ MetricsIdentifierType["client"] = "clientId";
+ MetricsIdentifierType["user"] = "userId";
+ MetricsIdentifierType["canonical"] = "canonicalAccountIdentifierOverride";
+})(MetricsIdentifierType || (MetricsIdentifierType = {}));
+/**
+ * @public
+ * The default namespaces to provide to the native `MetricsIdStore` for fetching and caching
+ */
+export var MetricsIdentifiersDefaultBagNamespaces;
+(function (MetricsIdentifiersDefaultBagNamespaces) {
+ MetricsIdentifiersDefaultBagNamespaces["client"] = "APPSTORE_ENGAGEMENT_CLIENT";
+ MetricsIdentifiersDefaultBagNamespaces["user"] = "APPSTORE_ENGAGEMENT";
+})(MetricsIdentifiersDefaultBagNamespaces || (MetricsIdentifiersDefaultBagNamespaces = {}));
+export var MetricsIdentifiersPaymentBagNamespaces;
+(function (MetricsIdentifiersPaymentBagNamespaces) {
+ MetricsIdentifiersPaymentBagNamespaces["client"] = "APPSTORE_PAYMENTS_ENGAGEMENT_CLIENT";
+ MetricsIdentifiersPaymentBagNamespaces["user"] = "APPSTORE_PAYMENTS_ENGAGEMENT";
+})(MetricsIdentifiersPaymentBagNamespaces || (MetricsIdentifiersPaymentBagNamespaces = {}));
+export var MetricsIdentifiersPersonalizationBagNamespaces;
+(function (MetricsIdentifiersPersonalizationBagNamespaces) {
+ MetricsIdentifiersPersonalizationBagNamespaces["user"] = "APPSTORE_PERSONALIZATION";
+})(MetricsIdentifiersPersonalizationBagNamespaces || (MetricsIdentifiersPersonalizationBagNamespaces = {}));
+/**
+ * The payment context mappings for a metrics identifier key.
+ */
+export const paymentIdentifierContextMapping = {
+ [MetricsIdentifierType.client]: {
+ keyType: MetricsIdentifierType.client,
+ bagNamespace: MetricsIdentifiersPaymentBagNamespaces.client,
+ crossSyncDevice: false,
+ },
+ [MetricsIdentifierType.user]: {
+ keyType: MetricsIdentifierType.user,
+ bagNamespace: MetricsIdentifiersPaymentBagNamespaces.user,
+ crossSyncDevice: true,
+ },
+};
+/**
+ * The default context mappings for a metrics identifier key.
+ */
+const defaultIdentifierContextMapping = {
+ [MetricsIdentifierType.client]: {
+ keyType: MetricsIdentifierType.client,
+ bagNamespace: MetricsIdentifiersDefaultBagNamespaces.client,
+ crossSyncDevice: false,
+ },
+ [MetricsIdentifierType.user]: {
+ keyType: MetricsIdentifierType.user,
+ bagNamespace: MetricsIdentifiersDefaultBagNamespaces.user,
+ crossSyncDevice: true,
+ },
+};
+/**
+ * The personalization context mappings for a metrics identifier key.
+ */
+export const personalizationIdentifierContextMapping = {
+ [MetricsIdentifierType.user]: {
+ keyType: MetricsIdentifierType.user,
+ bagNamespace: MetricsIdentifiersPersonalizationBagNamespaces.user,
+ crossSyncDevice: true,
+ },
+};
+/**
+ * Represents a cache for metrics identifiers and fields.
+ */
+export class MetricsIdentifiersCache {
+ /**
+ * Constructs a new instance of the MetricsIdentifiersCache class.
+ * @param identifierContextMapping - The mapping of identifier types to key contexts.
+ */
+ constructor(identifierContextMapping = defaultIdentifierContextMapping) {
+ this.cachedMetricsIds = {};
+ this.cachedMetricsFields = {};
+ /**
+ * A flag indicating whether a DSID fallback field should be added to the event fields.
+ */
+ this.shouldAddDsIdFallbackField = false;
+ /**
+ * The DSID of the currently logged in user on this JS request.
+ */
+ this.userDsId = null;
+ this.identifierContextMapping = identifierContextMapping;
+ }
+ /**
+ * Loads the values for the specified identifier types.
+ * @param objectGraph - The object graph.
+ * @param idTypes - The identifier types.
+ * @returns A promise that resolves to an array of loaded values.
+ */
+ async loadValues(objectGraph, idTypes) {
+ if (objectGraph.bag.isMetricsUserIdFallbackEnabled && !objectGraph.user.isUnderThirteen) {
+ this.shouldAddDsIdFallbackField = true;
+ this.userDsId = objectGraph.user.dsid;
+ }
+ else {
+ this.shouldAddDsIdFallbackField = false;
+ this.userDsId = null;
+ }
+ const loadedValues = [];
+ for (const idType of idTypes) {
+ const idTypeLoadedValues = await this.loadValuesForIdType(objectGraph, idType);
+ loadedValues.push(idTypeLoadedValues);
+ }
+ for (const { idType, id, fields } of loadedValues) {
+ if (id) {
+ this.cachedMetricsIds[idType] = id;
+ }
+ if (fields) {
+ this.cachedMetricsFields[idType] = fields;
+ }
+ }
+ }
+ /**
+ * @param objectGraph The app store object graph for all our native dependencies
+ * @param idType The type of id we're loading values for
+ * @returns The MetricsIdStore values for this id type
+ */
+ async loadValuesForIdType(objectGraph, idType) {
+ const returnValues = {
+ idType,
+ };
+ const context = this.identifierContextMapping[idType];
+ if (context) {
+ try {
+ const fields = await validation.context("MetricsIdentifiersCache:loadValues:metricsFields", async () => {
+ return await objectGraph.metricsIdentifiers.getMetricsFieldsForContexts([context]);
+ });
+ if (isSome(fields)) {
+ returnValues["fields"] = fields;
+ const extractedIdValue = fields[idType];
+ if (isSome(extractedIdValue) &&
+ typeof extractedIdValue === "string" &&
+ extractedIdValue.length > 0) {
+ returnValues["id"] = extractedIdValue;
+ }
+ }
+ }
+ catch (error) {
+ objectGraph.console.error(`Unable to fetch metrics fields for idType ${idType}`, error);
+ }
+ if (isNothing(returnValues["id"])) {
+ try {
+ const id = await validation.context("MetricsIdentifiersCache:loadValues:metricsIdentifier", async () => {
+ return await objectGraph.metricsIdentifiers.getIdentifierForContext(context);
+ });
+ if (isSome(id)) {
+ returnValues["id"] = id;
+ }
+ }
+ catch (error) {
+ objectGraph.console.error(`Unable to fetch metrics identifier for idType ${idType}`, error);
+ }
+ }
+ }
+ return returnValues;
+ }
+ /**
+ * Gets the metrics identifier for the specified identifier type.
+ * @param idType - The identifier type.
+ * @returns The metrics identifier, or undefined if not found.
+ */
+ getMetricsIdForType(idType) {
+ return this.cachedMetricsIds[idType];
+ }
+ /**
+ * Gets the metrics fields for the specified identifier types.
+ * @param idTypes - The identifier types.
+ * @returns The metrics fields, or undefined if not found.
+ */
+ getMetricsFieldsForTypes(idTypes) {
+ const fieldsForTypes = idTypes.map((idType) => { var _a; return (_a = this.cachedMetricsFields[idType]) !== null && _a !== void 0 ? _a : {}; });
+ const eventFields = Object.assign({}, ...fieldsForTypes);
+ if (this.shouldAddDsIdFallbackField && idTypes.indexOf(MetricsIdentifierType.user) !== -1) {
+ this.addDsIdFallbackFieldIfNecessary(eventFields);
+ }
+ return eventFields;
+ }
+ /**
+ * Adds a DSID fallback field to the event fields if necessary.
+ * @param eventFields - The event fields.
+ */
+ addDsIdFallbackFieldIfNecessary(eventFields) {
+ const existingUserId = eventFields[MetricsIdentifierType.user];
+ const isExistingUserIdInvalid = isNothing(existingUserId) ||
+ typeof existingUserId !== "string" ||
+ existingUserId.length === 0 ||
+ existingUserId.length === MetricsIdentifiersCache.clientGeneratedUserIdLength;
+ if (isExistingUserIdInvalid && isSome(this.userDsId) && this.userDsId.length > 0) {
+ eventFields["dsId"] = this.userDsId;
+ }
+ }
+ /**
+ * Allows the setting of an app-level canonical account override for downstream metrics computations
+ * @param canonicalAccountIdentifier - The identifier to use when operated with canonical account overides
+ */
+ setCanonicalAccountIdentifierOverride(canonicalAccountIdentifier) {
+ if (canonicalAccountIdentifier.length >= 0) {
+ this.cachedMetricsFields[MetricsIdentifierType.canonical] = {
+ [MetricsIdentifierType.canonical]: canonicalAccountIdentifier,
+ };
+ }
+ }
+}
+/**
+ * The metatype of the MetricsIdentifiersCache class.
+ */
+MetricsIdentifiersCache.defaultMetatype = makeMetatype("app-store:metricsIdentifiersCache");
+MetricsIdentifiersCache.paymentMetatype = makeMetatype("app-store:paymentMetricsIdentifiersCache");
+MetricsIdentifiersCache.personalizationMetatype = makeMetatype("app-store:personalizationMetricsIdentifiersCache");
+/**
+ * The length of a client generated user ID.
+ */
+MetricsIdentifiersCache.clientGeneratedUserIdLength = 24;
+export class MockMetricsIdentifiersCache extends MetricsIdentifiersCache {
+ constructor() {
+ super();
+ this.cachedMetricsIds = {
+ [MetricsIdentifierType.client]: "1a2b3c4d5e",
+ [MetricsIdentifierType.user]: "1a2b3c4d5e",
+ };
+ this.cachedMetricsFields = {
+ [MetricsIdentifierType.client]: {
+ clientId: "1a2b3c4d5e",
+ },
+ [MetricsIdentifierType.user]: {
+ userNs: "mockuserNs",
+ userId: "1a2b3c4d5e",
+ metricsId: 2,
+ },
+ [MetricsIdentifierType.canonical]: {
+ canonicalAccountIdentifierOverride: "mockCanonicalAccountIdentifier",
+ },
+ };
+ }
+}
+//# sourceMappingURL=metrics-identifiers-cache.js.map \ No newline at end of file