Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"unreachable code after return statement" error in Google Analytics JS code on Firefox - is this my fault?

Tags:

I'm using the current Google Analytics tracking code, and on Firefox 59 I get this error in console:

unreachable code after return statement

On this javascript: https://www.googletagmanager.com/gtag/js?id=UA-my-tracking-code:formatted

Since this problem doesn't seem to be googleable, hence it seems others do not have this problem, my guess is that it's me who does something wrong. But what this could be?

I'm putting the snippet in <head>, just like the docs say:

<!DOCTYPE html> <html>     <head>         <title>some title</title>         <meta charset="UTF-8">         <meta name="viewport" content="width=device-width, initial-scale=1.0">          <!-- Global site tag (gtag.js) - Google Analytics -->         <script async src="https://www.googletagmanager.com/gtag/js?id=UA-my-tracking-code"></script>          <script>           window.dataLayer = window.dataLayer || [];           function gtag(){dataLayer.push(arguments);}           gtag('js', new Date());           gtag('config', 'UA-my-tracking-code');         </script>      </head>     <body>      blah blah      </body> </html> 
like image 946
konrados Avatar asked Apr 28 '18 19:04

konrados


People also ask

Why is code unreachable after return statement?

The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

What is https www Google Analytics com Analytics JS?

Analytics. js is a JavaScript library that is used to measure users' interactions with your website. Whenever we refer to Google Analytics, we are actually referring to this library.


Video Answer


2 Answers

It is not your fault. If you prettify the code with Firefox's it can be found here:

Gc = function (a, b, c) {   var d = a.split('.');   var e = function (a, b) {     for (var c = 0; void 0 !== a && c < d.length; c++) {       if (null === a) return !1;       a = a[d[c]]     }     return void 0 !== a || 1 < c ? a : b.length ? e(Hc(b.pop()), b)  : Ic(d)   };   return e(Cc.eventModel, [         // <= True return value     b,     c   ]);   return Ic(d)  // <= This can never be reached! }, 

To prettify the code:

  1. Click on the warning's blue link. You'll see the garbled code.
  2. Click on the icon with the {} on the bottom left of that panel to prettify it. However the reference will be lost.
  3. Go back to the "Console" panel and click again in the same warning's blue link. Now you'll see the original reference in the correct line.

Firefox is correctly pointing the bug, the function Ic(d) will never be reached since the function returns earlier. Seems like a Google Tag Manager bug, however without inspecting the original code we cannot tell the severity.

like image 143
Me - Avatar answered Oct 13 '22 00:10

Me -


My OCD will not let this be. Warnings are as bad as errors to my brain... lol

The answer provided by @Me - caused me to perform the following:

  1. Navigate to the url in the <script async src="https://www.googletagmanager.com/gtag/js?id={Google Analytics Id here}"></script> tag and grab a copy of the JavaScript.
  2. Drop it into a file in my solution and replace the remote js src with this new local copy.
  3. Re-format the file (prettify).
  4. Comment out the offending line (if it can't be reached as-is I don't expect terrible harm to come from simply removing it).

The warning is gone, and so far everything seems to be working - no additional or new errors/warnings in the console and Analytics/Tracking data still seems to be coming through.

This is clearly not an ideal solution - I'll have to continue monitoring this until either a) Google fixes their code (which, incidentally, omits semi-colons all over - I doubt it'd come close to passing a Linter) or b) Google makes other changes to the code, causing mine to break or my Analytics to stop reporting.

like image 43
challamzinniagroup Avatar answered Oct 13 '22 00:10

challamzinniagroup