Archived topic
Adding Google Analytics to wp_head hook
3 replies · Started by Mark on July 10, 2018
I am trying to add GA to my wp_head hook. Below is the code GA supplies for tracking. I disguised the UA-NUMBER, of course. This is exactly what I added in the hook input box area. Am I missing something? Do I have to tick the "Execute PHP" box?
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-58020794-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-XXXXXX-X');
</script>
Hi there,
that looks correct, no need to check Execute PHP.
Google provides a second piece of code, which say needs to go inside the body, this can be added to the before header hook.
Thanks David,
For anyone who is trying to do this, here's what worked for me.
I ended up just using the one code snippet (provided by Google) in the wp_head hook, and it works as is.
I went to Google Analytics, Admin, Tracking Info, Tracking Code. Copied everything in the box (see snippet above). Didn't add anything else, just the GA code snippet.
I didn't tick the “Execute PHP” box. And I didn't add anything to the Before Header hook in the body. I actually couldn't find anything about code in the body. Its working, so I'm just going to leave it as is.
Thanks.
Thats great to hear Mark, and thank you for providing this.