-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathsafe-gtm.component.tsx
More file actions
30 lines (25 loc) · 911 Bytes
/
safe-gtm.component.tsx
File metadata and controls
30 lines (25 loc) · 911 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use client'
import { GoogleTagManager } from "@next/third-parties/google"
import { useEffect, useState } from "react";
const MAX_HASH_LENGTH = 4000;
export function SafeGTM({ gtmId}: { gtmId: string}) {
const [canLoad, setCanLoad] = useState(false)
useEffect(() => {
/** GTM can make the app crash when large tokens are sent via hash
* This code deletes temporaly the hash, loads the GTM and then puts it
* back again
*/
const hash = window.location.hash
const hasLargeHash = hash.length > MAX_HASH_LENGTH
let originalHash = ""
if(hasLargeHash) {
originalHash = hash
window.location.hash = ""
setTimeout(() => {
window.location.hash = originalHash
}, 500)
}
setCanLoad(true)
}, [])
return canLoad ? <GoogleTagManager gtmId={gtmId} /> : null
}