|
3 | 3 |
|
4 | 4 | window.analytics.ready(() => {
|
5 | 5 | window.ketch('once', 'consent', onKetchConsent)
|
| 6 | + window.ketch('on', 'consent', onKetchConsentGtagTrack) |
6 | 7 | window.ketch('on', 'userConsentUpdated', onKetchConsentUpdated)
|
7 | 8 | })
|
8 | 9 |
|
|
40 | 41 | window.ketchConsent = consent
|
41 | 42 | }
|
42 | 43 |
|
| 44 | + // On - each time the consent is loaded, track it to the gtag event |
| 45 | + const onKetchConsentGtagTrack = (consent) => { |
| 46 | + if (window.gtag && |
| 47 | + consent.purposes && |
| 48 | + 'analytics' in consent.purposes && |
| 49 | + 'targeted_advertising' in consent.purposes |
| 50 | + ) { |
| 51 | + const analyticsString = consent.purposes.analytics === true ? 'granted' : 'denied' |
| 52 | + const targetedAdsString = consent.purposes.targeted_advertising === true ? 'granted' : 'denied' |
| 53 | + |
| 54 | + const gtagObject = { |
| 55 | + analytics_storage: analyticsString, |
| 56 | + ad_personalization: targetedAdsString, |
| 57 | + ad_storage: targetedAdsString, |
| 58 | + ad_user_data: targetedAdsString, |
| 59 | + } |
| 60 | + window.gtag('consent', 'update', gtagObject) |
| 61 | + } |
| 62 | + } |
| 63 | + |
43 | 64 | // Use the analytics.addSourceMiddleware function to include the consent on all the events
|
44 | 65 | const addKetchConsentToContextMiddleware = () => {
|
45 | 66 | window.analytics.addSourceMiddleware(({ payload, next }) => {
|
|
0 commit comments