Skip to content

fix: added search tracking changes#699

Open
ramuvaram wants to merge 5 commits into
mainfrom
search-tracking-digitaldata
Open

fix: added search tracking changes#699
ramuvaram wants to merge 5 commits into
mainfrom
search-tracking-digitaldata

Conversation

@ramuvaram

@ramuvaram ramuvaram commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

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:

@aem-code-sync

aem-code-sync Bot commented Jul 14, 2026

Copy link
Copy Markdown

Hello, I'm the AEM Code Sync Bot and I will run some actions to deploy your branch and validate page speed.
In case there are problems, just click a checkbox below to rerun the respective action.

  • Re-run all PSI checks
  • Re-run failed PSI checks
  • Re-sync branch
Commits

@aem-code-sync

aem-code-sync Bot commented Jul 14, 2026

Copy link
Copy Markdown
Page Scores Audits Google
📱 /us/en_us PERFORMANCE A11Y SEO BEST PRACTICES SI FCP LCP TBT CLS PSI
🖥️ /us/en_us PERFORMANCE A11Y SEO BEST PRACTICES SI FCP LCP TBT CLS PSI

@aem-code-sync
aem-code-sync Bot temporarily deployed to search-tracking-digitaldata July 15, 2026 09:25 Inactive
@aem-code-sync
aem-code-sync Bot temporarily deployed to search-tracking-digitaldata July 15, 2026 12:57 Inactive
@ramuvaram
ramuvaram requested a review from dylandepass July 16, 2026 13:08
Comment thread scripts/consented/instrumentation.js Outdated
* Search-result page detection via .search-results container.
* @returns {boolean}
*/
export function isSearchPage() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported but no used outside of this file?

@awasthiruchi awasthiruchi Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed export keyword.

* automatically every time the search widget finishes a runSearch cycle.
* @param {Element} resultsCountEl
*/
function attachSearchResultsObserver(resultsCountEl) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated code to fire only on new search term.

@aem-code-sync
aem-code-sync Bot temporarily deployed to search-tracking-digitaldata July 17, 2026 06:04 Inactive
if (resultCount === 0) {
const satellite = getSatellite();
if (satellite?.track) {
satellite.track('nullSearch');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MutationObserver does not replay the count update that happened before attachment, so the initial URL-driven search is missed. I confirmed this by loading:

https://search-tracking-digitaldata--vitamix--aemsites.aem.network/us/en_us/search-result?search=blender

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 || '';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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') || '';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants