Skip to content

feat: call named search-insights methods and add inferQueryID support for conversions#64

Merged
JasonBerry merged 1 commit intomasterfrom
push-tmlxmkystzzq
Mar 16, 2026
Merged

feat: call named search-insights methods and add inferQueryID support for conversions#64
JasonBerry merged 1 commit intomasterfrom
push-tmlxmkystzzq

Conversation

@JasonBerry
Copy link
Collaborator

@JasonBerry JasonBerry commented Mar 12, 2026

Summary

This PR makes two related changes to the GTM template:

1. Use named methods instead of sendEvents

Switches all aa('sendEvents', ...) calls to their named method equivalents (e.g. clickedObjectIDsAfterSearch, convertedObjectIDs, viewedObjectIDs, etc.). This is a prerequisite for inferQueryID support, since the named method wrappers in search-insights handle the additional params that sendEvents does not.

Conversion events with subtypes now map to the correct underlying methods:

  • addToCartaddedToCartObjectIDs / addedToCartObjectIDsAfterSearch
  • purchasepurchasedObjectIDs / purchasedObjectIDsAfterSearch

2. Add inferQueryID parameter for conversion events

Adds an Infer Query ID checkbox to the template UI, available for:

  • Converted Object IDs
  • Converted Object IDs After Search

When enabled, the search-insights library automatically looks up the queryID from its internal localStorage cache (AlgoliaObjectQueryCache) instead of requiring it to be passed explicitly. This enables click-to-conversion attribution without manually threading the queryID through every tag.

3. Fix nested ternaries for GTM sandbox compatibility

GTM's sandboxed JavaScript does not correctly evaluate nested ternary expressions. Replaced nested ternaries for conversion subtype method resolution with if/else via helper functions (getConvertedObjectIDsAfterSearchMethod, getConvertedObjectIDsMethod).

4. Fix and expand unit tests

Fixed the two existing tests which incorrectly asserted queryID and objectData in clickedObjectIDs payloads (those fields only apply to clickedObjectIDsAfterSearch). Added 13 new tests covering:

  • clickedObjectIDsAfterSearch with queryID and positions
  • viewedObjectIDs, viewedFilters, clickedFilters, convertedFilters
  • convertedObjectIDsAfterSearch with addToCart, purchase, and default subtypes
  • convertedObjectIDs with addToCart and purchase subtypes
  • inferQueryID passthrough
  • setAuthenticatedUserToken
  • Failure when not initialized

Changes

  • src/template.ts — Switched to named method calls; replaced nested ternaries with helper functions; pass { inferQueryID: true } as additional params when enabled
  • src/template-parameters.json — Added inferQueryID checkbox with enabling conditions for conversion methods
  • src/types.d.ts — Added inferQueryID?: boolean to GtmDataConvertedObjectIDs and GtmDataConvertedObjectIDsAfterSearch
  • src/tests.yaml — Fixed 2 broken tests, added 13 new tests
  • template.tpl — Rebuilt output

@JasonBerry JasonBerry requested a review from a team as a code owner March 12, 2026 04:36
@JasonBerry JasonBerry requested a review from jcohonner March 12, 2026 04:38
@JasonBerry JasonBerry force-pushed the push-tmlxmkystzzq branch 2 times, most recently from bd205b1 to 3da98d1 Compare March 12, 2026 06:24
@JasonBerry JasonBerry force-pushed the push-tmlxmkystzzq branch 2 times, most recently from a08f703 to 338d065 Compare March 16, 2026 04:20
@JasonBerry JasonBerry requested review from jkaho and tecu23 March 16, 2026 04:21
… for conversions

fix: replace nested ternaries with if/else for GTM compatibility and add unit tests
@JasonBerry JasonBerry merged commit d894368 into master Mar 16, 2026
3 checks passed
@JasonBerry JasonBerry deleted the push-tmlxmkystzzq branch March 16, 2026 04:36
@JasonBerry
Copy link
Collaborator Author

@shipjs prepare

@algolia-bot
Copy link
Contributor

@JasonBerry shipjs prepare done

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.

4 participants