Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(analytics): prevent multiple GTM initializations #4174

Merged
merged 4 commits into from
Sep 21, 2024

Conversation

riya-amemiya
Copy link
Contributor

@riya-amemiya riya-amemiya commented Sep 21, 2024

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

  • Bug fix (non-breaking change which fixes an issue)

Testing

To test this change:

  1. Clone the updated branch
  2. Run the application locally
  3. Open the Chat screen multiple times
  4. Use browser developer tools to verify that GTM is initialized only once

Checklist

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes

Changes

This PR modifies two files:

  1. client/src/components/Chat/Footer.tsx
  2. 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.

Footer.tsx

useEffect(() => {
  if (config?.analyticsGtmId != null && typeof window.google_tag_manager === 'undefined') {
    const tagManagerArgs = {
      gtmId: config.analyticsGtmId,
    };
    TagManager.initialize(tagManagerArgs);
  }
}, [config?.analyticsGtmId]);

useAppStartup.ts

useEffect(() => {
  if (config?.analyticsGtmId != null && typeof window.google_tag_manager === 'undefined') {
    const tagManagerArgs = {
      gtmId: config.analyticsGtmId,
    };
    TagManager.initialize(tagManagerArgs);
  }
}, [config?.analyticsGtmId]);

These changes ensure that GTM is initialized only if it hasn't been already, preventing multiple unnecessary initializations.

@riya-amemiya riya-amemiya changed the title Riya amemiya/fix gtm Fix Multiple GTM Initializations Sep 21, 2024
@danny-avila danny-avila changed the title Fix Multiple GTM Initializations 🐛 fix(analytics): prevent multiple GTM initializations Sep 21, 2024
@danny-avila
Copy link
Owner

lgtm, thanks for the PR!

@danny-avila danny-avila merged commit 561650d into danny-avila:main Sep 21, 2024
2 checks passed
BertKiv pushed a commit to BertKiv/LibreChat that referenced this pull request Dec 10, 2024
)

* feat(types): Add global window interface for Google Tag Manager

* refactor(Chat/Footer): Move GTM initialization to useEffect for better lifecycle management

* fix(hooks): add useEffect to initialize TagManager conditionally
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.

[Bug]: Multiple Initializations of Google Tag Manager
2 participants