Skip to content

Commit 6884bff

Browse files
committed
turn comments
1 parent df4c310 commit 6884bff

File tree

4 files changed

+44
-42
lines changed

4 files changed

+44
-42
lines changed

src/cookieSyncManager.ts

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
Dictionary,
33
isEmpty,
4-
createCookieSyncUrl,
4+
createInitialCookieSyncUrl,
55
isFunction,
66
} from './utils';
77
import Constants from './constants';
@@ -17,12 +17,6 @@ export const DAYS_IN_MILLISECONDS = 1000 * 60 * 60 * 24;
1717

1818
export 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-
};
2620
declare 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

207201
export 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
}

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const replaceMPID = (value: string, mpid: MPID): string => value.replace('%%mpid
180180

181181
const replaceAmpWithAmpersand = (value: string): string => value.replace(/&amp;/g, '&');
182182

183-
const createCookieSyncUrl = (
183+
const createInitialCookieSyncUrl = (
184184
mpid: MPID,
185185
pixelUrl: string,
186186
redirectUrl?: string
@@ -358,7 +358,7 @@ const getHref = (): string => {
358358
export {
359359
createCookieString,
360360
revertCookieString,
361-
createCookieSyncUrl,
361+
createInitialCookieSyncUrl,
362362
valueof,
363363
converted,
364364
decoded,

test/jest/cookieSyncManager.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ describe('CookieSyncManager', () => {
608608
);
609609
});
610610

611-
const fullUrl = await appendGdprConsentUrl(mockUrl, mockMpInstance.Logger);
611+
const fullUrl = await appendGdprConsentUrl(mockUrl);
612612

613613
expect(fullUrl).toBe(
614614
`${mockUrl}&gdpr=1&gdpr_consent=test-consent-string`,
@@ -621,7 +621,7 @@ describe('CookieSyncManager', () => {
621621
callback(null, false); // Simulate a failure
622622
});
623623

624-
const fullUrl = await appendGdprConsentUrl(mockUrl, mockMpInstance.Logger);
624+
const fullUrl = await appendGdprConsentUrl(mockUrl);
625625
// Assert that the fallback method was called with the original URL
626626
expect(fullUrl).toBe(mockUrl);
627627
});
@@ -632,7 +632,7 @@ describe('CookieSyncManager', () => {
632632
throw new Error('Test Error');
633633
});
634634

635-
await expect(appendGdprConsentUrl(mockUrl, mockMpInstance.Logger)).rejects.toThrow('Test Error');
635+
await expect(appendGdprConsentUrl(mockUrl)).rejects.toThrow('Test Error');
636636
});
637637

638638
describe('#processPixelConfig', () => {
@@ -663,10 +663,10 @@ describe('CookieSyncManager', () => {
663663
cookieSyncManager.performCookieSync = jest.fn();
664664

665665
await cookieSyncManager.processPixelConfig(
666-
true,
666+
pixelSettings,
667667
{},
668668
testMPID,
669-
pixelSettings
669+
true,
670670
);
671671

672672
expect(cookieSyncManager.performCookieSync).toHaveBeenCalledWith(

test/jest/utils.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getHref,
55
replaceMPID,
66
replaceAmpWithAmpersand,
7-
createCookieSyncUrl,
7+
createInitialCookieSyncUrl,
88
} from '../../src/utils';
99
import { deleteAllCookies } from './utils';
1010

@@ -185,16 +185,16 @@ describe('Utils', () => {
185185
});
186186
});
187187

188-
describe('#createCookieSyncUrl', () => {
188+
describe('#createInitialCookieSyncUrl', () => {
189189
const pixelUrl: string = 'https://abc.abcdex.net/ibs:exampleid=12345&amp;exampleuuid=%%mpid%%&amp;redir=';
190190
const redirectUrl: string = 'https://cookiesync.mparticle.com/v1/sync?esid=123456&amp;MPID=%%mpid%%&amp;ID=${DD_UUID}&amp;Key=mpApiKey&amp;env=2';
191191

192192
it('should return a cookieSyncUrl when both pixelUrl and redirectUrl are not null', () => {
193-
expect(createCookieSyncUrl('testMPID', pixelUrl, redirectUrl)).toBe('https://abc.abcdex.net/ibs:exampleid=12345&exampleuuid=testMPID&redir=https%3A%2F%2Fcookiesync.mparticle.com%2Fv1%2Fsync%3Fesid%3D123456%26MPID%3DtestMPID%26ID%3D%24%7BDD_UUID%7D%26Key%3DmpApiKey%26env%3D2');
193+
expect(createInitialCookieSyncUrl('testMPID', pixelUrl, redirectUrl)).toBe('https://abc.abcdex.net/ibs:exampleid=12345&exampleuuid=testMPID&redir=https%3A%2F%2Fcookiesync.mparticle.com%2Fv1%2Fsync%3Fesid%3D123456%26MPID%3DtestMPID%26ID%3D%24%7BDD_UUID%7D%26Key%3DmpApiKey%26env%3D2');
194194
});
195195

196196
it('should return a cookieSyncUrl when pixelUrl is not null but redirectUrl is null', () => {
197-
expect(createCookieSyncUrl('testMPID', pixelUrl, null)).toBe('https://abc.abcdex.net/ibs:exampleid=12345&exampleuuid=testMPID&redir=');
197+
expect(createInitialCookieSyncUrl('testMPID', pixelUrl, null)).toBe('https://abc.abcdex.net/ibs:exampleid=12345&exampleuuid=testMPID&redir=');
198198
});
199199
});
200200
});

0 commit comments

Comments
 (0)