Skip to content

Commit 3c3c357

Browse files
committed
Fix localStorage permissions if DNT is not enabled
1 parent b33e5f3 commit 3c3c357

File tree

1 file changed

+32
-14
lines changed
  • packages/common/src/utils/analytics

1 file changed

+32
-14
lines changed

packages/common/src/utils/analytics/utils.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,38 @@ export const WHITELISTED_VSCODE_EVENTS = [
6767
'code-runner.run',
6868
];
6969

70-
export const DO_NOT_TRACK_ENABLED =
71-
typeof window !== 'undefined' &&
72-
Boolean(
73-
// @ts-ignore
74-
global.doNotTrack === '1' ||
75-
// @ts-ignore
76-
global.navigator.doNotTrack === '1' ||
77-
// @ts-ignore
78-
global.navigator.msDoNotTrack === '1' ||
79-
(typeof localStorage !== 'undefined' &&
80-
localStorage.getItem('DO_NOT_TRACK_ENABLED')) ||
81-
process.env.NODE_ENV === 'development' ||
82-
process.env.STAGING
83-
);
70+
const isDoNotTrackEnabled = () => {
71+
try {
72+
if (typeof window !== 'undefined') {
73+
let localStorageValue = true;
74+
try {
75+
localStorageValue =
76+
typeof localStorage !== 'undefined' &&
77+
localStorage.getItem('DO_NOT_TRACK_ENABLED') === 'true';
78+
} catch (e) {
79+
/* ignore */
80+
}
81+
82+
return Boolean(
83+
// @ts-ignore
84+
global.doNotTrack === '1' ||
85+
// @ts-ignore
86+
global.navigator.doNotTrack === '1' ||
87+
// @ts-ignore
88+
global.navigator.msDoNotTrack === '1' ||
89+
localStorageValue ||
90+
process.env.NODE_ENV === 'development' ||
91+
process.env.STAGING
92+
);
93+
}
94+
95+
return true;
96+
} catch (e) {
97+
return false;
98+
}
99+
};
100+
101+
export const DO_NOT_TRACK_ENABLED = isDoNotTrackEnabled();
84102

85103
export const isAllowedEvent = (eventName, secondArg) => {
86104
try {

0 commit comments

Comments
 (0)