Can you test google analytics on a localhost address?

ID : 10070

viewed : 56

Tags : google-analyticslocalhostgoogle-analytics

Top 5 Answer for Can you test google analytics on a localhost address?

vote vote

90

This question remains valid today, however the technology has changed. The old Urchin tracker is deprecated and obsolete. The new asynchronous Google Analytics tracking code uses slightly different code to achieve the same results.

Google Analytics Classic - Asynchronous Syntax - ga.js

The current syntax for setting the tracking domain to none on google analytics looks like this:

_gaq.push(['_setDomainName', 'none']); 

Google analytics will then fire off the _utm.gif tracker request on localhost. You can verify this by opening the developer tools in your favorite browser and watching the network requests during page load. If it is working you will see a request for _utm.gif in the network requests list.

Updated 2013 for Universal Analytics - analytics.js

Google released a new version of analytics called "Universal Analytics" (late 2012 or early 2013). As I write, this the program is still in BETA so the above code is still recommended for most users with existing installations of Google Analytics.

However, for new developments using the new analytics.js code, the Google Analytics, Advanced Configuration - Web Tracking Documentation shows that we can test Universal Analytics on localhost with this new code:

ga('create', 'UA-XXXX-Y', {   'cookieDomain': 'none' }); 

Check out the linked documentation for more details on advanced configuration of Universal Analytics.

Update 2019

Both Global Site Tag - gtag.js and Universal Analytics - analytics.js will detect localhost automatically. You do not need to make any change to the configuration.

If gtag.js detects that you're running a server locally (e.g. localhost), it automatically sets the cookie_domain to 'none'.

- developers.google.com

vote vote

86

Updated for 2014

This can now be achieved by simply setting the domain to none.

ga('create', 'UA-XXXX-Y', 'none'); 

See: https://developers.google.com/analytics/devguides/collection/analyticsjs/domains#localhost

vote vote

71

I had the same problem, and all the solutions didn't work until I did two things:

Obvious code:

var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXXXXXX-X']); _gaq.push(['_setDomainName', 'none']); _gaq.push(['_setAllowLinker', true]);    _gaq.push(['_trackPageview']); 

AND

I added localhost another FQDN - domain name. I did this on Windows sistem by editing:

C:\Windows\System32\drivers\etc\hosts 

file, and I put in the following:

127.0.0.1   my.domain.org 

Then I went to address http://my.domain.org/WebApp that is serving page with included google analytics JS.

If you are on unix, edit /etc/hosts for same result.

It think that Google should put Intranet configuration in ther GA FAQ. They just say that you need FQDA. Yes, you do, but not for them to access you, you need it just to have Host attribute in HTTP request.

I think another reason for FQDN is COOKIES! Cookies are used to track data and if you don't have FQDN, cookie can not be set, and JS code stops and doesn't get the gif.

vote vote

69

Answer for 2019

The best practice is to setup two separate properties for your development/staging, and your production servers. You do not want to pollute your Analytics data with test, and setting up filters is not pleasant if you are forced to do that.

That being said, Google Analytics now has real time tracking, and if you want to track Campaigns or Transactions, the lag is around 1 minute until the data is shown on the page, as long as you select the current day.

For example, you create Site and Site Test, and each one ha UA-XXXX-Y code.

In your application logic, where you serve the analytics JavaScript, check your environment and for production use your Site UA-XXXX-Y, and for staging/development use the Site Test one.

You can have this setup until you learn the ins and outs of GA, and then remove it, or keep it if you need to make constant changes (which you will test on development/staging first).

Source: personal experience, various articles.

vote vote

50

Now the answer for your question is yes, it will just work by copying the standard snippet. According to documentation, now the standard snippet has automatic cookie domain configuration: ga('create', 'UA-XXXXX-Y', 'auto'); where cookie domain is automatically determined.

In addition, if analytics.js detects that you're running a server locally (e.g. localhost) it automatically sets the cookieDomain to 'none'.

Top 3 video Explaining Can you test google analytics on a localhost address?

Related QUESTION?