0

I am using google tag manager in my chrome-extension, but on the maximum website, it will not load properly, give me the error Cannot read property 'parentNode' of undefined (f.parentNode.insertBefore(j,f)).error image

my google tag code image

  • Try following the instructions at https://developers.google.com/tag-manager/quickstart. You'll need to replace `GTM-XXXX` with you GTM container ID for starters. – shender Sep 04 '20 at 06:48
  • @sh78 i replace gmt -id with XXX for posting question – Deepak Kashyap Sep 04 '20 at 07:05
  • Did you copy the *entire* code snippet from step 1, and follow *all* of the Google instructions? Also, what does "on the maximum website" mean? – shender Sep 04 '20 at 21:55

1 Answers1

0

For some reason, the variable f in the line f.parentNode.insertBefore(j,f) is undefined. Here is the declaration of f, which is on line 11 of gtm.js in the screenshot you posted:

var f=d.getElementsByTagName(s)[0]

The variables d and s are being passed in as document and "script", so f should be the first <script> tag on the webpage. The only case I can think of that would make this fail is when the page has no <script> tags. Normally this would not be the case because the GTM script is usually on the page, but since you're using a Chrome Extension, it may not be.

I would try this instead:

var f=d.head.firstChild
Ben Larson
  • 404
  • 1
  • 5
  • 11
  • this problem is solved,now I m stuck on dataLayer, i write this command in my code -->window.dataLayer.push({'event': 'mynewevent'}); but this not sent to google analytics, but when i run same command in browser console...this hit successfully in browser console .. – Deepak Kashyap Sep 07 '20 at 13:42
  • Is the push happening from inside the Chrome Extension? Extensions don't have access to the `window`, so you might want to [inject a script onto the page to push the event for you](https://stackoverflow.com/questions/20499994/access-window-variable-from-content-script). – Ben Larson Sep 08 '20 at 22:56