|
| 1 | +import { getCookie } from '../cookies/getCookie'; |
| 2 | +import { |
| 3 | + PROPERTY_ID_MAIN, |
| 4 | + PROPERTY_ID_SUBDOMAIN, |
| 5 | +} from './lib/sourcepointConfig'; |
| 6 | +import { postCustomConsent } from './tcfv2/api'; |
| 7 | +import type { SPUserConsent } from './types/tcfv2'; |
| 8 | + |
| 9 | +const purposeIdToNonAdvertisingPurposesMap = new Map<number, string>([ |
| 10 | + [1, '677e3387b265dc07332909da'], // 1 |
| 11 | + [2, '677e3386b265dc073328f686'], //2 |
| 12 | + [3, '677e3386b265dc073328f96f'], // 3 |
| 13 | + [4, '677e3386b265dc073328fc01'], //4 |
| 14 | + [5, '677e3386b265dc073328fe72'], // 5 |
| 15 | + [6, '677e3386b265dc073328ff70'], //6 |
| 16 | + [7, '677e3386b265dc0733290050'], //7 |
| 17 | + [8, '677e3386b265dc07332903c8'], //8 |
| 18 | + [9, '677e3386b265dc0733290502'], //9 |
| 19 | + [10, '677e3386b265dc073329071a'], //10 |
| 20 | + [11, '677e3386b265dc07332909c2'], //11 |
| 21 | +]); |
| 22 | + |
| 23 | +const spBaseUrl = 'https://cdn.privacy-mgmt.com/consent/tcfv2/consent/v3'; |
| 24 | +interface Vendors { |
| 25 | + _id: string; |
| 26 | + name: string; |
| 27 | + vendorType: string; |
| 28 | + googleId?: string; |
| 29 | +} |
| 30 | + |
| 31 | +interface LegIntCategories { |
| 32 | + _id: string; |
| 33 | + name: string; |
| 34 | + iabPurposeRef: { |
| 35 | + iabId: number; |
| 36 | + name: string; |
| 37 | + }; |
| 38 | +} |
| 39 | +interface UserConsentStatus { |
| 40 | + vendors: Vendors[]; |
| 41 | + legIntCategories: LegIntCategories[]; |
| 42 | + legIntVendors: Vendors[]; |
| 43 | + categories: LegIntCategories[]; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * THis function gets the user's consent for the larger gdpr vendor list |
| 48 | + * then calls postUserConsent |
| 49 | + * https://sourcepoint-public-api.readme.io/reference/get_consent-v3-history-siteid-1 |
| 50 | + */ |
| 51 | +export const mergeVendorList = async (): Promise<void> => { |
| 52 | + const userConsent = await getUserConsentOnAdvertisingList(); |
| 53 | + await postUserConsent(userConsent); |
| 54 | + await mergeUserConsent(); |
| 55 | + window.location.reload(); |
| 56 | +}; |
| 57 | + |
| 58 | +/** |
| 59 | + * |
| 60 | + * |
| 61 | + * @return {*} {Promise<UserConsentStatus[]>} |
| 62 | + */ |
| 63 | +const getUserConsentOnAdvertisingList = async (): Promise< |
| 64 | + UserConsentStatus[] |
| 65 | +> => { |
| 66 | + const consentUUID = getCookie({ name: 'consentUUID' }); |
| 67 | + const url = `${spBaseUrl}/history/${PROPERTY_ID_MAIN}?consentUUID=${consentUUID}`; |
| 68 | + |
| 69 | + const getUserConsentOnAdvertisingListResponse = await fetch(url, { |
| 70 | + method: 'GET', |
| 71 | + headers: { |
| 72 | + Accept: 'application/json', |
| 73 | + 'Content-Type': 'application/json', |
| 74 | + }, |
| 75 | + }); |
| 76 | + |
| 77 | + return (await getUserConsentOnAdvertisingListResponse.json()) as UserConsentStatus[]; |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + * |
| 82 | + * |
| 83 | + * @param {UserConsentStatus[]} data |
| 84 | + */ |
| 85 | +const postUserConsent = async (data: UserConsentStatus[]): Promise<void> => { |
| 86 | + const vendorIds = data[0]?.vendors.map((vendor) => vendor._id); |
| 87 | + let purposeIds = data[0]?.categories.map( |
| 88 | + (category) => |
| 89 | + purposeIdToNonAdvertisingPurposesMap.get(category.iabPurposeRef.iabId) ?? |
| 90 | + '', |
| 91 | + ); |
| 92 | + let legitimateInterestPurposeIds = data[0]?.legIntCategories.map( |
| 93 | + (category) => |
| 94 | + purposeIdToNonAdvertisingPurposesMap.get(category.iabPurposeRef.iabId) ?? |
| 95 | + '', |
| 96 | + ); |
| 97 | + |
| 98 | + purposeIds = purposeIds?.filter((id) => id !== ''); |
| 99 | + legitimateInterestPurposeIds = legitimateInterestPurposeIds?.filter( |
| 100 | + (id) => id !== '', |
| 101 | + ); |
| 102 | + |
| 103 | + if (vendorIds && purposeIds && legitimateInterestPurposeIds) { |
| 104 | + await postCustomConsent( |
| 105 | + vendorIds, |
| 106 | + purposeIds, |
| 107 | + legitimateInterestPurposeIds, |
| 108 | + ); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +/** |
| 113 | + * This function merges the main vendor list with the sub-domain user consent status |
| 114 | + * https://sourcepoint-public-api.readme.io/reference/post_consent-v3-siteid-tcstring |
| 115 | + */ |
| 116 | +const mergeUserConsent = async (): Promise<void> => { |
| 117 | + const consentUUID = getCookie({ name: 'consentUUID' }); |
| 118 | + const url = `${spBaseUrl}/${PROPERTY_ID_SUBDOMAIN}/tcstring?consentUUID=${consentUUID}`; |
| 119 | + const spUserConsentString = localStorage.getItem( |
| 120 | + `_sp_user_consent_${PROPERTY_ID_MAIN}`, |
| 121 | + ); |
| 122 | + const userConsent = JSON.parse(spUserConsentString ?? '{}') as SPUserConsent; |
| 123 | + |
| 124 | + await fetch(url, { |
| 125 | + method: 'POST', |
| 126 | + headers: { |
| 127 | + Accept: 'application/json', |
| 128 | + 'Content-Type': 'application/json', |
| 129 | + }, |
| 130 | + body: JSON.stringify({ |
| 131 | + euconsent: userConsent.gdpr?.euconsent, |
| 132 | + }), |
| 133 | + }); |
| 134 | +}; |
0 commit comments