Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Business

Google Analytics

Hi there,

I'm redesigning this site for someone - teafields.co.uk. I'm building the new site in WordPress.

The owner uses Google Analytics in the current site and wants to continue using it in the new site. The new site will use the same hosting and domain name as the current one.

What steps do I have to take to make sure that his Google Analytics data isn't affected? I'm completely new to Google Analytics but can see that there is a snippet of code in the head referring to it. Is it just a case of copying that code into the head of the new site?

Cheers,

Ede

2 Answers

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

As far as I'm aware you just need to ensure that the tracking code file. if it exists as HTML file in the domain and Google Anayltics will continue to track the site.

Google Analytics will guide you through the different ways Analytics is tracked but I prefer to use the HTML file with the tracking code in it.

Hi Jonathan,

Thanks for your speedy answer.

I wasn't aware that there was a separate file with the tracking code in. Is it the same code that may appear in the head? The following is what I have in the head...

<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-26403987-2']);
    _gaq.push(['_trackPageview']);

    (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
      ga.src = 'http://www.google-analytics.com/ga.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();
</script>

Cheers,

Ede

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

That's right it's just a simple HTML file that goes in the root folder of your domain. Once installed analytics picks it up and immediately starts tracking data.

I'm sorry, there's a bit more to it than I said. Yes add the html file that Analytics file

Something that looks like this

google-site-verification: googled02f8577e9df138c.html

And in one site that I have, I have the following code in each page that I want to track

    <!--Google Analytics Code -->   
        <script>
              (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
              (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
              m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
              })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

              ga('create', 'UA-36732515-3', 'elimchurchbishopauckland.co.uk');
              ga('send', 'pageview');
        </script>

Hi Jonathan,

Thanks for the reply. That seems straightforward enough.

Cheers,

Ede

John Steer-Fowler
PLUS
John Steer-Fowler
Courses Plus Student 11,734 Points

If you are using Wordpress, I would just put the tracking code into the header template so that it automatically will be on all pages and any future pages.

It's a massive pain to have to keep adding tracking to individual pages

Hi John,

Thanks for that note. I had the same thought and wondered whether that was possible.

Cheers,

Ede

John Steer-Fowler
John Steer-Fowler
Courses Plus Student 11,734 Points

Hi Ede,

It's certainly possible, it's how most people handle Google Analytics tracking code + any javascript widgets really