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.

Working with Google Adwords in Sandbox mode

Google Adwords is a tool to create and manage your ads while the API helps you to do those stuff programmatically. Since Adwords is a paid service, Google provides a Sandbox environment to make our hands dirty by playing with code. To have a quick knowledge about adwords have a glance at my previous post about An introduction to Google Adwords. Adwords has API support in Java, Php, DotNet, Python, Perl, Ruby. Here we are going to look into the API development in PHP. The PHP client library for Adwords is available at Google Code.

Download and unzip the package to your server. Remember this API works only on SOAP enabled servers.

Inside the aw_api folder, you can find a ‘src’ folder, which carries the core files responsible for all the service calls. in the ‘example’ folder, there are pre defined working examples to create/add/delete campaigns, ads, ad groups, etc.,. Before working on that, we need to configure the package to work on sandbox.

For that going to the folder ‘src -> Google -> Api -> Ads -> AdWords’. There you can find two files, ‘auth.ini’ and ‘settings.ini’.

open the ‘Auth.ini’ file:

update the following few lines in that file

email = "<em>youremail</em>"
password = "<em>yourpassword</em>"
userAgent = "GOOGLE"
applicationToken = "USD"
developerToken = "<em>youremail</em>"
; Uncomment to make requests against a client account.
clientId = "client_1+<em>youremail</em>"

In the above code replace ‘youremail‘ with your Google username and ‘yourpassword‘ with your Google password. Before hand you must have signed up to Google Adwords using that username.

In the settings.php file:

you can find the following lines around the 12th line of the file.


Remove the ‘;'(semi colon), before the line that assigns the adwords-sandbox to default_server for working with sandbox environment. After completely developing your App, you can move code live by applying for developer token from Google. Once the developer token is obtained for your MCC account, you can fill your actual developer token in the corresponding variable in ‘Auth.ini’ file and in the ‘clienId’ variable you can fill in the other adwords account you have to manage the ads.

In the settings.php file swap the ‘;’ between the ‘DEFAULT_SERVER’ lines. Thats it now you can explore the API functionalities by running the example files. And Adwords Sandbox doesnt have a GUI so you need to code thoroughly to see the output. Once moving to the live environment all the ads created using API functions will be reflected in the corresponding Campaign.

Google Adwords API – An Introduction

Google Adwords is a platform to spreading business through Internet provided by Google. You can create your own ads for your products. Whenever you create an ad, it gets activates only after passing the policy violation check. After this validation, your ad will start displayed in Google search page and Google Adsense enabled sites.
Account creation in Adwords is free but to create and maintain ads. You need to integrate your bank account or credit cards to your account. Since for every click on your ads, Google will be billing you. You can create any number of ads manually but if you need to have mass creation and maintenance of ads, there comes the API into use. Google provides API's(Application Programming Interface) for most of it's service and Adwords is one of them.

Google describes their Adwords API as:

"The Google AdWords API lets developers build applications that interact directly with the AdWords platform. With these applications, advertisers and third parties can more efficiently and creatively manage their large or complex AdWords accounts and campaigns."

Google API uses SOAP technology for transferring data and information between the server and client machines for RPC(Remote Procedure Calls). You can get client libraries in different languages such as Java, PHP, Ruby, Python, dotnet, Perl. In the following section I am using the PHP client library for the service calls or even if you are interested in using client libraries you can use raw SOAP calls to the specified url with the required parameters in any programming language. You can download the PHP client library from Google code.  

After unzipping the downloaded the package, you can find lots of working examples inside the 'examples' folder and all the source files will be placed under the 'src' folder. In the next post I will explain the configuring and working of the API in test environment. Remember this package will work only on the servers which is configured with SOAP functionality.