fix: added search tracking changes#699
Conversation
|
Hello, I'm the AEM Code Sync Bot and I will run some actions to deploy your branch and validate page speed.
|
| * Search-result page detection via .search-results container. | ||
| * @returns {boolean} | ||
| */ | ||
| export function isSearchPage() { |
There was a problem hiding this comment.
exported but no used outside of this file?
There was a problem hiding this comment.
@dylandepass Good catch.
@ramuvaram I think we can remove the export keyword since it's only used internally in this file. let us know if you have plan to use it externally.
There was a problem hiding this comment.
removed export keyword.
| * automatically every time the search widget finishes a runSearch cycle. | ||
| * @param {Element} resultsCountEl | ||
| */ | ||
| function attachSearchResultsObserver(resultsCountEl) { |
There was a problem hiding this comment.
This observer fires on every #results-count text change, which includes pagination clicks and type-filter toggles, not just new searches. All three go through runSearch which writes to #results-count.
Is that what you want @awasthiruchi?
There was a problem hiding this comment.
@dylandepass confirmed with the legacy AEM code and live site's Network tab that setDigitalDataForSearch() only fires after genuine search results come back, not on pagination/filter changes.
@ramuvaram Will scope attachSearchResultsObserver to only fire on an actual new search term, so it matches current production behavior. Thanks!
There was a problem hiding this comment.
Updated code to fire only on new search term.
| if (resultCount === 0) { | ||
| const satellite = getSatellite(); | ||
| if (satellite?.track) { | ||
| satellite.track('nullSearch'); |
There was a problem hiding this comment.
Set the search fields on digitalData.page.pageInfo before calling satellite.track('nullSearch') otherwise the rule sees missing or stale term and result values.
| const container = document.querySelector('.search-results'); | ||
| if (!container) return; | ||
|
|
||
| const resultsCountEl = container.querySelector('#results-count'); |
There was a problem hiding this comment.
MutationObserver does not replay the count update that happened before attachment, so the initial URL-driven search is missed. I confirmed this by loading:
The page rendered 163 results, but digitalData.page.pageInfo was missing onsiteSearchTerm, onsiteSearchResults, and onsiteSearchToolType. Please process the current completed result when attaching, or subscribe to an explicit search-complete event from the widget.
| debugLog('Adobe Analytics nullSearch fired', { searchTerm, toolType }); | ||
| } | ||
| } else { | ||
| window.digitalData.page.pageInfo.onsiteSearchTerm = searchTerm || ''; |
There was a problem hiding this comment.
Can you confirm the lauch has a data element changed rule watching these digitalData fields? This branch does not call satellite.track(), so a direct-call configuration would not send a successful-search event.. if change detection is intentional, the JSDoc should say that instead.
| let lastSearchTerm = null; | ||
| const observer = new MutationObserver(() => { | ||
| const params = new URLSearchParams(window.location.search); | ||
| const searchTerm = params.get('search') || ''; |
There was a problem hiding this comment.
This runs for every live-search prefix, so typing one zero-result term can fire nullSearch several times. Debounce the tracking path and emit once for the final settled query.
Please always provide the GitHub issue(s) your PR is for, as well as test URLs where your change can be observed (before and after):
Fix #vita-765
Test URLs: