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.
[x] Bug fix (non-breaking change which fixes an issue)
Testing
To test this change:
Clone the updated branch
Run the application locally
Open the Chat screen multiple times
Use browser developer tools to verify that GTM is initialized only once
Checklist
[x] My code adheres to this project's style guidelines
[x] I have performed a self-review of my own code
[x] I have commented in any complex areas of my code
[x] My changes do not introduce new warnings
[x] I have written tests demonstrating that my changes are effective or that my feature works
[x] Local unit tests pass with my changes
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 calling TagManager.initialize.
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 https://github.com/danny-avila/LibreChat/issues/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.