🐛 fix(analytics): prevent multiple GTM initializations #4174
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.
Summary
This PR addresses the issue of multiple unnecessary initializations of Google Tag Manager (GTM) in the LibreChat application. Currently,
TagManager.initialize
is being called redundantly, resulting in GTM being added every time the Chat screen is opened. This PR implements a check to ensure GTM is initialized only once per session, improving performance and preventing potential analytics data inconsistencies.Closes #4171
Change Type
Testing
To test this change:
Checklist
Changes
This PR modifies two files:
client/src/components/Chat/Footer.tsx
client/src/hooks/Config/useAppStartup.ts
In both files, the GTM initialization logic has been updated to check for the existence of
window.google_tag_manager
before callingTagManager.initialize
.Footer.tsx
useAppStartup.ts
These changes ensure that GTM is initialized only if it hasn't been already, preventing multiple unnecessary initializations.