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

Consent & local storage config flag #1064

Merged
merged 10 commits into from
Oct 25, 2023
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
6 changes: 6 additions & 0 deletions sandbox/src/components/InAppMessagesDemo/InAppMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ const {
alloyInstance
} = config[configKey];

const getURLParams = key => {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(key);
};

if (alloyInstance !== window.alloy) {
alloyInstance("configure", {
defaultConsent: getURLParams("defaultConsent") || "in",
datastreamId,
orgId,
edgeDomain,
Expand Down
34 changes: 25 additions & 9 deletions src/components/DecisioningEngine/createEventRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@ export const createEventPruner = (
};

export default ({ storage }) => {
const restore = createRestoreStorage(storage, STORAGE_KEY);
const save = createSaveStorage(
storage,
STORAGE_KEY,
createEventPruner(MAX_EVENT_RECORDS, RETENTION_PERIOD)
);

const events = restore({});
let currentStorage = storage;
let restore;
let save;
let events;
const setStorage = newStorage => {
currentStorage = newStorage;

restore = createRestoreStorage(currentStorage, STORAGE_KEY);
save = createSaveStorage(
currentStorage,
STORAGE_KEY,
createEventPruner(MAX_EVENT_RECORDS, RETENTION_PERIOD)
);
events = restore({});
};

setStorage(storage);

const addEvent = (event, eventType, eventId, action) => {
if (!events[eventType]) {
events[eventType] = {};
Expand Down Expand Up @@ -132,5 +142,11 @@ export default ({ storage }) => {
return events[eventType][eventId];
};

return { addExperienceEdgeEvent, addEvent, getEvent, toJSON: () => events };
return {
addExperienceEdgeEvent,
addEvent,
getEvent,
toJSON: () => events,
setStorage
};
};
31 changes: 26 additions & 5 deletions src/components/DecisioningEngine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,33 @@ import {
MOBILE_EVENT_TYPE
} from "./constants";
import createEvaluateRulesetsCommand from "./createEvaluateRulesetsCommand";
import { clearLocalStorage, createInMemoryStorage } from "./utils";

const createDecisioningEngine = ({ config, createNamespacedStorage }) => {
const { orgId } = config;
const createDecisioningEngine = ({
config,
createNamespacedStorage,
consent
}) => {
const { orgId, personalizationStorageEnabled } = config;
const storage = createNamespacedStorage(
`${sanitizeOrgIdForCookieName(orgId)}.decisioning.`
);
const eventRegistry = createEventRegistry({ storage: storage.persistent });
let applyResponse;
if (!personalizationStorageEnabled) {
clearLocalStorage(storage.persistent);
}

const eventRegistry = createEventRegistry({
storage: createInMemoryStorage()
});
const decisionProvider = createDecisionProvider({ eventRegistry });
const contextProvider = createContextProvider({ eventRegistry, window });

const evaluateRulesetsCommand = createEvaluateRulesetsCommand({
contextProvider,
decisionProvider
});

const subscribeRulesetItems = createSubscribeRulesetItems();
let applyResponse;

return {
lifecycle: {
Expand All @@ -48,6 +57,18 @@ const createDecisioningEngine = ({ config, createNamespacedStorage }) => {
},
onComponentsRegistered(tools) {
applyResponse = createApplyResponse(tools.lifecycle);
if (personalizationStorageEnabled) {
consent
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to have .awaitConsent() here in the index.js file. But I wonder if it makes sense to move the listener within eventRegistry instead? 🤔 There are pros/cons.

.awaitConsent()
.then(() => {
eventRegistry.setStorage(storage.persistent);
})
.catch(() => {
if (storage) {
clearLocalStorage(storage.persistent);
}
});
}
},
onBeforeEvent({
event,
Expand Down
14 changes: 14 additions & 0 deletions src/components/DecisioningEngine/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ export const getActivityId = proposition => {

return id;
};
export const createInMemoryStorage = () => {
const inMemoryStorage = {};
return {
getItem: key => {
return key in inMemoryStorage ? inMemoryStorage[key] : null;
},
setItem: (key, value) => {
inMemoryStorage[key] = value;
}
};
};
export const clearLocalStorage = storage => {
storage.clear();
};
1 change: 1 addition & 0 deletions src/core/config/createCoreConfigs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default () =>
orgId: string()
.unique()
.required(),
personalizationStorageEnabled: boolean().default(true),
onBeforeEventSend: callback().default(noop),
edgeConfigOverrides: validateConfigOverride
}).deprecated("edgeConfigId", string().unique(), "datastreamId");
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export const setupResponseHandler = (applyResponse, window, condition) => {
]);
const eventRegistry = createEventRegistry({ storage });
const decisionProvider = createDecisionProvider({ eventRegistry });

const contextProvider = createContextProvider({ eventRegistry, window });

const onResponseHandler = createOnResponseHandler({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ describe("DecisioningEngine:createDecisionProvider", () => {
beforeEach(() => {
storage = jasmine.createSpyObj("storage", ["getItem", "setItem", "clear"]);
eventRegistry = createEventRegistry({ storage });

decisionProvider = createDecisionProvider({ eventRegistry });
decisionProvider.addPayloads([
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe("DecisioningEngine:createOnResponseHandler", () => {

storage = jasmine.createSpyObj("storage", ["getItem", "setItem", "clear"]);
eventRegistry = createEventRegistry({ storage });

decisionProvider = createDecisionProvider({ eventRegistry });
applyResponse = createApplyResponse(lifecycle);
});
Expand Down
20 changes: 16 additions & 4 deletions test/unit/specs/components/DecisioningEngine/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/
import createDecisioningEngine from "../../../../../src/components/DecisioningEngine/index";
import { injectStorage } from "../../../../../src/utils";
import { defer, injectStorage } from "../../../../../src/utils";
import {
mockRulesetResponseWithCondition,
proposition
Expand All @@ -21,15 +21,26 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
let mockEvent;
let onResponseHandler;
let decisioningEngine;
beforeEach(() => {
let awaitConsentDeferred;
let consent;

beforeEach(async () => {
mergeData = jasmine.createSpy();
const config = { orgId: "exampleOrgId" };
awaitConsentDeferred = defer();
consent = jasmine.createSpyObj("consent", {
awaitConsent: awaitConsentDeferred.promise
});
const config = {
orgId: "exampleOrgId",
personalizationStorageEnabled: true
};
window.referrer =
"https://www.google.com/search?q=adobe+journey+optimizer&oq=adobe+journey+optimizer";
const createNamespacedStorage = injectStorage(window);
decisioningEngine = createDecisioningEngine({
config,
createNamespacedStorage
createNamespacedStorage,
consent
});
mockEvent = {
getContent: () => ({}),
Expand All @@ -38,6 +49,7 @@ describe("createDecisioningEngine:commands:evaluateRulesets", () => {
mergeData
};
decisioningEngine.lifecycle.onComponentsRegistered(() => {});
await awaitConsentDeferred.resolve();
});

it("should run the evaluateRulesets command and satisfy the rule based on global context", () => {
Expand Down
26 changes: 26 additions & 0 deletions test/unit/specs/components/DecisioningEngine/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/
import {
createInMemoryStorage,
createRestoreStorage,
createSaveStorage,
getActivityId,
Expand All @@ -18,9 +19,11 @@ import {

describe("DecisioningEngine:utils", () => {
let storage;
let inMemoryStorage;

beforeEach(() => {
storage = jasmine.createSpyObj("storage", ["getItem", "setItem", "clear"]);
inMemoryStorage = createInMemoryStorage();
});

it("restores from storage", () => {
Expand Down Expand Up @@ -191,4 +194,27 @@ describe("DecisioningEngine:utils", () => {
};
expect(getActivityId(proposition)).toEqual(undefined);
});
it("should set and retrieve an item from in-memory storage", () => {
const key = "testKey";
const value = "testValue";
inMemoryStorage.setItem(key, value);
const retrievedValue = inMemoryStorage.getItem(key);
expect(retrievedValue).toEqual(value);
});

it("should return null for a non-existent item", () => {
const key = "nonExistentKey";
const retrievedValue = inMemoryStorage.getItem(key);
expect(retrievedValue).toBeNull();
});

it("should overwrite the value for an existing key", () => {
const key = "existingKey";
const originalValue = "originalValue";
const updatedValue = "updatedValue";
inMemoryStorage.setItem(key, originalValue);
inMemoryStorage.setItem(key, updatedValue);
const retrievedValue = inMemoryStorage.getItem(key);
expect(retrievedValue).toEqual(updatedValue);
});
});