mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-21 10:50:14 +01:00
🐛 fix(analytics): prevent multiple GTM initializations (#4174)
* 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
This commit is contained in:
parent
c1c13a69dc
commit
561650d6f9
3 changed files with 24 additions and 14 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import React from 'react';
|
||||
import React, { useEffect } from 'react';
|
||||
import ReactMarkdown from 'react-markdown';
|
||||
import TagManager from 'react-gtm-module';
|
||||
import { Constants } from 'librechat-data-provider';
|
||||
|
|
@ -34,13 +34,6 @@ export default function Footer({ className }: { className?: string }) {
|
|||
</a>
|
||||
);
|
||||
|
||||
if (config?.analyticsGtmId != null) {
|
||||
const tagManagerArgs = {
|
||||
gtmId: config.analyticsGtmId,
|
||||
};
|
||||
TagManager.initialize(tagManagerArgs);
|
||||
}
|
||||
|
||||
const mainContentParts = (
|
||||
typeof config?.customFooter === 'string'
|
||||
? config.customFooter
|
||||
|
|
@ -50,6 +43,15 @@ export default function Footer({ className }: { className?: string }) {
|
|||
localize('com_ui_latest_footer')
|
||||
).split('|');
|
||||
|
||||
useEffect(() => {
|
||||
if (config?.analyticsGtmId != null && typeof window.google_tag_manager === 'undefined') {
|
||||
const tagManagerArgs = {
|
||||
gtmId: config.analyticsGtmId,
|
||||
};
|
||||
TagManager.initialize(tagManagerArgs);
|
||||
}
|
||||
}, [config?.analyticsGtmId]);
|
||||
|
||||
const mainContentRender = mainContentParts.map((text, index) => (
|
||||
<React.Fragment key={`main-content-part-${index}`}>
|
||||
<ReactMarkdown
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue