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.
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.
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.
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.
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.
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.
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.