Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Closed
wants to merge 1 commit into from

3 participants

@artichokes

No description provided.

@retlehs
Owner

thanks - it's already async though and we're trying to be as close to https://github.com/h5bp/html5-boilerplate as possible

@retlehs retlehs closed this
@kressaty

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.

@retlehs
Owner
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
View
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')+'.google-analytics.com/ga.js';\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') + '.google-analytics.com/ga.js';\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.