Are you looking for a way to automatically include Google Analytics code? While there’s probably a plugin for this, we have created a quick code snippet that you can use to automatically include Google Analytics code in WordPress.
Instructions:
All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin, then be sure to replace the UA-XXXXXXX-X in line 9 with your own Google Analytics tracking code.
function add_google_analytics() {
?>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-XXXXXXX-X");
pageTracker._trackPageview();
} catch(err) {}</script>
<?php
}
add_action('wp_footer', 'add_google_analytics');
If you liked this code snippet, please consider checking out our other articles on the site like: How to Install Google Analytics on Your WordPress Site and how to set up author tracking in WordPress with Google Analytics.
[…] This post was mentioned on Twitter by Y Combinator Newest! and HN Firehose, newsery1. newsery1 said: Wordpress auto include Google analytics Code – http://bit.ly/fPtIjD – [Hacker News FH] […]