Setting Up Google Analytics on localhost

Can you setup Google Analytics to work on your localhost? Yes!

I searched for the answer to this question and found many responses, several of them said it’s not possible. The solution wasn’t easy to find and I tried several things before finally getting it to work, so I thought I would post what I did to get it to work on my localhost.

First, you’ll need to to create a Google Analytics account (http://www.google.com/analytics/); and for that, you’ll need a gmail account. When you create a new Analytics account, the first thing you’ll need to supply is a url for your website. Google Analytics won’t let you put in the url for your localhost (e.g.: http://localhost:8080), you have to enter a url that Google Analytics will allow (i.e.: it must include a “.”). I used http://localhost.local (a suggestion I found on a forum somewhere).

Continue through the Google Analytics setup screens (the rest are self explanatory). Once you get to the last page of the setup screens, you’ll see some javascript that was generated for you to paste into your site. Before copying the code, select the “Multiple top-level domains” radio button (there are a couple additional lines of code in this version of the javascript). This is the javascript you’ll want to paste into the <head> or your page.

Here’s what the code looks like…

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxxx-x']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setAllowLinker', 'true']);
_gaq.push(['_trackPageview']);

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

(Note: The UA id is unique for each Analytics account. Replace the ‘UA-xxxxxxxx-x’ value with the unique UA id that is generated by Goole Analytics for your site.)

After adding this code to your page, view the source of the page and verify the javascript is there. Then be patient, it can take up to 24 hours for Google to detect your code. (It took about 20 minutes for mine)

Once Google finds your code, the icon on your Google Analytics Overview screen will change from a yellow warning icon to a green checkmark.

Setup Successful Image

After you get the green checkbox, Google Analytics will start capturing your data. I’ve noticed there is a 5-10 minute delay from the time I click through the screens on my site, to the time I see the data in the Google Analytics reports.

One other thing to note, the date range on the Google Analytics dashboard and reports defaults to end on yesterday’s date. Make sure to modify the date range to include today’s date to see your most recent activity.

Change Date Range Image

One thought on “Setting Up Google Analytics on localhost

  1. Kristoffer says:

    Hello!

    Thanks for the tutorial. I get stuck on this part though “Before copying the code, select the “Multiple top-level domains” radio button (there are a couple additional lines of code in this version of the javascript)”. I cant find the redio button you speek of when generating the JS code. All i can inf is the default JS tracking code and optional php implementation. Meaby GA has chaanged sence this tutorial was mad?

  2. Ravi Mittal says:

    Thanks Amy

  3. anu says:

    Hi,

    I was trying to track the google analytics on my local jsp page(accessing through the tomcat server).when i’m trying to give Website URL as http://localhost:8080/Appname/..jsp it’s not taking.is there any anyway to track the localhost URL.

    1. Raj says:

      You need to setup virtual host for your app.

  4. Krishna says:

    Thanks for the post. Its really helped to enable google analytics tracking for my localhost application.

  5. Akash says:

    But the google analytics doesnt take localhost url, and the copy pasting the script code is the further step.

  6. Prafful Rajvaidya says:

    Its nice to know that how to set a url for a localhost but in the next option where we have to put our site name in the begining of form what we have to put their.

  7. Joy says:

    Hi, I cannot create a google analytics account because of this error: “URL ends with an invalid top-level domain name”. The website URL that I entered is the same as yours “localhost.local”.

    1. Sim says:

      Even it did not work for me either. I think this solution is out dated.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*