Skip to content

Commit bfb0809

Browse files
authored
feat: do not load snippets if settings not provided (#731)
1 parent 1329eef commit bfb0809

File tree

5 files changed

+100
-13
lines changed

5 files changed

+100
-13
lines changed

packages/use-gtm/src/__tests__/__snapshots__/index.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,5 @@ Array [
7272
},
7373
]
7474
`;
75+
76+
exports[`GTM hook Provider should not load when no id is provided 1`] = `""`;

packages/use-gtm/src/__tests__/index.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ describe('GTM hook', () => {
6464
expect(document.head.innerHTML).toMatchSnapshot()
6565
})
6666

67+
it('Provider should not load when no id is provided', () => {
68+
renderHook(() => useGTM<DefaultEvents>(), {
69+
wrapper: wrapper({}),
70+
})
71+
72+
expect(document.head.innerHTML).toMatchSnapshot()
73+
})
74+
6775
it('Provider should load when id is provided', () => {
6876
renderHook(() => useGTM<DefaultEvents>(), {
6977
wrapper: wrapper({

packages/use-gtm/src/useGTM.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function useGTM<T extends Events>(): GTMContextInterface<T> {
3131
}
3232

3333
export type GTMProviderProps<T> = {
34-
id: string
34+
id?: string
3535
environment?: GTMEnvironment
3636
children: ReactNode
3737
onLoadError?: () => void
@@ -45,20 +45,29 @@ function GTMProvider<T extends Events>({
4545
onLoadError,
4646
events,
4747
}: GTMProviderProps<T>) {
48+
const shouldLoad = !!id
49+
4850
useEffect(() => {
49-
const { noScript, script, dataLayerInit } = generateScripts(id, environment)
51+
if (shouldLoad) {
52+
const { noScript, script, dataLayerInit } = generateScripts(
53+
id,
54+
environment,
55+
)
5056

51-
document.head.insertBefore(dataLayerInit, document.head.childNodes[0])
52-
document.head.insertBefore(script, document.head.childNodes[1])
53-
document.body.insertBefore(noScript, document.body.childNodes[0])
57+
document.head.insertBefore(dataLayerInit, document.head.childNodes[0])
58+
document.head.insertBefore(script, document.head.childNodes[1])
59+
document.body.insertBefore(noScript, document.body.childNodes[0])
5460

55-
if (onLoadError) document.addEventListener(LOAD_ERROR_EVENT, onLoadError)
61+
if (onLoadError) document.addEventListener(LOAD_ERROR_EVENT, onLoadError)
5662

57-
return () => {
58-
if (onLoadError)
59-
document.removeEventListener(LOAD_ERROR_EVENT, onLoadError)
63+
return () => {
64+
if (onLoadError)
65+
document.removeEventListener(LOAD_ERROR_EVENT, onLoadError)
66+
}
6067
}
61-
}, [environment, id, onLoadError])
68+
69+
return () => {}
70+
}, [environment, id, onLoadError, shouldLoad])
6271

6372
const value = useMemo<GTMContextInterface<T>>(() => {
6473
const curiedEvents = Object.entries(events || {}).reduce(

packages/use-segment/src/__tests__/index.tsx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,62 @@ describe('segment hook', () => {
8080
settings: undefined,
8181
}),
8282
})
83-
expect(result.current.analytics).not.toBeNull()
83+
expect(result.current.analytics).toBe(undefined)
84+
})
85+
86+
it('useSegment should not load when All integrations disabled', () => {
87+
const { result } = renderHook(() => useSegment<DefaultEvents>(), {
88+
wrapper: wrapper({
89+
events: defaultEvents,
90+
initOptions: { integrations: { All: false } },
91+
settings: { writeKey: 'sample ' },
92+
}),
93+
})
94+
expect(result.current.analytics).toBe(undefined)
95+
})
96+
97+
it('useSegment should not load when all of integrations disabled', () => {
98+
const { result } = renderHook(() => useSegment<DefaultEvents>(), {
99+
wrapper: wrapper({
100+
events: defaultEvents,
101+
initOptions: {
102+
integrations: {
103+
testInteg: false,
104+
testInteg2: false,
105+
testInteg3: false,
106+
},
107+
},
108+
settings: { writeKey: 'sample ' },
109+
}),
110+
})
111+
expect(result.current.analytics).toBe(undefined)
112+
})
113+
114+
it('useSegment should load when at least one integrations enabled', async () => {
115+
const mock = jest
116+
.spyOn(AnalyticsBrowser, 'load')
117+
.mockResolvedValue([{} as Analytics, {} as Context])
118+
119+
const { result, waitForNextUpdate } = renderHook(
120+
() => useSegment<DefaultEvents>(),
121+
{
122+
wrapper: wrapper({
123+
events: defaultEvents,
124+
initOptions: {
125+
integrations: {
126+
testInteg: false,
127+
testInteg2: true,
128+
testInteg3: false,
129+
},
130+
},
131+
settings: { writeKey: 'sample ' },
132+
}),
133+
},
134+
)
135+
136+
await waitForNextUpdate()
137+
expect(mock).toHaveBeenCalledTimes(1)
138+
expect(result.current.analytics).toStrictEqual({})
84139
})
85140

86141
it('Provider should load with key', async () => {

packages/use-segment/src/useSegment.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,28 @@ function SegmentProvider<T extends Events>({
6363
undefined,
6464
)
6565

66+
const shouldLoad = useMemo(() => {
67+
const hasNoIntegrationsSettings = !initOptions?.integrations
68+
const isAllEnabled = !!initOptions?.integrations?.All
69+
const isAnyIntegrationEnabled = Object.values(
70+
initOptions?.integrations ?? {},
71+
).reduce<boolean>((acc, integration) => !!acc || !!integration, false)
72+
73+
return (
74+
!!settings?.writeKey &&
75+
(hasNoIntegrationsSettings || isAllEnabled || isAnyIntegrationEnabled)
76+
)
77+
}, [initOptions?.integrations, settings?.writeKey])
78+
6679
useEffect(() => {
67-
if (settings?.writeKey) {
80+
if (shouldLoad && settings) {
6881
AnalyticsBrowser.load(settings, initOptions)
6982
.then(([res]) => setAnalytics(res))
7083
.catch((err: Error) => {
7184
onError?.(err)
7285
})
7386
}
74-
}, [onError, settings, initOptions])
87+
}, [onError, settings, initOptions, shouldLoad])
7588

7689
const value = useMemo<SegmentContextInterface<T>>(() => {
7790
const curiedEvents = Object.entries(events).reduce(

0 commit comments

Comments
 (0)