@@ -63,17 +63,27 @@ type TrackEventCallback = <EventName extends InsightsEventName>(
63
63
const InsightsContext = React . createContext < TrackEventCallback > ( ( ) => { } ) ;
64
64
65
65
interface InsightsProviderProps extends InsightsEventContext {
66
+ /** If true, the events will be sent to the server. */
66
67
enabled : boolean ;
68
+
69
+ /** If true, the visitor cookie tracking will be used */
70
+ visitorCookieTrackingEnabled : boolean ;
71
+
72
+ /** The URL of the app. */
67
73
appURL : string ;
74
+
75
+ /** The host of the API. */
68
76
apiHost : string ;
77
+
78
+ /** The children of the provider. */
69
79
children : React . ReactNode ;
70
80
}
71
81
72
82
/**
73
83
* Wrap the content of the app with the InsightsProvider to track events.
74
84
*/
75
85
export function InsightsProvider ( props : InsightsProviderProps ) {
76
- const { enabled, appURL, apiHost, children, ...context } = props ;
86
+ const { enabled, appURL, apiHost, children, visitorCookieTrackingEnabled , ...context } = props ;
77
87
78
88
const visitorIdRef = React . useRef < string | null > ( null ) ;
79
89
const eventsRef = React . useRef < {
@@ -140,7 +150,8 @@ export function InsightsProvider(props: InsightsProviderProps) {
140
150
} ) ;
141
151
142
152
const flushBatchedEvents = useDebounceCallback ( async ( ) => {
143
- const visitorId = visitorIdRef . current ?? ( await getVisitorId ( appURL ) ) ;
153
+ const visitorId =
154
+ visitorIdRef . current ?? ( await getVisitorId ( appURL , visitorCookieTrackingEnabled ) ) ;
144
155
visitorIdRef . current = visitorId ;
145
156
146
157
flushEventsSync ( ) ;
@@ -184,15 +195,15 @@ export function InsightsProvider(props: InsightsProviderProps) {
184
195
* Get the visitor ID and store it in a ref.
185
196
*/
186
197
React . useEffect ( ( ) => {
187
- getVisitorId ( appURL ) . then ( ( visitorId ) => {
198
+ getVisitorId ( appURL , visitorCookieTrackingEnabled ) . then ( ( visitorId ) => {
188
199
visitorIdRef . current = visitorId ;
189
200
// When the page is unloaded, flush all events, but only if the visitor ID is set
190
201
window . addEventListener ( 'beforeunload' , flushEventsSync ) ;
191
202
} ) ;
192
203
return ( ) => {
193
204
window . removeEventListener ( 'beforeunload' , flushEventsSync ) ;
194
205
} ;
195
- } , [ flushEventsSync , appURL ] ) ;
206
+ } , [ flushEventsSync , appURL , visitorCookieTrackingEnabled ] ) ;
196
207
197
208
return (
198
209
< InsightsContext . Provider value = { trackEvent } >
0 commit comments