Skip to content

Commit

Permalink
fix: don't store query/object associations when userHasOptedOut is tr…
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonBerry authored Sep 11, 2024
1 parent d2c3e98 commit 7c4ab3a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
10 changes: 10 additions & 0 deletions lib/__tests__/click.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ describe("clickedObjectIDsAfterSearch", () => {
expect.any(Number)
]);
});

it("shouldn't store the queryID when user has opted out", () => {
const aa = new AlgoliaAnalytics({
requestFn: jest.fn().mockResolvedValue(true)
});
aa.init({ ...credentials, userHasOptedOut: true });
expect(getQueryForObject("index1", "2")).toBeUndefined();
aa.clickedObjectIDsAfterSearch(clickParams);
expect(getQueryForObject("index1", "2")).toBeUndefined();
});
});

describe("clickedObjectIDs", () => {
Expand Down
26 changes: 26 additions & 0 deletions lib/__tests__/conversion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,32 @@ describe("addedToCartObjectIDsAfterSearch", () => {
expect.any(Number)
]);
});

it("shouldn't store the queryID when user has opted out", () => {
const aa = new AlgoliaAnalytics({
requestFn: jest.fn().mockResolvedValue(true)
});
aa.init({ ...credentials, userHasOptedOut: true });
expect(getQueryForObject("index1", "12345")).toBeUndefined();
aa.addedToCartObjectIDsAfterSearch(convertParams);
expect(getQueryForObject("index1", "12345")).toBeUndefined();
});

it("shouldn't store the objectData.queryID if specified when user has opted out", () => {
const aa = new AlgoliaAnalytics({
requestFn: jest.fn().mockResolvedValue(true)
});
aa.init({ ...credentials, userHasOptedOut: true });
expect(getQueryForObject("index1", "12345")).toBeUndefined();
aa.addedToCartObjectIDsAfterSearch({
...convertParams,
objectData: convertParams.objectData.map((data) => ({
...data,
queryID: "objectData-query"
}))
});
expect(getQueryForObject("index1", "12345")).toBeUndefined();
});
});

describe("purchasedObjectIDsAfterSearch", () => {
Expand Down
5 changes: 3 additions & 2 deletions lib/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ export function clickedObjectIDsAfterSearch(
extractAdditionalParams<InsightsSearchClickEvent>(params);

events.forEach(({ index, queryID, objectIDs }) =>
objectIDs.forEach((objectID) =>
storeQueryForObject(index, objectID, queryID)
objectIDs.forEach(
(objectID) =>
!this._userHasOptedOut && storeQueryForObject(index, objectID, queryID)
)
);

Expand Down
6 changes: 4 additions & 2 deletions lib/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export function addedToCartObjectIDsAfterSearch(
events.forEach(({ index, queryID, objectIDs, objectData }) =>
objectIDs.forEach((objectID, i) => {
const objQueryID = objectData?.[i]?.queryID ?? queryID;
if (objQueryID) storeQueryForObject(index, objectID, objQueryID);
if (!this._userHasOptedOut && objQueryID)
storeQueryForObject(index, objectID, objQueryID);
})
);

Expand Down Expand Up @@ -102,7 +103,8 @@ export function addedToCartObjectIDs(
events.forEach(({ index, objectIDs, objectData }) =>
objectIDs.forEach((objectID, i) => {
const queryID = objectData?.[i]?.queryID;
if (queryID) storeQueryForObject(index, objectID, queryID);
if (!this._userHasOptedOut && queryID)
storeQueryForObject(index, objectID, queryID);
})
);

Expand Down

0 comments on commit 7c4ab3a

Please sign in to comment.