X

How To Automatically Include Google Analytics Code

Snippets by IsItWP

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.

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!
[63% OFF Today!]
[63% OFF Today!]
[60% OFF Today!]
[60% OFF Today!]
[50% OFF TODAY!]
[50% OFF TODAY!]
[63% OFF Today!]
[63% OFF Today!]
[60% OFF Today!]
[60% OFF Today!]
[50% OFF Today!]
[50% OFF Today!]