Skip to content

Commit b61d7f7

Browse files
committed
add safe gtm component
1 parent 32730d0 commit b61d7f7

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use client'
2+
3+
import { GoogleTagManager } from "@next/third-parties/google"
4+
import { useEffect, useState } from "react";
5+
6+
const MAX_URL_LENGTH = 8000;
7+
8+
export function SafeGTM({ gtmId}: { gtmId: string}) {
9+
const [canLoad, setCanLoad] = useState(false)
10+
11+
useEffect(() => {
12+
const urlLength = window.location.href.length
13+
const hasLargeToken = urlLength > MAX_URL_LENGTH
14+
if(hasLargeToken) {
15+
console.warn("GTM disabled: URL too long", urlLength)
16+
}
17+
setCanLoad(!hasLargeToken)
18+
}, [])
19+
20+
return canLoad ? <GoogleTagManager gtmId={gtmId} /> : null
21+
}

src/features/common/components/shell/shell.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { COOKIE_LEVELS } from "@/features/analytics/models/cookie-levels.constan
1212
import { OnetrustScriptComponent } from "@/features/analytics/components/onetrust-script.component";
1313
import { CLIENT_CONFIG } from "@/features/analytics/services/config";
1414
import { COOKIE_CONSENT_STATUS } from "@/features/analytics/models/cookie-consent-status.constants";
15-
import { GoogleTagManager } from "@next/third-parties/google";
1615
import styles from "./shell.module.scss";
1716
import { UiLocalizationService } from "@/features/localization/services/ui-localization.service";
1817
import { clsx } from "clsx";
@@ -27,6 +26,7 @@ import { ThemeDetectorComponent } from "@/features/common/components/theme-detec
2726
import { ThemeCookieValues } from "@/features/common/values/theme.values";
2827
import { AbTestingScriptComponent } from "@/features/analytics/components/ab-testing-script/ab-testing-script.component";
2928
import AdobeAnalyticsScript from "@/features/analytics/components/adobe-analytics-script.component";
29+
import { SafeGTM } from "./gtm/safe-gtm.component";
3030

3131
const GTM_ID = process.env.NEXT_PUBLIC_GTM_ID;
3232

@@ -113,7 +113,7 @@ export const ShellComponent: React.FC<ShellComponentProps> = ({
113113
process.env.NEXT_PUBLIC_IS_PROD &&
114114
GTM_ID && (
115115
<>
116-
<GoogleTagManager gtmId={GTM_ID} />
116+
<SafeGTM gtmId={GTM_ID} />
117117
<AbTestingScriptComponent />
118118
</>
119119
)}

0 commit comments

Comments
 (0)