zomboble
zomboble

Reputation: 833

Google Analytics causing 500 internal error

Using this code generated from googles analytic product: (information censored)

<meta name="google-site-verification" content="xxxxxxxxxxxx" />



<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'xxxxxxxx']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 
    'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 
    'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(ga, s);
  })();

</script>

Causes internal 500 error, the code is in the head tag

Upvotes: 0

Views: 4947

Answers (4)

Michael
Michael

Reputation: 13

I have gotten the 500 Error too. In my case, it was the template engine smarty!!!

It takes this line as a smarty command because of the {}

function gtag(){dataLayer.push(arguments);}

change this to:

{literal}function gtag(){dataLayer.push(arguments);}{/literal}

maybe someone runns in this error with smarty too :-)

Upvotes: 0

jtmulls18
jtmulls18

Reputation: 129

If your website is using the Smarty Template engine, just pasting this code into the template will cause the 500 Internal Server Error, because the template is interpreting the code improperly.

Past the code inside a

{literal} 
...
{/literal}

block and your problems will be solved!

Shout out to Logicia.co.uk blog which helped me out with this same issue.

Upvotes: 7

mike
mike

Reputation: 7177

WAG (Wild-A**-Guess)*, based on your comment about lots of other SEO stuff and scripts in head...

Split the analytics code into two parts. Put the part that loads ga.js closer to the top of head, above any other script calls.

<script type="text/javascript">
  (function() {
    var ga = document.createElement('script'); ga.type = 
    'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 
    'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; 
    s.parentNode.insertBefore(ga, s);
  })();
</script>

Put the other part, that performs the analytics at the bottom of the head tag, or even at the bottom of the body tag.

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'xxxxxxxx']);
  _gaq.push(['_trackPageview']);
</script>

If you're still having problems, see what happens if you remove the _gaq code altogether

  • If you still get 500 errors, then the problem is with the async loading of ga.js. You could try switching to the older, non-async style of Google Analytics.
  • If the error goes away, then the problem is with the analytics tracking pixels requests...

Upvotes: -1

Brodie
Brodie

Reputation: 8757

have you tried moving the script tag to the bottom of your content before the end of the element?

ie:

<body>
  stuff here
  <ga script include>
</body>

The script itself is appending and loading another script tag to the top of the head. So you might be getting conflicts between GA appending and loading the new script and your other JS compiling.

Upvotes: 0

Related Questions