-
Notifications
You must be signed in to change notification settings - Fork 3
[CI-4786] Add removing search term on track recommendation click #230
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
[CI-4786] Add removing search term on track recommendation click #230
Conversation
mocca102
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking great!
Can we add a test case similar to this in ComponentTests.stories.tsx
expect(storageGetItem(CONSTANTS.SEARCH_TERM_STORAGE_KEY)).toBeNull();
The tests seem to be failing as well (I don't think it's related to your PR) but we need to fix it
…s for search term clearing on recommendation selection
@mocca102 Thank you for the review! It was interesting 🧠
|
Actually, I like this approach better. This LGTM. @Alexey-Pavlov I'll approve it but please make sure to get an approval from a CDX team member as well |
Mudaafi
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, I just had one comment on the new test
| await userEvent.click(firstRecommendation); | ||
| } | ||
|
|
||
| expect(storageGetItem(CONSTANTS.SEARCH_TERM_STORAGE_KEY)).toBeNull(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this particular test is created to ensure that search_term_storage is cleared, but should we also check if the tracking event is fired using that new utility captureTrackingRequest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, we can, without any problems
Added this check ✅
Thank you for the review!
| expect(canvas.getAllByText('in Socks').length).toEqual(1); | ||
| expect(canvas.getAllByText(/in Socks/)).toHaveLength(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for fixing this
| export const trackRecommendationSelect = (cioClient, recommendationData: any = {}) => { | ||
| storageRemoveItem(CONSTANTS.SEARCH_TERM_STORAGE_KEY); | ||
| cioClient?.tracker.trackRecommendationClick(recommendationData); | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good abstraction
… and validate tracking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds functionality to automatically remove search terms from storage when recommendation clicks are tracked, addressing a consistency issue between this library and the autocomplete-ui library. It also introduces improved tracking utilities and test patterns.
- Added
trackRecommendationSelectutility function to handle recommendation clicks with storage cleanup - Updated
useDownShifthook to use the new tracking function instead of direct tracker calls - Added
captureTrackingRequestutility for better test reliability and reusability
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/utils/tracking.ts | Added new tracking utilities including trackRecommendationSelect and captureTrackingRequest |
| src/hooks/useDownShift.ts | Updated to use the new trackRecommendationSelect function for better separation of concerns |
| src/stories/tests/HooksTests.stories.tsx | Updated tests to use the new captureTrackingRequest utility and improved test assertions |
| src/stories/tests/ComponentTests.stories.tsx | Added new test for zero-state recommendation selection and updated existing test patterns |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| } | ||
| }; | ||
|
|
||
| export const trackRecommendationSelect = (cioClient, recommendationData: any = {}) => { |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The recommendationData parameter uses any type which reduces type safety. Consider defining a proper interface for recommendation data to improve type checking and developer experience.
| export const trackRecommendationSelect = (cioClient, recommendationData: any = {}) => { | |
| interface RecommendationData { | |
| podId?: string; | |
| numResultsViewed?: number; | |
| url?: string; | |
| section?: string; | |
| items?: Array<{ | |
| itemId?: string; | |
| itemName?: string; | |
| variationId?: string; | |
| }>; | |
| // Add other properties as needed | |
| } | |
| export const trackRecommendationSelect = ( | |
| cioClient: Nullable<ConstructorIOClient>, | |
| recommendationData: RecommendationData = {} | |
| ) => { |
| if (key === '_constructorio_requests') { | ||
| try { | ||
| const requests = JSON.parse(value); | ||
| if (requests.some((req: any) => req?.url?.includes(urlPattern))) { |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using any type for request objects reduces type safety. Consider defining a proper interface for request objects to improve type checking.
| const originalSetItem = Storage.prototype.setItem; | ||
|
|
||
| Storage.prototype.setItem = function setItemInterceptor(key, value) { |
Copilot
AI
Oct 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modifying the global Storage.prototype could cause issues if multiple components use this function simultaneously or if the restoration fails. Consider using a more isolated approach or adding proper error handling to ensure the prototype is always restored.
This pull request adds a new utility function for tracking recommendation selections and updates the
useDownShifthook to use this new function. The main idea is that we have removingSEARCH_TERM_STORAGE_KEYinsrc/tracker.jsin autocomplete-ui, but in this library, this part is missing. I added it here, so customers who use our autocomplete library don't face issues when we send the old search term by clicking on recommendations and adding to cart.Also, this PR improves how recommendation clicks are tracked, making the tracking logic more modular and reusable.
Tracking improvements:
trackRecommendationSelectfunction tosrc/utils/tracking.tsto encapsulate recommendation click tracking logic and ensure consistent storage cleanup.useDownShifthook insrc/hooks/useDownShift.tsto use the newtrackRecommendationSelectfunction instead of calling the tracker directly, improving maintainability and separation of concerns.storageRemoveItemto remove the oldSEARCH_TERM_STORAGE_KEY