Skip to content

Commit 0a2a835

Browse files
committed
feat(google-tag-manager): warn instead of throw err
1 parent cad2467 commit 0a2a835

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`GoogleTagManager({...options})(events) When default dataLayer is not defined logs an error informing the developer that no events are being tracked 1`] = `
4+
"
5+
WARN
6+
[@redux-beacon/google-tag-manager] Events are not being tracked, window.dataLayer
7+
is not a function. Please include the Google Tag Manager snippet:
8+
https://developers.google.com/tag-manager/quickstart
9+
"
10+
`;
11+
12+
exports[`GoogleTagManager({...options})(events) When iAmADataLayer custom named dataLayer is not defined should log a warning to console informing the user. 1`] = `""`;

packages/google-tag-manager/src/__tests__/google-tag-manager.test.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
import * as makeConsoleMock from 'consolemock';
12
import GoogleTagManager from '../';
23

4+
beforeAll(() => {
5+
console = makeConsoleMock(console);
6+
});
7+
38
beforeEach(() => {
49
window.dataLayer = undefined;
510
window.iAmADataLayer = undefined;
611
});
712

13+
/* tslint:disable: no-console */
14+
afterEach(() => {
15+
console.clearHistory();
16+
});
17+
818
describe('GoogleTagManager({...options})(events)', () => {
919
describe('When given an array of events', () => {
1020
it('pushes those events to the data layer', () => {
@@ -70,23 +80,36 @@ describe('GoogleTagManager({...options})(events)', () => {
7080
});
7181

7282
describe('When default dataLayer is not defined', () => {
73-
it('should throw an error informing the user.', () => {
83+
it('does not throw an error', () => {
84+
window.dataLayer = undefined;
85+
86+
expect(() => GoogleTagManager()).not.toThrow();
87+
});
88+
it('does nothing when events are pushed to the target', () => {
89+
window.dataLayer = undefined;
90+
7491
const events = [{ hitType: 'pageview' }];
75-
expect(() => GoogleTagManager()(events)).toThrow(
76-
'window.dataLayer is not defined. Have you forgotten to include Google Tag Manager and dataLayer?'
77-
);
92+
const target = GoogleTagManager();
93+
94+
expect(() => target(events)).not.toThrow();
95+
});
96+
it('logs an error informing the developer that no events are being tracked', () => {
97+
window.dataLayer = undefined;
98+
99+
const events = [{ hitType: 'pageview' }];
100+
GoogleTagManager()(events);
101+
102+
expect(console.printHistory()).toMatchSnapshot();
78103
});
79104
});
80105

81106
describe('When iAmADataLayer custom named dataLayer is not defined', () => {
82-
it('should throw an error informing the user.', () => {
107+
it('should log a warning to console informing the user.', () => {
83108
const options = {
84109
dataLayerName: 'iAmADataLayer',
85110
};
86111
const events = [{ hitType: 'pageview' }];
87-
expect(() => GoogleTagManager(options)(events)).toThrow(
88-
'window.iAmADataLayer is not defined. Have you forgotten to include Google Tag Manager and dataLayer?'
89-
);
112+
expect(console.printHistory()).toMatchSnapshot();
90113
});
91114
});
92115
});

packages/google-tag-manager/src/google-tag-manager.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,14 @@ const GoogleTagManager = ({
1313
!(window as any)[dataLayerName] ||
1414
typeof (window as any)[dataLayerName].push !== 'function'
1515
) {
16-
throw new Error(
17-
`redux-beacon error: window.${dataLayerName} is not defined. Have you forgotten to include Google Tag Manager and dataLayer?`
18-
);
16+
/* tslint:disable: no-console */
17+
console.warn(`
18+
[@redux-beacon/google-tag-manager] Events are not being tracked, window.${dataLayerName}
19+
is not a function. Please include the Google Tag Manager snippet:
20+
https://developers.google.com/tag-manager/quickstart
21+
`);
22+
23+
return;
1924
}
2025

2126
events.forEach(event => {

0 commit comments

Comments
 (0)