Google Analytics API – Lesson 2: Resources

July 17th, 2013 by

Google Analytics API – Lesson 2: Resources

The second part of this series will outline exactly where to begin when working with the Google Analytics API. Google provides all of their code and a lot of information, which can be quite overwhelming. Throughout this post, I will break down the necessary resources and next steps to begin programming with the API. If you haven’t checked it out yet, head over to the first part of the series for some background before continuing.

Where do I start?

Fortunately for those of us who are new to the idea of programming with an API, Google provides a Hello Analytics API tutorial. This tutorial allows you to select from four languages: Python, Java, JavaScript, and PHP. If none of these languages work for you, you’re welcome to follow the tutorial and use one of the beta languages available.

This tutorial will explain how to download and install the client libraries, use the API console to create OAuth 2.0 tokens, and give code samples. Full code samples are available in their respective Google code libraries and code snippets are available in various tutorials to get you started. These code snippets provide information about exactly what each piece of code is doing and explains how it works. In time, when working with the code, you will be able to understand and edit it appropriately to create new and interesting applications based on the API.

In addition to the analytics tutorials, it may be beneficial to set up a development environment. This information can be accessed by doing a simple Google search for the language that you prefer. In addition, it will allow you to better read and synthesize code.

What’s next?

After reviewing the documentation and setting up with the Hello Analytics API tutorial, it may be useful to download some code samples and begin to review them. If you choose to do so, you can also run these programs or upload them to a server depending on which language you have decided to use.

PHP Code

One important item to note is that you must edit your files to include the correct OAuth 2.0 information, which you receive from following the Hello Analytics API tutorial. Items such as the client ID and URI will need to be added in the configuration file that you have downloaded. Most of the code libraries provide information or documentation on where to make these changes. If the information is not in the code library, it will be provided in the comments section accompanying the file.

Sample Application


What should happen?

When your libraries are downloaded and you run the files, the first key item that should occur is for the OAuth 2.0 to activate. This will either launch your browser or launch another window in your browser with a Google popup asking if the application is permitted to use your account information. You should then allow access.


If this occurs successfully, you may then use the other code snippets to query data from the Google Analytics servers.
Please keep in mind that the data from these code samples will not be pretty. It may use some basic formatting, but it will most likely output in a very simple format. Further styling and programming may be necessary to have the data appear in a certain way.

What if it breaks!?

The code breaking is always a very real possibility. I am hard pressed to think of a situation in which my code actually worked perfectly on the first try. In one instance I used PHP as my desired language. After uploading the code, I was able to authorize the attachment of the application to my Google account. However, once the authorization would finish, I would immediately receive a server error.

500 Server Error

When something like this occurs, usually you receive an error stating which line the error is occurring on. This all depends on the language that is being used. For PHP, it presents the line error right in the browser.

If you run into errors and are not able to resolve them by reviewing the code, there are many resources that are available to use. The first option is to complete a search with the information or the error in it. This often yields results from a number of other people running in to the same problem. If you are unable to find your problem by conducting a search, there are other options. Stack Overflow, Google Developers Forum, and some subReddits such as /r/learnprogramming are all valid resources where other developers are available to answer questions.

Have more questions about starting with the Analytics API? Tell us in the comments, and stay tuned for the next part of the series!

One Response to “Google Analytics API – Lesson 2: Resources”

  1. December 30, 2014 at 4:58 am, rochie de mireasa said:

    Hi there, simply changed into alert to your blog through Google, and located that it’s really informative. I am gonna be careful for brussels. I will appreciate in the event you proceed this in future. Lots of folks will probably be benefited from your writing. Cheers!|I’m curious to find out what blog platform you’re working with? I’m having some minor security problems with my latest site and I’d like to find something more risk-free. Do you have any recommendations?|I’m extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, it is rare to see a great blog like this one today.|I am really inspired together with your writing abilities and also with the format on your blog. Is that this a paid topic or did you customize it your self? Either way stay up the excellent high quality writing, it’s rare to peer a nice blog like this one today..|Hello, Neat post. There’s an issue with your web site in web explorer, would check this? IE nonetheless is the marketplace chief and a huge component to other people will leave out your fantastic writing due to this problem.|I am not sure where you’re getting your information, but great topic. I needs to spend some time learning much more or understanding more. Thanks for wonderful info I was looking for this information for my mission.|Hi, i think that i saw you visited my weblog so i came to


Leave a Reply

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