Skip to content

Commit f3811d5

Browse files
committed
Disable google tracking if tracking is not allowed
1 parent 4ec4b2a commit f3811d5

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

packages/analytics-nextjs/src/AnalyticsProvider.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export function AnalyticsProvider({
7373
const {
7474
tracking_policy: trackingPolicy = TrackingPolicy.DEFAULT as TrackingPolicy,
7575
segment_analytics_id: segmentWriteKey = customSegmentWriteKey,
76+
google_analytics_id: googleAnalyticsId,
7677
uuid,
7778
} = newsroom || {};
7879

@@ -90,6 +91,14 @@ export function AnalyticsProvider({
9091
);
9192
}, [isEnabled, trackingPolicy]);
9293

94+
useEffect(() => {
95+
if (!googleAnalyticsId) {
96+
return;
97+
}
98+
99+
window[`ga-disable-${googleAnalyticsId}`] = !isTrackingCookieAllowed;
100+
}, [isTrackingCookieAllowed, googleAnalyticsId]);
101+
93102
useEffect(() => {
94103
async function loadAnalytics(writeKey: string) {
95104
try {
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
declare interface Window {}
1+
type GoogleAnalyticsId = string;
2+
3+
interface Window {
4+
[key: `ga-disable-${string}`]: boolean | undefined;
5+
}

packages/analytics-nextjs/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export interface RecipientInfo {
1313

1414
// Pulled from `@prezly/sdk` to get rid of direct dependency requirement
1515
export enum TrackingPolicy {
16+
STRICT = 'strict',
1617
DEFAULT = 'default',
18+
WILD_WEST = 'wild-west',
1719
DISABLED = 'disabled',
1820
CONSENT_TO_IDENTIFY = 'consent-to-identify',
1921
}

0 commit comments

Comments
 (0)