From 52f2328ab1b45734c342e9021fe693795a03972d Mon Sep 17 00:00:00 2001 From: Aline Morelli Date: Thu, 29 Aug 2019 18:56:25 -0300 Subject: [PATCH] Fix: avoid push when datalayer doesnt exist (#37) --- dist/TagManager.js | 1 + package-lock.json | 2 +- package.json | 2 +- src/TagManager.js | 5 ++++- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/dist/TagManager.js b/dist/TagManager.js index 7f0bc7cd..115c753c 100644 --- a/dist/TagManager.js +++ b/dist/TagManager.js @@ -64,6 +64,7 @@ var TagManager = { _ref2$dataLayerName = _ref2.dataLayerName, dataLayerName = _ref2$dataLayerName === undefined ? 'dataLayer' : _ref2$dataLayerName; + if (window[dataLayerName]) return window[dataLayerName].push(_dataLayer); var snippets = _Snippets2.default.dataLayer(_dataLayer, dataLayerName); var dataScript = this.dataScript(snippets); document.head.appendChild(dataScript); diff --git a/package-lock.json b/package-lock.json index 2db8a948..e93f6a4f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "react-gtm-module", - "version": "2.0.5", + "version": "2.0.7", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 99664058..5f141e40 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-gtm-module", - "version": "2.0.7", + "version": "2.0.8", "description": "React Google Tag Manager Module", "main": "dist/index.js", "scripts": { diff --git a/src/TagManager.js b/src/TagManager.js index 902cf03b..f2abf1a1 100644 --- a/src/TagManager.js +++ b/src/TagManager.js @@ -43,7 +43,10 @@ const TagManager = { document.body.insertBefore(gtm.noScript(), document.body.childNodes[0]) }, dataLayer: function ({dataLayer, dataLayerName = 'dataLayer'}) { - window[dataLayerName].push(dataLayer) + if (window[dataLayerName]) return window[dataLayerName].push(dataLayer) + const snippets = Snippets.dataLayer(dataLayer, dataLayerName) + const dataScript = this.dataScript(snippets) + document.head.appendChild(dataScript) } }