Last active
October 13, 2020 17:12
-
-
Save macintoshhelper/37083ccba8c3cc2e296fe2e32926b788 to your computer and use it in GitHub Desktop.
React Google Analytics with GDPR consent
This file contains hidden or 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
import ua from 'universal-analytics'; | |
const TRACKING_ID = 'UA-XXXXXXXXX-X'; | |
let visitor; | |
export function init() { | |
const isDev = !process.env.NODE_ENV || process.env.NODE_ENV === 'development'; | |
if (!isDev) { | |
visitor = ua(TRACKING_ID); | |
} | |
} | |
export function sendEvent(...args) { | |
if (!visitor) { // noop | |
return; | |
} | |
visitor.event(...args) | |
} | |
export function sendPageView(...args) { | |
if (!visitor) { // noop | |
return; | |
} | |
visitor.pageview(...args); | |
} |
This file contains hidden or 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
import React from 'react'; | |
import CookieConsent, { Cookies } from 'react-cookie-consent'; | |
import useAnalytics from './use-analytics'; | |
const App = () => { | |
const { sendPageView } = useAnalytics(); | |
useEffect(() => { | |
sendPageView('/'); // should noop if consent not given (as visitor object is not initialised) | |
}, [sendPageView]) | |
return ( | |
<CookieConsent | |
location="bottom" | |
buttonText="Agree" | |
onAccept={() => { | |
Cookies.set('cookie_consent', JSON.stringify({ essential: true, analytics: true })); | |
}} | |
onDecline={() => { | |
Cookies.set('cookie_consent', JSON.stringify({ essential: true, analytics: false })); | |
}} | |
style={{ background: "#2B373B" }} | |
buttonStyle={{ color: "#4e503b", fontSize: "13px" }} | |
expires={365} | |
> | |
This website uses cookies to enhance the user experience.{" "} | |
<span style={{ fontSize: "10px" }}>This bit of text is smaller :O</span> | |
</CookieConsent> | |
... | |
); | |
}; | |
export default App; |
This file contains hidden or 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
import { useEffect } from 'react'; | |
import CookieConsent, { Cookies } from "react-cookie-consent"; | |
import { init, sendEvent, sendPageView } from './analytics'; | |
const useAnalytics = () => { | |
useEffect(() => { | |
const consentCookieString = Cookies.get('cookie_consent'); | |
let consentCookie = {}; | |
try { | |
consentCookie = JSON.parse(consentCookieString); | |
} catch(err) {} | |
if (consentCookie.analytics) { | |
init(); | |
} | |
}, []); | |
return { sendEvent, sendPageView }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment