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

feat(search): handle display tagging from search response #1388

Merged
merged 6 commits into from
Jan 12, 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export const platformSearchResponse = {
}
],
tagging: {
display:
'https://api.staging.empathy.co/tagging/v1/track/empathy/display?q=jeans&lang=en&scope=desktop&totalHits=686&page=1&origin=url%3Aexternal&filtered=true&spellcheck=false',
query:
'https://api.staging.empathy.co/tagging/v1/track/empathy/query?q=jeans&lang=en&scope=desktop&totalHits=686&page=1&origin=url%3Aexternal&filtered=true&spellcheck=false'
},
Expand Down
12 changes: 8 additions & 4 deletions packages/x-adapter-platform/src/mappers/__tests__/url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { extractUrlParameters, getDisplayClickTagging, getTaggingInfoFromUrl } from '../url.utils';
import {
extractUrlParameters,
getDisplayTaggingInfoFromUrl,
getTaggingInfoFromUrl
} from '../url.utils';

describe('url utils methods tests', () => {
describe('extractUrlParameters', () => {
Expand Down Expand Up @@ -45,14 +49,14 @@ describe('url utils methods tests', () => {

describe('getDisplayClickTagging', () => {
it('should not break when dealing with bad urls', () => {
expect(getDisplayClickTagging('null')).toStrictEqual({
expect(getDisplayTaggingInfoFromUrl('null')).toStrictEqual({
url: 'null',
params: { displayId: 'no_query', follow: false }
});
});

it('should retrieve the tagging info from the url, replacing q with displayId', () => {
const { url, params } = getDisplayClickTagging(
const { url, params } = getDisplayTaggingInfoFromUrl(
'https://api.empathy.co/?q=chips&env=mobile&lang=english&lang=spanish'
);
expect(url).toBe('https://api.empathy.co/');
Expand All @@ -65,7 +69,7 @@ describe('url utils methods tests', () => {
});

it('should set no_query tagging info when no q param exist as in topclicked response', () => {
const { url, params } = getDisplayClickTagging(
const { url, params } = getDisplayTaggingInfoFromUrl(
'https://api.empathy.co/?env=mobile&lang=english&lang=spanish'
);
expect(url).toBe('https://api.empathy.co/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ Object {
"url": "https://assets.empathy.co/",
},
],
"displayTagging": Object {
"params": Object {
"displayId": "jeans",
"filtered": "true",
"follow": false,
"lang": "en",
"origin": "url:external",
"page": "1",
"scope": "desktop",
"spellcheck": "false",
"totalHits": "686",
},
"url": "https://api.staging.empathy.co/tagging/v1/track/empathy/display",
},
"facets": Array [
Object {
"filters": Array [
Expand Down
12 changes: 6 additions & 6 deletions packages/x-adapter-platform/src/mappers/url.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ export function getTaggingInfoFromUrl(taggingUrl: string): TaggingRequest {
*
* @public
*/
export function getDisplayClickTagging(displayTaggingUrl: string): TaggingRequest {
const displayClickTagging = getTaggingInfoFromUrl(displayTaggingUrl);
const displayClickTaggingParams = displayClickTagging.params;
export function getDisplayTaggingInfoFromUrl(displayTaggingUrl: string): TaggingRequest {
const displayTagging = getTaggingInfoFromUrl(displayTaggingUrl);
const displayTaggingParams = displayTagging.params;

displayClickTaggingParams.displayId = displayClickTaggingParams.q ?? 'no_query';
delete displayClickTaggingParams.q;
displayTaggingParams.displayId = displayTaggingParams.q ?? 'no_query';
delete displayTaggingParams.q;

return displayClickTagging;
return displayTagging;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMutableSchema } from '@empathyco/x-adapter';
import { Result } from '@empathyco/x-types';
import { getDisplayClickTagging, getTaggingInfoFromUrl } from '../../mappers/url.utils';
import { getDisplayTaggingInfoFromUrl, getTaggingInfoFromUrl } from '../../mappers/url.utils';
import { PlatformResult } from '../../types/models/result.model';

/**
Expand Down Expand Up @@ -36,7 +36,7 @@ export const resultSchema = createMutableSchema<PlatformResult, Result>({
add2cart: ({ add2cart }) => getTaggingInfoFromUrl(add2cart),
checkout: ({ checkout }) => getTaggingInfoFromUrl(checkout),
click: ({ click }) => getTaggingInfoFromUrl(click),
displayClick: ({ displayClick }) => getDisplayClickTagging(displayClick)
displayClick: ({ displayClick }) => getDisplayTaggingInfoFromUrl(displayClick)
}
}
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createMutableSchema } from '@empathyco/x-adapter';
import { SearchResponse } from '@empathyco/x-types';
import { getTaggingInfoFromUrl } from '../../mappers/url.utils';
import { getDisplayTaggingInfoFromUrl, getTaggingInfoFromUrl } from '../../mappers/url.utils';
import { PlatformSearchResponse } from '../../types/responses/search-response.model';
import { bannerSchema } from '../models/banner.schema';
import { facetSchema } from '../models/facet.schema';
Expand Down Expand Up @@ -41,5 +41,6 @@ export const searchResponseSchema = createMutableSchema<PlatformSearchResponse,
$path: 'catalog.partials',
$subSchema: partialResultsSchema
},
queryTagging: ({ catalog }) => getTaggingInfoFromUrl(catalog?.tagging?.query)
queryTagging: ({ catalog }) => getTaggingInfoFromUrl(catalog?.tagging?.query),
displayTagging: ({ catalog }) => getDisplayTaggingInfoFromUrl(catalog?.tagging?.display)
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface PlatformSearchResponse {
partials: PlatformPartialResult[];
tagging: {
query: string;
display: string;
};
};
direct: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export function getEmptySearchResponseStub(): SearchResponse {
params: {},
url: ''
},
displayTagging: {
params: {},
url: ''
},
redirections: [],
results: [],
spellcheck: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function getSearchResponseStub(): SearchResponse {
partialResults: [],
promoteds: getPromotedsStub(),
queryTagging: getTaggingResponseStub(),
displayTagging: getTaggingResponseStub(),
redirections: getRedirectionsStub(),
results: getResultsStub(),
spellcheck: '',
Expand Down
4 changes: 4 additions & 0 deletions packages/x-components/src/adapter/mocked-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export function createSearchResponse(partial?: Partial<SearchResponse>): SearchR
url: `${trackEndpoint}/query`,
params: { page: 1 }
},
displayTagging: {
url: `${trackEndpoint}/display`,
params: { page: 1 }
},
spellcheck: '',
...partial
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const saveSearchResponse: SearchXStoreModule['actions']['saveSearchRespon
totalResults,
spellcheck,
redirections,
queryTagging
queryTagging,
displayTagging
}
) => {
if (totalResults === 0) {
Expand Down Expand Up @@ -50,6 +51,11 @@ export const saveSearchResponse: SearchXStoreModule['actions']['saveSearchRespon
if (queryTagging) {
commit('setQueryTagging', queryTagging);
}

if (displayTagging) {
commit('setDisplayTagging', displayTagging);
}

commit('setTotalResults', totalResults);
commit('setSpellcheck', spellcheck ?? '');
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const searchEmitters = createStoreEmitters(searchXStoreModule, {
partialResults: state.partialResults,
promoteds: state.promoteds,
queryTagging: state.queryTagging,
displayTagging: state.displayTagging,
redirections: state.redirections,
results: state.results,
spellcheck: state.spellcheckedQuery,
Expand Down
7 changes: 7 additions & 0 deletions packages/x-components/src/x-modules/search/store/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ export const searchXStoreModule: SearchXStoreModule = {
setQueryTagging(state, queryTagging) {
state.queryTagging = queryTagging;
},
setDisplayTagging(state, displayTagging) {
state.displayTagging = displayTagging;
},
updateResult(state, result) {
const stateResult = state.results.find(stateResult => result.id === stateResult.id);
if (stateResult) {
Expand Down Expand Up @@ -153,6 +156,10 @@ export function resettableState() {
queryTagging: {
url: '',
params: {}
},
displayTagging: {
url: '',
params: {}
}
};
}
8 changes: 8 additions & 0 deletions packages/x-components/src/x-modules/search/store/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export interface SearchState extends StatusState, QueryState {
promoteds: Promoted[];
/** The query tagging used to track the search events. */
queryTagging: TaggingRequest;
/** The display tagging used to track the search events. */
displayTagging: TaggingRequest;
/** The redirections associated to the `query`. */
redirections: Redirection[];
/** The list of the related tags, related to the `query` property of the state. */
Expand Down Expand Up @@ -172,6 +174,12 @@ export interface SearchMutations
* @param queryTagging - The new query tagging object to save to the state.
*/
setQueryTagging(queryTagging: TaggingRequest): void;
/**
* Sets the display tagging of the module.
*
* @param displayTagging - The new display tagging object to save to the state.
*/
setDisplayTagging(DisplayTagging: TaggingRequest): void;
/**
* Sets the redirection of the module.
*
Expand Down
1 change: 1 addition & 0 deletions packages/x-types/src/response/search-response.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface SearchResponse {
partialResults?: PartialResult[];
promoteds?: Promoted[];
queryTagging?: TaggingRequest;
displayTagging?: TaggingRequest;
redirections?: Redirection[];
results: Result[];
spellcheck?: string;
Expand Down