fix: improve Tracking useEffect
This commit is contained in:
@@ -64,7 +64,28 @@ function createPageObject(trackingData: TrackingData) {
|
|||||||
export default function Tracking({ pageData }: TrackingProps) {
|
export default function Tracking({ pageData }: TrackingProps) {
|
||||||
const pathName = usePathname()
|
const pathName = usePathname()
|
||||||
const queryString = useSearchParams().toString()
|
const queryString = useSearchParams().toString()
|
||||||
const [consentCookie, setConsentCookie] = useState<null | string>(null)
|
|
||||||
|
function CookiebotCallbackOnAccept() {
|
||||||
|
const cookie = window._satellite.cookie.get("CookieConsent")
|
||||||
|
|
||||||
|
if (window.Cookiebot?.changed && window.adobe) {
|
||||||
|
if (cookie?.includes("statistics:true")) {
|
||||||
|
window.adobe.optIn.approve(window.adobe.OptInCategories.ANALYTICS, true)
|
||||||
|
} else {
|
||||||
|
window.adobe.optIn.deny(window.adobe.OptInCategories.ANALYTICS, true)
|
||||||
|
}
|
||||||
|
window.adobe.optIn.complete()
|
||||||
|
console.warn("window.load event explicitly dispatched.")
|
||||||
|
window.dispatchEvent(new Event("load"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function CookebotCallbackOnDecline() {
|
||||||
|
if (window.Cookiebot?.changed && window.adobe) {
|
||||||
|
window.adobe.optIn.deny(window.adobe.OptInCategories.ANALYTICS, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (window.datalayer) {
|
if (window.datalayer) {
|
||||||
const trackingData = { ...pageData, pathName, queryString }
|
const trackingData = { ...pageData, pathName, queryString }
|
||||||
@@ -76,39 +97,17 @@ export default function Tracking({ pageData }: TrackingProps) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// handle consent
|
// handle consent
|
||||||
window.addEventListener(
|
window.addEventListener("CookiebotOnAccept", CookiebotCallbackOnAccept)
|
||||||
"CookiebotOnAccept",
|
window.addEventListener("CookiebotOnDecline", CookebotCallbackOnDecline)
|
||||||
function CookiebotCallback_OnAccept(e) {
|
|
||||||
const cookie = window._satellite.cookie.get("CookieConsent")
|
|
||||||
setConsentCookie(cookie)
|
|
||||||
|
|
||||||
if (window.Cookiebot?.changed && window.adobe) {
|
return () => {
|
||||||
if (cookie?.includes("statistics:true")) {
|
window.removeEventListener("CookiebotOnAccept", CookiebotCallbackOnAccept)
|
||||||
window.adobe.optIn.approve(
|
window.removeEventListener(
|
||||||
window.adobe.OptInCategories.ANALYTICS,
|
"CookiebotOnDecline",
|
||||||
true
|
CookebotCallbackOnDecline
|
||||||
)
|
)
|
||||||
} else {
|
}
|
||||||
window.adobe.optIn.deny(
|
}, [])
|
||||||
window.adobe.OptInCategories.ANALYTICS,
|
|
||||||
true
|
|
||||||
)
|
|
||||||
}
|
|
||||||
window.adobe.optIn.complete()
|
|
||||||
window.dispatchEvent(new Event("load"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
window.addEventListener(
|
|
||||||
"CookiebotOnDecline",
|
|
||||||
function CookebotCallback_OnDecline() {
|
|
||||||
if (window.Cookiebot?.changed && window.adobe) {
|
|
||||||
window.adobe.optIn.deny(window.adobe.OptInCategories.ANALYTICS, true)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}, [consentCookie])
|
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user