11import {
22 Dictionary ,
33 isEmpty ,
4- createCookieSyncUrl ,
4+ createInitialCookieSyncUrl ,
55 isFunction ,
66} from './utils' ;
77import Constants from './constants' ;
@@ -17,12 +17,6 @@ export const DAYS_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
1717
1818export type CookieSyncDates = Dictionary < number > ;
1919
20- // this is just a partial definition of TCData for the purposes of our implementation. The full schema can be found here:
21- // https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md#tcdata
22- type TCData = {
23- gdprApplies ?: boolean ;
24- tcString ?: string ;
25- } ;
2620declare global {
2721 interface Window {
2822 __tcfapi : any ;
@@ -52,17 +46,11 @@ export interface ICookieSyncManager {
5246 mpid : MPID ,
5347 cookieSyncDates : CookieSyncDates ,
5448 ) => void ;
55- performCookieSyncWithGDPR : (
56- url : string ,
57- moduleId : string ,
58- mpid : MPID ,
59- cookieSyncDates : CookieSyncDates ,
60- ) => void ;
6149 processPixelConfig : (
62- mpidIsNotInCookies : boolean ,
50+ pixelSettings : IPixelConfiguration ,
6351 persistence : IPersistenceMinified ,
6452 mpid : string ,
65- pixelSettings : IPixelConfiguration
53+ mpidIsNotInCookies : boolean ,
6654 ) => void
6755} ;
6856
@@ -91,26 +79,29 @@ export default function CookieSyncManager(
9179
9280 pixelConfigurations . forEach ( ( pixelSetting : IPixelConfiguration ) =>
9381 self . processPixelConfig (
94- mpidIsNotInCookies ,
82+ pixelSetting ,
9583 persistence ,
9684 mpid ,
97- pixelSetting
85+ mpidIsNotInCookies ,
9886 )
9987 ) ;
10088 } ;
10189
10290 this . processPixelConfig = async (
103- mpidIsNotInCookies : boolean ,
91+ pixelSettings : IPixelConfiguration ,
10492 persistence : IPersistenceMinified ,
10593 mpid : string ,
106- pixelSettings : IPixelConfiguration ,
94+ mpidIsNotInCookies : boolean ,
10795 ) : Promise < void > => {
10896 // set requiresConsent to false to start each additional pixel configuration
10997 // set to true only if filteringConsenRuleValues.values.length exists
11098 let requiresConsent = false ;
11199 // Filtering rules as defined in UI
112100 const {
113- filteringConsentRuleValues, pixelUrl, redirectUrl, moduleId,
101+ filteringConsentRuleValues,
102+ pixelUrl,
103+ redirectUrl,
104+ moduleId,
114105 // Tells you how often we should do a cookie sync (in days)
115106 frequencyCap,
116107 } = pixelSettings ;
@@ -145,15 +136,18 @@ export default function CookieSyncManager(
145136 }
146137
147138 // Url for cookie sync pixel
148- const cookieSyncUrl = createCookieSyncUrl ( mpid , pixelUrl , redirectUrl ) ;
139+ const initialCookieSyncUrl = createInitialCookieSyncUrl ( mpid , pixelUrl , redirectUrl ) ;
149140 const moduleIdString = moduleId . toString ( ) ;
150141
151- // fullUrl will be the cookieSyncUrl if an error is thrown, or there if TcfApi is not available
142+ // The IAB Europe Transparency & Consent Framework requires adding certain query parameters to
143+ // a cookie sync url when using GDPR
144+ // fullUrl will be the initialCookieSyncUrl if an error is thrown, or there if TcfApi is not available.
145+ // Otherwise, it will include the GdprConsent parameters that TCF requires
152146 let fullUrl : string ;
153147 try {
154- fullUrl = isTcfApiAvailable ( ) ? await appendGdprConsentUrl ( cookieSyncUrl , mpInstance . Logger ) : cookieSyncUrl ;
148+ fullUrl = isTcfApiAvailable ( ) ? await appendGdprConsentUrl ( initialCookieSyncUrl ) : initialCookieSyncUrl ;
155149 } catch ( error ) {
156- fullUrl = cookieSyncUrl ;
150+ fullUrl = initialCookieSyncUrl ;
157151 const errorMessage = ( error as Error ) . message || error . toString ( ) ;
158152 mpInstance . Logger . error ( errorMessage ) ;
159153 }
@@ -206,26 +200,34 @@ export const isLastSyncDateExpired = (
206200
207201export const isTcfApiAvailable = ( ) : boolean => isFunction ( window . __tcfapi ) ;
208202
209- export async function appendGdprConsentUrl ( url : string , logger : Logger ) : Promise < string > {
203+ // This is just a partial definition of TCData for the purposes of our implementation. The full schema can be found here:
204+ // https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework/blob/master/TCFv2/IAB%20Tech%20Lab%20-%20CMP%20API%20v2.md#tcdata
205+ type TCData = {
206+ gdprApplies ?: boolean ;
207+ tcString ?: string ;
208+ } ;
209+
210+ export async function appendGdprConsentUrl ( url : string ) : Promise < string > {
210211 return new Promise ( ( resolve , reject ) => {
211212 try {
212213 const tcfAPICallBack = ( inAppTCData : TCData , success : boolean ) => {
213214 if ( success && inAppTCData ) {
214- const gdprApplies = inAppTCData . gdprApplies ? 1 : 0 ;
215+ // gdprApplies is a boolean, but the query parameter is 1 for true, or 0 for false
216+ const gdprApplies : number = inAppTCData . gdprApplies ? 1 : 0 ;
215217 const tcString = inAppTCData . tcString ;
216218 resolve ( `${ url } &gdpr=${ gdprApplies } &gdpr_consent=${ tcString } ` ) ;
217219 } else {
218220 resolve ( url ) ; // No GDPR data; fallback to original URL
219221 }
220222 }
221223
222- // `getInAppTCData` is the function name
223- // 2 is the version of TCF (2.2 as of 1/22/2025)
224- // callback
225- window . __tcfapi ( 'getInAppTCData' , 2 , tcfAPICallBack ) ;
224+ window . __tcfapi (
225+ 'getInAppTCData' , // function name that requests the TCData
226+ 2 , // version of TCF (2.2 as of 1/22/2025)
227+ tcfAPICallBack
228+ ) ;
226229 } catch ( error ) {
227230 reject ( error ) ;
228- // test to see if this line is reached. if so, just throw an error and remove 213/214
229231 }
230232 } ) ;
231233}
0 commit comments