Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for requesting the CORE id in the getIdentity command #1175

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/components/Identity/createComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import getIdentityOptionsValidator from "./getIdentity/getIdentityOptionsValidator.js";
import appendIdentityToUrlOptionsValidator from "./appendIdentityToUrl/appendIdentityToUrlOptionsValidator.js";
import ecidNamespace from "../../constants/ecidNamespace.js";

export default ({
addEcidQueryToPayload,
addQueryStringIdentityToPayload,
ensureSingleIdentity,
setLegacyEcid,
handleResponseForIdSyncs,
getEcidFromResponse,
getNamespacesFromResponse,
getIdentity,
consent,
appendIdentityToUrl,
logger,
getIdentityOptionsValidator,
}) => {
let ecid;
let namespaces;
let edge = {};
return {
lifecycle: {
Expand All @@ -36,15 +37,17 @@ export default ({
return ensureSingleIdentity({ request, onResponse, onRequestFailure });
},
onResponse({ response }) {
if (!ecid) {
ecid = getEcidFromResponse(response);

const newNamespaces = getNamespacesFromResponse(response);
if (
(!namespaces || !namespaces[ecidNamespace]) &&
newNamespaces &&
newNamespaces[ecidNamespace]
) {
// Only data collection calls will have an ECID in the response.
// https://jira.corp.adobe.com/browse/EXEG-1234
if (ecid) {
setLegacyEcid(ecid);
}
setLegacyEcid(newNamespaces[ecidNamespace]);
}
namespaces = newNamespaces;
// For sendBeacon requests, getEdge() will return {}, so we are using assign here
// so that sendBeacon requests don't override the edge info from before.
edge = { ...edge, ...response.getEdge() };
Expand All @@ -56,16 +59,18 @@ export default ({
getIdentity: {
optionsValidator: getIdentityOptionsValidator,
run: (options) => {
const { namespaces: requestedNamespaces } = options;
return consent
.awaitConsent()
.then(() => {
return ecid ? undefined : getIdentity(options);
return namespaces ? undefined : getIdentity(options);
})
.then(() => {
return {
identity: {
ECID: ecid,
},
identity: requestedNamespaces.reduce((acc, namespace) => {
acc[namespace] = namespaces[namespace] || null;
return acc;
}, {}),
edge,
};
});
Expand All @@ -77,10 +82,15 @@ export default ({
return consent
.withConsent()
.then(() => {
return ecid ? undefined : getIdentity(options);
return namespaces ? undefined : getIdentity(options);
})
.then(() => {
return { url: appendIdentityToUrl(ecid, options.url) };
return {
url: appendIdentityToUrl(
namespaces[ecidNamespace],
options.url,
),
};
})
.catch((error) => {
logger.warn(`Unable to append identity to url. ${error.message}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,39 @@ governing permissions and limitations under the License.
*/

import { validateConfigOverride } from "../../../utils/index.js";
import { objectOf, literal, arrayOf } from "../../../utils/validation/index.js";
import { objectOf, enumOf, arrayOf } from "../../../utils/validation/index.js";
import ecidNamespace from "../../../constants/ecidNamespace.js";
import coreNamespace from "../../../constants/coreNamespace.js";

/**
* Verifies user provided event options.
* @param {*} options The user event options to validate
* @returns {*} Validated options
*/
export default objectOf({
namespaces: arrayOf(literal("ECID"))

const validator = objectOf({
namespaces: arrayOf(enumOf(ecidNamespace, coreNamespace))
.nonEmpty()
.uniqueItems()
.default(["ECID"]),
.default([ecidNamespace]),
edgeConfigOverrides: validateConfigOverride,
})
.noUnknownFields()
.default({
namespaces: ["ECID"],
namespaces: [ecidNamespace],
});

export default ({ thirdPartyCookiesEnabled }) => {
return (options) => {
const validatedOptions = validator(options);
if (
!thirdPartyCookiesEnabled &&
validatedOptions.namespaces.includes(coreNamespace)
) {
throw new Error(
`namespaces: The ${coreNamespace} namespace cannot be requested when third-party cookies are disabled.`,
);
}
return validatedOptions;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import ecidNamespace from "../../constants/ecidNamespace.js";

export default (response) => {
const identityResultPayloads = response.getPayloadsByType("identity:result");
const ecidPayload = identityResultPayloads.find(
(payload) => payload.namespace && payload.namespace.code === ecidNamespace,
);
return ecidPayload ? ecidPayload.id : undefined;
return identityResultPayloads.reduce((acc, payload) => {
if (payload.namespace && payload.namespace.code) {
acc[payload.namespace.code] = payload.id;
}
return acc;
}, {});
};
15 changes: 12 additions & 3 deletions src/components/Identity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ import awaitVisitorOptIn from "./visitorService/awaitVisitorOptIn.js";
import injectGetEcidFromVisitor from "./visitorService/injectGetEcidFromVisitor.js";
import injectHandleResponseForIdSyncs from "./injectHandleResponseForIdSyncs.js";
import injectEnsureSingleIdentity from "./injectEnsureSingleIdentity.js";
import addEcidQueryToPayload from "./addEcidQueryToPayload.js";
import injectAddEcidQueryToPayload from "./injectAddEcidQueryToPayload.js";
import injectSetDomainForInitialIdentityPayload from "./injectSetDomainForInitialIdentityPayload.js";
import injectAddLegacyEcidToPayload from "./injectAddLegacyEcidToPayload.js";
import injectAddQueryStringIdentityToPayload from "./injectAddQueryStringIdentityToPayload.js";
import addEcidToPayload from "./addEcidToPayload.js";
import injectAwaitIdentityCookie from "./injectAwaitIdentityCookie.js";
import getEcidFromResponse from "./getEcidFromResponse.js";
import getNamespacesFromResponse from "./getNamespacesFromResponse.js";
import createGetIdentity from "./getIdentity/createGetIdentity.js";
import createIdentityRequest from "./getIdentity/createIdentityRequest.js";
import createIdentityRequestPayload from "./getIdentity/createIdentityRequestPayload.js";
import injectAppendIdentityToUrl from "./appendIdentityToUrl/injectAppendIdentityToUrl.js";
import createGetIdentityOptionsValidator from "./getIdentity/createGetIdentityOptionsValidator.js";

const createIdentity = ({
config,
Expand Down Expand Up @@ -115,18 +116,26 @@ const createIdentity = ({
orgId,
globalConfigOverrides,
});
const getIdentityOptionsValidator = createGetIdentityOptionsValidator({
thirdPartyCookiesEnabled,
});
const addEcidQueryToPayload = injectAddEcidQueryToPayload({
thirdPartyCookiesEnabled,
areThirdPartyCookiesSupportedByDefault,
});
return createComponent({
addEcidQueryToPayload,
addQueryStringIdentityToPayload,
ensureSingleIdentity,
setLegacyEcid: legacyIdentity.setEcid,
handleResponseForIdSyncs,
getEcidFromResponse,
getNamespacesFromResponse,
getIdentity,
consent,
appendIdentityToUrl,
logger,
config,
getIdentityOptionsValidator,
});
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,22 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import addEcidQueryToPayload from "../../../../../src/components/Identity/addEcidQueryToPayload.js";
import ecidNamespace from "../../constants/ecidNamespace.js";
import coreNamespace from "../../constants/coreNamespace.js";

describe("Identity::addEcidQueryToPayload", () => {
it("adds an ECID query to the event", () => {
const payload = jasmine.createSpyObj("payload", ["mergeQuery"]);
addEcidQueryToPayload(payload);
expect(payload.mergeQuery).toHaveBeenCalledWith({
identity: {
fetch: ["ECID"],
},
});
});
});
export default ({
thirdPartyCookiesEnabled,
areThirdPartyCookiesSupportedByDefault,
}) => {
const query = {
identity: {
fetch: [ecidNamespace],
},
};
if (thirdPartyCookiesEnabled && areThirdPartyCookiesSupportedByDefault()) {
query.identity.fetch.push(coreNamespace);
}
return (payload) => {
payload.mergeQuery(query);
};
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 Adobe. All rights reserved.
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -10,12 +10,4 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

import ecidNamespace from "../../constants/ecidNamespace.js";

export default (payload) => {
payload.mergeQuery({
identity: {
fetch: [ecidNamespace],
},
});
};
export default "CORE";
1 change: 1 addition & 0 deletions src/utils/validation/createUniqueItemsValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ import isUnique from "../isUnique.js";
export default () => {
return (value, path) => {
assertValid(isUnique(value), value, path, "array values to be unique");
return value;
};
};
2 changes: 1 addition & 1 deletion src/utils/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ const boundString = string.bind(base);
const boundEnumOf = function boundEnumOf(...values) {
return boundAnyOf(
values.map(boundLiteral),
`one of these values: [${JSON.stringify(values)}]`,
`one of these values: ${JSON.stringify(values)}`,
);
};

Expand Down
Loading
Loading