X

Auto include Google analytics Code

Adding this PHP code snippet to the functions.php of your wordpress theme will add Google analytics, be sure to replace UA-XXXXXXX-X with your own google 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');

Comments  Leave a Reply

  1. Tweets that mention Wordpress Auto include Google analytics Code – wpsnipp.com Wordpress code snippets for your blog -- Topsy.com February 6, 2011 at 4:43 pm

    […] 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] […]

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!