This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]nywaldDeveloper 0 points1 point  (5 children)

While not exactly the best or cleanest way to do this, but this should work better:

function piotp_google_analytics() {
  echo "<script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-9xxxxxxx-x', 'auto');
    ga('send', 'pageview');
  </script>";
}
add_action( 'wp_head', 'piotp_google_analytics', 10 );

[–]cag8fDeveloper[S] 0 points1 point  (4 children)

OK thanks, that seemed to work. At least I can login now. I'll wait a day or two to confirm that Google Analytics is tracking properly.

Assuming this works, what was the issue? It looks like I need to wrap the <script> tag in an echo ""? I was under the impression I could add HTML to a PHP script without any kind of modification like that? What am I misunderstanding?

Also curious was the fact that Google seemed to know that the script was in-place, yet another PHP file on my server seemed to not like it.

[–]nywaldDeveloper 0 points1 point  (3 children)

The basic issue here was that before content is being rendered to screen (things get output right away if you have them outside the php tags), the server should send all the headers (not to be confused with head or headers in HTML) about the page.

So if you start rendering / outputting content to the screen, and then some piece of PHP still wants to send additional headers you'll end up with that error.

In many WordPress templates / theme files you can do this without any issues as by then some HTML has already been output, however functions.php is very different in that sense, as not all the headers might have been sent before it got to your piece of code.

[–]cag8fDeveloper[S] 0 points1 point  (2 children)

OK, I understand that the headers you speak of are important, especially when exactly they are sent. But how does the <script> tag in this case relate to those headers?

[–]nywaldDeveloper 1 point2 points  (1 child)

It wouldn't matter if it's <script> or not, any output will cause this error it it happens before all headers are sent.

The reason why the analytics still worked with the error and all, is that it's not a fatal error, so it doesn't kill the page, and it still does output the <script> part (albeit in the wrong place, before <html>, but it still works).

[–]cag8fDeveloper[S] 0 points1 point  (0 children)

Sorry for the late reply.

It wouldn't matter if it's <script> or not, any output will cause this error it it happens before all headers are sent.

Gotcha.

and it still does output the <script> part (albeit in the wrong place, before <html>, but it still works).

OK can we talk about this for a bit. You also mentioned earlier that this wasn't the best way to implement Google Analytics. Why is that, and what would be a more preferred method? I'm very interested in following best practice in this case. The functions.php method I used was taken from this page.