Skip to content

Commit 4ec4b2a

Browse files
committed
Calculate isTrackingCookieAllowed && update analytics Context
1 parent 9438f61 commit 4ec4b2a

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

packages/analytics-nextjs/src/AnalyticsProvider.tsx

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Script from 'next/script';
99
import type { PropsWithChildren } from 'react';
1010
import { createContext, useContext, useEffect, useState } from 'react';
1111

12-
import { isTrackingCookieAllowed } from './lib';
12+
import { isNavigatorTrackingAllowed } from './lib';
1313
import { logToConsole, normalizePrezlyMetaPlugin, sendEventToPrezlyPlugin } from './plugins';
1414
import { TrackingPolicy } from './types';
1515
import type {
@@ -22,15 +22,13 @@ interface Context {
2222
analytics: Analytics | undefined;
2323
consent: boolean | null;
2424
gallery?: PickedGalleryProperties;
25-
isEnabled: boolean;
2625
/**
2726
* - TRUE - tracking allowed (i.e. user clicked "Allow")
2827
* - FALSE - tracking disallowed (i.e. user clicked "Disallow" or browser "Do Not Track" mode is ON)
2928
* - NULL - unknown (i.e. user didn't click anything yet, and no browser preference set)
3029
*/
3130
isTrackingCookieAllowed: boolean | null;
3231
newsroom?: PickedNewsroomProperties;
33-
setConsent(consent: boolean): void;
3432
story?: PickedStoryProperties;
3533
trackingPolicy: TrackingPolicy;
3634
}
@@ -73,14 +71,25 @@ export function AnalyticsProvider({
7371
user,
7472
}: PropsWithChildren<Props>) {
7573
const {
76-
tracking_policy: trackingPolicy = TrackingPolicy.DEFAULT,
74+
tracking_policy: trackingPolicy = TrackingPolicy.DEFAULT as TrackingPolicy,
7775
segment_analytics_id: segmentWriteKey = customSegmentWriteKey,
7876
uuid,
7977
} = newsroom || {};
8078

81-
const [consent, setConsent] = useState<boolean | null>(null);
79+
const [isTrackingCookieAllowed, setIsTrackingCookieAllowed] = useState(
80+
trackingPolicy === TrackingPolicy.WILD_WEST,
81+
);
82+
8283
const [analytics, setAnalytics] = useState<Analytics | undefined>(undefined);
8384

85+
useEffect(() => {
86+
setIsTrackingCookieAllowed(
87+
isEnabled &&
88+
(isNavigatorTrackingAllowed() ?? false) &&
89+
trackingPolicy === TrackingPolicy.WILD_WEST,
90+
);
91+
}, [isEnabled, trackingPolicy]);
92+
8493
useEffect(() => {
8594
async function loadAnalytics(writeKey: string) {
8695
try {
@@ -155,13 +164,11 @@ export function AnalyticsProvider({
155164
<AnalyticsContext.Provider
156165
value={{
157166
analytics,
158-
consent,
167+
consent: null,
159168
gallery,
160-
isEnabled,
161-
isTrackingCookieAllowed: isTrackingCookieAllowed(consent, newsroom),
169+
isTrackingCookieAllowed,
162170
newsroom,
163171
story,
164-
setConsent,
165172
trackingPolicy,
166173
}}
167174
>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './getApiUrl';
22
export * from './getRecipientInfo';
3+
export * from './isNavigatorTrackingAllowed';
34
export * from './isTrackingCookieAllowed';
45
export * from './stringify';
56
export * from './urlParameters';

0 commit comments

Comments
 (0)