4

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?

4
  • It works for me. When I visit the page and type dataLayer into the console. I get the object. Commented Aug 6, 2015 at 17:59
  • 2
    The GTM script probably loads after your push method and overwrites your dataLayer variable. Try putting it inside some variant of document.ready Commented Aug 6, 2015 at 17:59
  • It works for me too. Commented Aug 6, 2015 at 18:00
  • @Jan yes I think that's probably the issue. OP that Google thing is simply overwriting the value of your variable. Commented Aug 6, 2015 at 18:01

1 Answer 1

4

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"
    }
]
Sign up to request clarification or add additional context in comments.

5 Comments

That's one poor minifier that doesn't remove comments
@Jan It also doesn't even collapse multiple spaces into one... doesn't break functionality, but makes all other minification basically pointless.
Thank you so much. Accepting when the time limit comes off. So, how did you spot/realise that? Just so I know how to tell in future?
Well, visiting the link you posted gives a console error, so I'm guessing that was clue #1 ;) stackoverflow.com/tags/javascript/info <- Check the "debugging" section
@DougFirr The Chrome console told me that there's a syntax error, and I saw that your code was minified, so I dropped it into an HTML beautifier (just my favourite, not mine or anything), and then I was basically just saw that there's no bracket or anything after an indented block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.