Skip to content

Commit 6744edb

Browse files
authored
fix(use-gtm): ensure environment auth is present before injecting url (#719)
1 parent 66bcdca commit 6744edb

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ exports[`GTM hook Provider should call onLoadError if script fail to load 1`] =
1515
})(window,document,'script','dataLayer','testId');</script>"
1616
`;
1717
18+
exports[`GTM hook Provider should load env when environment auth is missing 1`] = `
19+
"<script src=\\"https://www.googletagmanager.com/gtm.js?id=testId\\"></script><script>window.dataLayer = window.dataLayer || [];</script><script>(function(w,d,s,l,i){w[l]=w[l]||[];
20+
w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});
21+
var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
22+
j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl+'';
23+
24+
j.addEventListener('error', function() {
25+
var _ge = new CustomEvent('gtm_loading_error', { bubbles: true });
26+
d.dispatchEvent(_ge);
27+
});
28+
29+
f.parentNode.insertBefore(j,f);
30+
})(window,document,'script','dataLayer','testId');</script>"
31+
`;
32+
1833
exports[`GTM hook Provider should load when id and environment is provided 1`] = `
1934
"<script src=\\"https://www.googletagmanager.com/gtm.js?id=testId&amp;gtm_auth=gtm&amp;gtm_preview=world&amp;gtm_cookies_win=x\\"></script><script>window.dataLayer = window.dataLayer || [];</script><script>(function(w,d,s,l,i){w[l]=w[l]||[];
2035
w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,20 @@ describe('GTM hook', () => {
8888
expect(document.head.innerHTML).toMatchSnapshot()
8989
})
9090

91+
it('Provider should load env when environment auth is missing', () => {
92+
renderHook(() => useGTM<DefaultEvents>(), {
93+
wrapper: wrapper({
94+
// @ts-expect-error we test a failing case
95+
environment: {
96+
preview: 'world',
97+
},
98+
id: 'testId',
99+
}),
100+
})
101+
102+
expect(document.head.innerHTML).toMatchSnapshot()
103+
})
104+
91105
it('Provider should load with events when provided', () => {
92106
const { result } = renderHook(() => useGTM<DefaultEvents>(), {
93107
wrapper: wrapper({

packages/use-gtm/src/scripts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export const DATALAYER_NAME = 'dataLayer'
44
export const LOAD_ERROR_EVENT = 'gtm_loading_error'
55

66
const flattenEnvironment = (environment?: GTMEnvironment) =>
7-
environment
8-
? `&${Object.entries(environment)
7+
environment && environment.auth
8+
? `&${Object.entries({ ...environment, cookies_win: 'x' })
99
.filter(([, value]) => !!value)
1010
.map(([key, value]) => `gtm_${key}=${value}`, '')
11-
.join('&')}&gtm_cookies_win=x`
11+
.join('&')}`
1212
: ''
1313

1414
const generateSnippets = (id: string, environment?: GTMEnvironment) => {

0 commit comments

Comments
 (0)