|
| 1 | +import { GA4 } from 'react-ga4/types/ga4'; |
| 2 | +import Cookies from "js-cookie"; |
| 3 | +import { GoogleAnalyticsConsentValue } from './useGoogleAnalytics'; |
| 4 | + |
| 5 | +declare global { |
| 6 | + interface Window { |
| 7 | + OnetrustActiveGroups?: string; |
| 8 | + OptanonWrapper?: () => void; |
| 9 | + } |
| 10 | + } |
| 11 | + |
| 12 | +// UofM OneTrust cookie documentation: https://vpcomm.umich.edu/resources/cookie-disclosure/ |
| 13 | +enum OneTrustCookieCategory { |
| 14 | + StrictlyNecessary = "C0001", |
| 15 | + Performance = "C0002", |
| 16 | + Functionality = "C0003", |
| 17 | + Targeting = "C0004", |
| 18 | + SocialMedia = "C0005", |
| 19 | +} |
| 20 | + |
| 21 | +export const useOneTrust = (): [(googleAnalytics:GA4) => void] | [] => |
| 22 | + { |
| 23 | + // Embeds the script for UofM OneTrust consent banner implementation |
| 24 | + // See instructions at https://vpcomm.umich.edu/resources/cookie-disclosure/#3rd-party-google-analytics |
| 25 | + const initializeOneTrust = (googleAnalytics: GA4) => { |
| 26 | + // Callback is used by OneTrust to update Google Analytics consent tags and remove cookies |
| 27 | + const updateGtagCallback = () => { |
| 28 | + if (!window.OnetrustActiveGroups) { |
| 29 | + return; |
| 30 | + } |
| 31 | + // Update Google Analytics consent based on OneTrust active groups |
| 32 | + // "Strictly Necessary Cookies" are always granted. C0001 (StrictlyNecessary), C0003 (Functionality) |
| 33 | + if (window.OnetrustActiveGroups.includes(OneTrustCookieCategory.Performance)) { |
| 34 | + googleAnalytics.gtag("consent", "update", { analytics_storage: GoogleAnalyticsConsentValue.Granted }); |
| 35 | + } |
| 36 | + if (window.OnetrustActiveGroups.includes(OneTrustCookieCategory.Functionality)) { |
| 37 | + googleAnalytics.gtag("consent", "update", { functional_storage: GoogleAnalyticsConsentValue.Granted }); |
| 38 | + } |
| 39 | + |
| 40 | + // "Analytics & Advertising Cookies" are optional for EU users. C0002 (Performance) |
| 41 | + if (window.OnetrustActiveGroups.includes(OneTrustCookieCategory.Targeting)) { |
| 42 | + googleAnalytics.gtag("consent", "update", { |
| 43 | + ad_storage: GoogleAnalyticsConsentValue.Granted, |
| 44 | + ad_user_data: GoogleAnalyticsConsentValue.Granted, |
| 45 | + ad_personalization: GoogleAnalyticsConsentValue.Granted, |
| 46 | + personalization_storage: GoogleAnalyticsConsentValue.Granted |
| 47 | + }); |
| 48 | + } else { |
| 49 | + // Remove Google Analytics cookies if tracking is declined by EU users |
| 50 | + // Uses same library as this GA4 implementation: https://dev.to/ramonak/react-enable-google-analytics-after-a-user-grants-consent-5bg3 |
| 51 | + Cookies.remove("_ga"); |
| 52 | + Cookies.remove("_gat"); |
| 53 | + Cookies.remove("_gid"); |
| 54 | + } |
| 55 | + googleAnalytics.event({ action: 'um_consent_updated', category: 'consent' }); |
| 56 | + } |
| 57 | + window.OptanonWrapper = updateGtagCallback; |
| 58 | + |
| 59 | + const oneTrustScriptDomain = "03e0096b-3569-4b70-8a31-918e55aa20da" |
| 60 | + const src =`https://cdn.cookielaw.org/consent/${oneTrustScriptDomain}/otSDKStub.js` |
| 61 | + |
| 62 | + const script = document.createElement('script'); |
| 63 | + script.src = src; |
| 64 | + script.type = 'text/javascript'; |
| 65 | + script.dataset.domainScript = oneTrustScriptDomain; |
| 66 | + document.head.appendChild(script); |
| 67 | + } |
| 68 | + |
| 69 | + return [ initializeOneTrust ]; |
| 70 | +}; |
0 commit comments