File size: 1,108 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { isRegionInCcpaZone } from '@automattic/calypso-analytics';
import cookie from 'cookie';

/**
 * The Global Privacy Control (GPC) flag is a browser setting that allows users to signal their privacy preference to websites.
 * It's still not implemented across browsers, so adding it here.
 * See: <https://globalprivacycontrol.org/>
 */
declare global {
	interface Navigator {
		globalPrivacyControl?: boolean;
	}
}

/**
 * Checks if the GPC flag is set in the user's browser.
 * @returns {boolean} - True if the GPC flag is set, false otherwise (flag is undefined or false).
 */
export const isGpcFlagSetOptOut = (): boolean => {
	return window.navigator?.globalPrivacyControl === true;
};

/**
 * May we track the user with GPC flag in CCPA region?
 * @returns {boolean} - False if the user is in the CCPA region and the GPC flag is set, true otherwise.
 */
export const mayWeTrackUserGpcInCcpaRegion = (): boolean => {
	const cookies = cookie.parse( document.cookie );
	if ( isRegionInCcpaZone( cookies.country_code, cookies.region ) && isGpcFlagSetOptOut() ) {
		return false;
	}
	return true;
};