Skip to content

Commit 52f2328

Browse files
authored
Fix: avoid push when datalayer doesnt exist (#37)
1 parent bc79f55 commit 52f2328

File tree

4 files changed

+7
-3
lines changed

4 files changed

+7
-3
lines changed

dist/TagManager.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var TagManager = {
6464
_ref2$dataLayerName = _ref2.dataLayerName,
6565
dataLayerName = _ref2$dataLayerName === undefined ? 'dataLayer' : _ref2$dataLayerName;
6666

67+
if (window[dataLayerName]) return window[dataLayerName].push(_dataLayer);
6768
var snippets = _Snippets2.default.dataLayer(_dataLayer, dataLayerName);
6869
var dataScript = this.dataScript(snippets);
6970
document.head.appendChild(dataScript);

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-gtm-module",
3-
"version": "2.0.7",
3+
"version": "2.0.8",
44
"description": "React Google Tag Manager Module",
55
"main": "dist/index.js",
66
"scripts": {

src/TagManager.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ const TagManager = {
4343
document.body.insertBefore(gtm.noScript(), document.body.childNodes[0])
4444
},
4545
dataLayer: function ({dataLayer, dataLayerName = 'dataLayer'}) {
46-
window[dataLayerName].push(dataLayer)
46+
if (window[dataLayerName]) return window[dataLayerName].push(dataLayer)
47+
const snippets = Snippets.dataLayer(dataLayer, dataLayerName)
48+
const dataScript = this.dataScript(snippets)
49+
document.head.appendChild(dataScript)
4750
}
4851
}
4952

0 commit comments

Comments
 (0)