-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable analytics w gdpr consent
- Loading branch information
Showing
6 changed files
with
145 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"use client"; | ||
|
||
import Link from "next/link"; | ||
import { useState, useEffect } from "react"; | ||
import { deniedConsent, grantedConsent } from "@/components/GoogleAnalytics"; | ||
import { Button } from "./ui/button"; | ||
|
||
function gtag(...args: any[]) { | ||
window.dataLayer = window.dataLayer || []; | ||
window.dataLayer.push(...args); | ||
} | ||
|
||
export default function CookieBanner() { | ||
const [showConsentDialog, setShowConsentDialog] = useState(false); | ||
|
||
function denyCookies() { | ||
gtag("consent", "update", deniedConsent); | ||
window.localStorage.setItem("consentMode", JSON.stringify(deniedConsent)); | ||
setShowConsentDialog(false); | ||
} | ||
|
||
function allowCookies() { | ||
gtag("consent", "update", grantedConsent); | ||
window.localStorage.setItem("consentMode", JSON.stringify(grantedConsent)); | ||
setShowConsentDialog(false); | ||
} | ||
|
||
useEffect(() => { | ||
const consent = window.localStorage.getItem("consentMode"); | ||
if (!consent) { | ||
setShowConsentDialog(true); | ||
return; | ||
} | ||
const consentMode = JSON.parse(consent) as | ||
| typeof grantedConsent | ||
| typeof deniedConsent; | ||
if (consentMode.ad_storage === "denied") { | ||
setShowConsentDialog(true); | ||
return; | ||
} | ||
}, []); | ||
|
||
if (!showConsentDialog) return null; | ||
|
||
return ( | ||
<div className="fixed inset-x-0 bottom-0 z-50 mx-auto mb-[72px] flex max-w-max flex-col items-center justify-between gap-4 rounded-lg bg-[#1E1E1E] p-3 shadow sm:flex-row md:mb-4 md:max-w-screen-sm md:px-4"> | ||
<div className="text-center text-white"> | ||
<Link target="_blank" href="https://static.long.so/privacy-policy.pdf"> | ||
<p> | ||
We use <span className="font-bold">cookies</span> on our site. | ||
</p> | ||
</Link> | ||
</div> | ||
|
||
<div className="flex gap-2"> | ||
<button | ||
className="rounded-md border-gray-900 px-5 py-2 text-gray-300" | ||
onClick={denyCookies} | ||
> | ||
Decline | ||
</button> | ||
<button | ||
className="rounded-lg bg-white px-5 py-2 text-black" | ||
onClick={allowCookies} | ||
> | ||
Allow Cookies | ||
</button> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"use client"; | ||
import Script from "next/script"; | ||
import { GoogleTagManager } from "@next/third-parties/google"; | ||
|
||
export const grantedConsent = { | ||
ad_storage: "granted", | ||
analytics_storage: "granted", | ||
functionality_storage: "granted", | ||
personalization_storage: "granted", | ||
security_storage: "granted", | ||
} as const; | ||
|
||
export const deniedConsent = { | ||
ad_storage: "denied", | ||
analytics_storage: "denied", | ||
functionality_storage: "denied", | ||
personalization_storage: "denied", | ||
security_storage: "denied", | ||
} as const; | ||
|
||
export default function GoogleAnalytics() { | ||
const gtmId = "GTM-M22BZ6KC"; | ||
return ( | ||
<> | ||
<Script id="google-consent" strategy="afterInteractive"> | ||
{` | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
if (localStorage.getItem('consentMode') === null) { | ||
gtag('consent', 'default', ${JSON.stringify(deniedConsent)}); | ||
} else { | ||
gtag('consent', 'default', JSON.parse(localStorage.getItem('consentMode'))); | ||
} | ||
if (localStorage.getItem('userId') != null) { | ||
window.dataLayer.push({ | ||
'walletAddress': localStorage.getItem('walletAddress') | ||
}); | ||
} | ||
`} | ||
</Script> | ||
<GoogleTagManager gtmId={gtmId} /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters