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

MOB-9055: Resolve nested criteria match issue #423

Merged
merged 2 commits into from
Aug 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
4 changes: 3 additions & 1 deletion src/anonymousUserTracking/complexCriteria.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,9 @@ describe('complexCriteria', () => {
{
eventName: 'button-clicked',
dataFields: {
'button-clicked.lastPageViewed': 'welcome page'
'button-clicked': {
lastPageViewed: 'welcome page'
}
},
eventType: 'customEvent'
},
Expand Down
28 changes: 28 additions & 0 deletions src/anonymousUserTracking/criteriaCompletionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}

class CriteriaCompletionChecker {
private localStoredEventList: any[];

Check warning on line 37 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type

constructor(localStoredEventList: string) {
this.localStoredEventList = JSON.parse(localStoredEventList);
Expand Down Expand Up @@ -70,7 +70,7 @@
return matchingCriteria ? matchingCriteria.criteriaId : null;
}

private prepareEventsToProcess(): any[] {

Check warning on line 73 in src/anonymousUserTracking/criteriaCompletionChecker.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
const eventsToProcess: any[] = this.getEventsWithCartItems();
const nonPurchaseEvents: any[] = this.getNonCartEvents();

Expand Down Expand Up @@ -328,9 +328,21 @@
return true;
}
}

if (field.includes('.') && query.comparatorType !== 'IsSet') {
const valueFromObj = this.getValueFromNestedObject(eventData, field);
if (valueFromObj) {
return this.evaluateComparison(
query.comparatorType,
valueFromObj,
query.value ? query.value : ''
);
}
}
const eventKeyItems = filteredLocalDataKeys.filter(
(keyItem) => keyItem === field
);

if (eventKeyItems.length) {
return this.evaluateComparison(
query.comparatorType,
Expand All @@ -343,6 +355,22 @@
return matchResult;
}

private getValueFromNestedObject(eventData: any, field: string): any {
const valueFromObj = this.getFieldValue(eventData, field);
if (typeof valueFromObj === 'object') {
return Object.keys(valueFromObj).map((key) =>
this.getValueFromNestedObject(valueFromObj, key)
);
} else {
return valueFromObj;
}
}

private getFieldValue(data: any, field: string): any {
const fields = field.split('.');
return fields.reduce((acc, field) => acc?.[field], data);
}

private doesItemMatchQueries(item: any, searchQueries: any[]): boolean {
let shouldReturn = false;
const filteredSearchQueries = searchQueries.filter((searchQuery) => {
Expand Down
8 changes: 5 additions & 3 deletions src/anonymousUserTracking/tests/complexCriteria.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ describe('complexCriteria', () => {
{
eventName: 'button-clicked',
dataFields: {
'button-clicked.lastPageViewed': 'welcome page'
'button-clicked': { lastPageViewed: 'welcome page' }
},
eventType: 'customEvent'
},
Expand Down Expand Up @@ -1309,8 +1309,10 @@ describe('complexCriteria', () => {
return JSON.stringify([
{
dataFields: {
'animal-found.type': 'cat',
'animal-found.count': 4
'animal-found': {
type: 'cat',
count: 4
}
},
eventType: 'customEvent'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ describe('CriteriaCompletionChecker', () => {
{
eventName: 'testEvent',
createdAt: 1708494757530,
dataFields: { 'browserVisit.website.domain': 'google.com' },
dataFields: {
browserVisit: {
website: {
domain: 'google.com'
}
}
},
createNewFields: true,
eventType: 'customEvent'
}
Expand Down Expand Up @@ -109,7 +115,7 @@ describe('CriteriaCompletionChecker', () => {
{
eventName: 'testEvent',
createdAt: 1708494757530,
dataFields: { 'browserVisit.website.domain': 'google.com' },
dataFields: { browserVisit: { website: { domain: 'google.com' } } },
createNewFields: true,
eventType: 'customEvent'
}
Expand Down Expand Up @@ -1008,7 +1014,7 @@ describe('CriteriaCompletionChecker', () => {
{
eventName: 'button-clicked',
dataFields: {
'button-clicked.animal': 'test page',
'button-clicked': { animal: 'test page' },
total: 3
},
createdAt: 1700071052507,
Expand Down
Loading