@@ -55,13 +55,13 @@ export type AnalyticsProviderProps<T> = {
5555 loadOptions ?: LoadOptions
5656
5757 /**
58- * This option help you in case you don't want to load analytics
58+ * This option force provider to render children only when isAnalytics is ready
5959 */
60- shouldLoadAnalytics ?: boolean
60+ shouldRenderOnlyWhenReady ?: boolean
6161 /**
62- * // This option force provider to render children only when isAnalytics is ready
62+ * used with shouldRenderOnlyWhenReady can blocked rendering until consent the first time.
6363 */
64- shouldRenderOnlyWhenReady ?: boolean
64+ needConsent ?: boolean
6565 allowedConsents : CategoryKind [ ]
6666 deniedConsents : CategoryKind [ ]
6767 onError ?: ( err : Error ) => void
@@ -79,18 +79,26 @@ export function AnalyticsProvider<T extends Events>({
7979 settings,
8080 loadOptions,
8181 shouldRenderOnlyWhenReady = false ,
82+ needConsent = true ,
8283 onError,
8384 onEventError,
8485 allowedConsents,
8586 deniedConsents,
8687 events,
88+ onLoaded,
8789} : AnalyticsProviderProps < T > ) {
8890 const [ isAnalyticsReady , setIsAnalyticsReady ] = useState ( false )
8991 const [ internalAnalytics , setAnalytics ] = useState < Analytics | undefined > (
9092 undefined ,
9193 )
9294
93- const shouldLoad = useMemo ( ( ) => ! ! settings ?. writeKey , [ settings ?. writeKey ] )
95+ const shouldLoad = useMemo ( ( ) => {
96+ if ( needConsent ) {
97+ return false
98+ }
99+
100+ return ! ! settings ?. writeKey
101+ } , [ settings ?. writeKey , needConsent ] )
94102
95103 useDeepCompareEffectNoCheck ( ( ) => {
96104 if ( shouldLoad && settings ) {
@@ -112,6 +120,8 @@ export function AnalyticsProvider<T extends Events>({
112120 } ,
113121 } )
114122
123+ onLoaded ( rudderAnalytics )
124+
115125 setIsAnalyticsReady ( true )
116126 } ,
117127 ...loadOptions ,
@@ -122,10 +132,11 @@ export function AnalyticsProvider<T extends Events>({
122132 setAnalytics ( { ...analytics , trackLink : trackLink ( analytics ) } )
123133 setIsAnalyticsReady ( true )
124134 } )
125- } else if ( ! shouldLoad ) {
126- // When user has refused tracking, set ready anyway
127- setIsAnalyticsReady ( true )
128135 }
136+ // else if (!shouldLoad && !needConsent ) {
137+ // // When user has refused tracking, set ready anyway
138+ // setIsAnalyticsReady(true)
139+ // }
129140 } , [ onError , settings , loadOptions , shouldLoad ] )
130141
131142 const value = useMemo < AnalyticsContextInterface < T > > ( ( ) => {
@@ -144,7 +155,18 @@ export function AnalyticsProvider<T extends Events>({
144155 }
145156 } , [ events , internalAnalytics , isAnalyticsReady , onEventError ] )
146157
147- const shouldRender = ! shouldRenderOnlyWhenReady || isAnalyticsReady
158+ const shouldRender =
159+ ! shouldRenderOnlyWhenReady || ( isAnalyticsReady && ! needConsent )
160+
161+ useDeepCompareEffectNoCheck ( ( ) => {
162+ internalAnalytics ?. consent ( {
163+ consentManagement : {
164+ enabled : true ,
165+ allowedConsentIds : allowedConsents ,
166+ deniedConsentIds : deniedConsents ,
167+ } ,
168+ } )
169+ } , [ allowedConsents , deniedConsents ] )
148170
149171 return (
150172 < AnalyticsContext . Provider value = { value } >
0 commit comments