Use Google analytics from different domain or localhost

Google has updated their Analytics API code so that “ga.js” script will become deprecated very soon. Google recommends the new users to use the “analystic.js” version for future use. Even though the new version is still in beta, it works just fine.

While using the new version of Analytics script, I had to test the code from localhost and from different domains other than the one which I provided while creating the account. Since the domain provided in the code and one in the account information are different the visits weren’t got tracked.

After Googling for some time, I came to notice the domain name that has being provided in the code. So the Analytics API accepts the tracking information received from the specified domain. To override that functionality and send data from other domain or from localhost, extra parameter needs to be added.

Here is the general script provided by Google.

ga(‘create’, ‘UA-xxxxxx-1′, ‘’);

Change the above code like the below one to make it work from other domain names.

ga(‘create’, ‘UA-xxxxxx-1′, {‘cookieDomain': ‘none’});

There are also other kind of customization available like new page view can be identified. Button click, drop down change or anchor click events can be captured and those elements can be tracked by sending the customized parameters. To know more about these functions please visit The Introduction to Analytics.js.