diff --git a/src/anonymousUserTracking/complexCriteria.test.ts b/src/anonymousUserTracking/complexCriteria.test.ts index c7284341..df29440c 100644 --- a/src/anonymousUserTracking/complexCriteria.test.ts +++ b/src/anonymousUserTracking/complexCriteria.test.ts @@ -694,7 +694,9 @@ describe('complexCriteria', () => { { eventName: 'button-clicked', dataFields: { - 'button-clicked.lastPageViewed': 'welcome page' + 'button-clicked': { + lastPageViewed: 'welcome page' + } }, eventType: 'customEvent' }, diff --git a/src/anonymousUserTracking/criteriaCompletionChecker.ts b/src/anonymousUserTracking/criteriaCompletionChecker.ts index cbe2bc0a..85c00195 100644 --- a/src/anonymousUserTracking/criteriaCompletionChecker.ts +++ b/src/anonymousUserTracking/criteriaCompletionChecker.ts @@ -328,9 +328,21 @@ class CriteriaCompletionChecker { 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, @@ -343,6 +355,22 @@ class CriteriaCompletionChecker { 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) => { diff --git a/src/anonymousUserTracking/tests/complexCriteria.test.ts b/src/anonymousUserTracking/tests/complexCriteria.test.ts index ed7c6876..c58280bc 100644 --- a/src/anonymousUserTracking/tests/complexCriteria.test.ts +++ b/src/anonymousUserTracking/tests/complexCriteria.test.ts @@ -694,7 +694,7 @@ describe('complexCriteria', () => { { eventName: 'button-clicked', dataFields: { - 'button-clicked.lastPageViewed': 'welcome page' + 'button-clicked': { lastPageViewed: 'welcome page' } }, eventType: 'customEvent' }, @@ -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' }, diff --git a/src/anonymousUserTracking/tests/criteriaCompletionChecker.test.ts b/src/anonymousUserTracking/tests/criteriaCompletionChecker.test.ts index 8b564656..21da9c2e 100644 --- a/src/anonymousUserTracking/tests/criteriaCompletionChecker.test.ts +++ b/src/anonymousUserTracking/tests/criteriaCompletionChecker.test.ts @@ -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' } @@ -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' } @@ -1008,7 +1014,7 @@ describe('CriteriaCompletionChecker', () => { { eventName: 'button-clicked', dataFields: { - 'button-clicked.animal': 'test page', + 'button-clicked': { animal: 'test page' }, total: 3 }, createdAt: 1700071052507,