🐛 fix double-logging data catalog searches #4553
Merged
+11
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4316
Context
The data catalog was logging searches to google analytics twice. You can see this on prod by going to https://ourworldindata.org/data, entering a query, and then opening the dev console and typing
dataLayer.at(-1)
anddataLayer.at(-2)
Problem
The data catalog ran all of its business logic through a single
useEffect
, which meant it called anytime any of its dependencies changed. Analytics was one of the last things I added and I didn't stop to think that this callback would fire anytime any of the dependencies updated, including updates to the cache or even no-op calls to update the state.The fix
Extracts the analytics logic into an independent effect, which only runs when
stateAsUrl
changes (which always corresponds to a search running, even when the results are restored from the cache)How to test
Add a
console.log
tologDataCatalogSearch
and run some searches locally, noting that a log call only happens when a new query is run.