Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Move Google Analytics to head and use asynchronous syntax. #394

wants to merge 1 commit into from

3 participants


No description provided.


thanks - it's already async though and we're trying to be as close to as possible

@retlehs retlehs closed this

The problem is that this does not adhere to Google's guidelines, and therefore many of their verification tools don't work when Analytics code is not directly before the closing . Additionally, with the amount of options in setting up analytics properly, the code you have is often not the best experience for users. I'd argue it should be removed completely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 5 deletions.
  1. +10 −5 inc/roots-actions.php
15 inc/roots-actions.php
@@ -12,12 +12,17 @@ function roots_google_analytics() {
$roots_google_analytics_id = GOOGLE_ANALYTICS_ID;
if ($roots_google_analytics_id !== '') {
echo "\n\t<script>\n";
- echo "\t\tvar _gaq=[['_setAccount','$roots_google_analytics_id'],['_trackPageview'],['_trackPageLoadTime']];\n";
- echo "\t\t(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];\n";
- echo "\t\tg.src=('https:'==location.protocol?'//ssl':'//www')+'';\n";
- echo "\t\ts.parentNode.insertBefore(g,s)}(document,'script'));\n";
+ echo "\t\tvar _gaq = _gaq || [];\n";
+ echo "\t\t_gaq.push(['_setAccount', '$roots_google_analytics_id']);\n";
+ echo "\t\t_gaq.push(['_trackPageview']);\n";
+ echo "\n";
+ echo "\t\t(function() {\n";
+ echo "\t\t\tvar ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n";
+ echo "\t\t\tga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';\n";
+ echo "\t\t\tvar s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n";
+ echo "\t\t})();\n";
echo "\t</script>\n";
-add_action('roots_footer', 'roots_google_analytics');
+add_action('roots_head', 'roots_google_analytics');
Something went wrong with that request. Please try again.