-
Notifications
You must be signed in to change notification settings - Fork 355
Expand file tree
/
Copy pathprovider.tsx
More file actions
109 lines (88 loc) · 2.95 KB
/
Copy pathprovider.tsx
File metadata and controls
109 lines (88 loc) · 2.95 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use client';
import { useConsentManager } from '@c15t/nextjs/client';
import { PropsWithChildren, useEffect, useRef } from 'react';
import { FragmentOf } from '~/client/graphql';
import { Analytics } from '~/lib/analytics';
import { GoogleAnalyticsProvider } from '~/lib/analytics/providers/google-analytics';
import { MetaPixelProvider } from '~/lib/analytics/providers/meta-pixel';
import { AnalyticsProvider as AnalyticsProviderLib } from '~/lib/analytics/react';
import { AnalyticsProvider as AnalyticsProviderType } from '~/lib/analytics/types';
import { getConsentCookie } from '~/lib/consent-manager/cookies/client';
import { WebAnalyticsFragment } from './fragment';
interface Props {
channelId: number;
isCookieConsentEnabled: boolean;
settings?: FragmentOf<typeof WebAnalyticsFragment> | null;
}
const getConsent = () => {
const consentCookie = getConsentCookie();
if (!consentCookie) {
return null;
}
return {
functionality: consentCookie['c.functionality'],
marketing: consentCookie['c.marketing'],
measurement: consentCookie['c.measurement'],
necessary: consentCookie['c.necessary'],
};
};
const getAnalytics = ({ channelId, isCookieConsentEnabled, settings }: Props): Analytics | null => {
if (!channelId) {
return null;
}
const providers: AnalyticsProviderType[] = [];
if (settings?.webAnalytics?.ga4?.tagId) {
providers.push(
new GoogleAnalyticsProvider({
gaId: settings.webAnalytics.ga4.tagId,
consentModeEnabled: isCookieConsentEnabled,
developerId: 'dMjk3Nj',
getConsent,
}),
);
}
if (settings?.webAnalytics?.metaPixel?.pixelId) {
providers.push(
new MetaPixelProvider({
pixelId: settings.webAnalytics.metaPixel.pixelId,
consentModeEnabled: isCookieConsentEnabled,
getConsent,
}),
);
}
if (providers.length === 0) {
return null;
}
return new Analytics({ channelId, providers });
};
export function AnalyticsProvider({
channelId,
isCookieConsentEnabled,
settings,
children,
}: PropsWithChildren<Props>) {
const { consents } = useConsentManager();
const prevConsentsRef = useRef<Record<string, boolean> | null>(null);
const analytics = getAnalytics({
channelId,
isCookieConsentEnabled,
settings,
});
// Update consent when user changes preferences
useEffect(() => {
if (!isCookieConsentEnabled || !analytics) {
return;
}
const currentConsents = consents;
const prevConsents = prevConsentsRef.current;
// Check if consents have changed
if (prevConsents && JSON.stringify(currentConsents) !== JSON.stringify(prevConsents)) {
const consentState = getConsent();
if (consentState) {
analytics.consent.consentUpdated(consentState);
}
}
prevConsentsRef.current = currentConsents;
}, [isCookieConsentEnabled, analytics, consents]);
return <AnalyticsProviderLib analytics={analytics ?? null}>{children}</AnalyticsProviderLib>;
}