Doug Fir
Doug Fir

Reputation: 21212

array.push() but no results in array?

Link here.

<script>
  dataLayer = [];
</script>

<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXX" // disguised GTM snippet
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PB4S9P');</script>
<!-- End Google Tag Manager -->

<script>
  dataLayer.push({
    'event':'nevent',
    'eventCategory': 'form submissions',
    'eventAction': 'saw form',
    'eventLabel': '[GPI - Donation Request Form]'  // e.g. Newsletter signup form - NEW SITE
  });  
</script>

When I visit the link and open the console and type dataLayer I expected to see the object. But I cannot.

When I am on the page if I cut n paste the dataLayer.push({}); it works - I see it added to the dataLayer.

So how could it be that the dataLayer array is not populated with the contents of the push below it?

Upvotes: 4

Views: 89

Answers (1)

Siguza
Siguza

Reputation: 23840

The provided code doesn't match the one on your page.
Your page is (somewhat) minified, i.e. it has no line breaks.
This conflicts with your end-of-line-style comment (//):

<script>  dataLayer.push({    'event':'nevent',    'eventCategory': 'form submissions',    'eventAction': 'saw form',    'eventLabel': '[GPI - Donation Request Form]'  // e.g. Newsletter signup form - NEW SITE  });  </script>

It cuts of the });, resulting in a syntax error.
Nevertheless though, I do get a result for typing dataLayer in the console:

[
    {
        "gtm.start": 1438884079089,
        "event": "gtm.js"
    },
    {
        "event": "gtm.dom"
    },
    {
        "event": "gtm.load"
    }
]

Upvotes: 4

Related Questions