Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7b991b9

Browse files
committed
feat(google-tag-manager): warn instead of throw err
1 parentcad2467 commit7b991b9

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-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 @@
11
importGoogleTagManagerfrom'../';
2+
import*asmakeConsoleMockfrom'consolemock';
3+
4+
beforeAll(()=>{
5+
console=makeConsoleMock(console);
6+
});
27

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
constevents=[{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+
consttarget=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+
constevents=[{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('shouldthrow an error informing the user.',()=>{
107+
it('shouldlog a warning to console informing the user.',()=>{
83108
constoptions={
84109
dataLayerName:'iAmADataLayer',
85110
};
86111
constevents=[{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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ const GoogleTagManager = ({
1313
!(windowasany)[dataLayerName]||
1414
typeof(windowasany)[dataLayerName].push!=='function'
1515
){
16-
thrownewError(
17-
`redux-beacon error: window.${dataLayerName} is not defined. Have you forgotten to include Google Tag Manager and dataLayer?`
18-
);
16+
console.warn(`
17+
[@redux-beacon/google-tag-manager] Events are not being tracked, window.${dataLayerName}
18+
is not a function. Please include the Google Tag Manager snippet:
19+
https://developers.google.com/tag-manager/quickstart
20+
`);
21+
22+
return;
1923
}
2024

2125
events.forEach(event=>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp