Google Analytics API – Lesson 3: Starting to Code

September 5th, 2013 by

If you’ve been with us through the entire series, welcome back!  This time we’ll get in and get our hands dirty in some code.  If you haven’t been with us for the series, start by reading the first part of the series, which discusses some background, and the second part of the series, which provides a high level overview of the steps we will be taking today.

Let’s Start at the Very Beginning…

The last installment in this series will look markedly similar to this session.  However, throughout the next few posts I intend to reference code samples and actually guide you through the step-by-step process from my perspective.  The goal is that you should be able to follow these instructions as a supplementation to Google’s already thorough instructions.

Since my final goal is to query specific metrics from Google and eventually move them to a database, I chose to complete this process in PHP.  This way, I can easily interface with a MySQL database.  PHP may not be the best language for the process that you are trying to complete.  For this reason, Google also provides several other languages.  This guide still may be useful in understanding overall how the process works, for those of you who may prefer Java or Ruby.

…A Very Good Place to Start

The first step, if you have not yet completed it, is to read the first two blog posts in this series.  This will give you the background necessary to understand the process that I am going to follow.

In the last post, I mentioned the code libraries and the Hello Analytics API tutorial.  The first step for this process will be to download the proper files.  The PHP library can be accessed here. 

The download area looks like this:

Google Client Library

Once you have reached this page, click to download the latest release file.  The file will be in a tar.gz format.  Use a file manager such as WinZip or 7Zip to extract the .tar file to your desktop or other convenient location.

Open the containing folder.  Within this folder, there will be several more directories.

It should look like this:

File Structure One

License, notice, and readme are all files necessary for legal purposes.  You will not need to access these files for this tutorial.  The other three folders are of the most interest.

Open “examples”.  Once you have done that, you will see a lengthy list of a multitude of examples.  To keep things simple, I recommend that you delete all of the folders and files except for the analytics folder and the batch text file.

When that is complete, it should look like this:

File Structure Two

Once that is complete, you will free up some more space and make the files easier to work with.

Now your files are prepared for the process.

Understanding the Files and Process

Within this analytics folder is the actual code that will run and display in your browser.  In addition to this folder, the “src” folder at the high level holds the code that runs in the background.  This code will ensure that you are authenticating properly.

In its simplest terms, the whole process works as follows:

Visit test site -> click link to authenticate -> allow application authentication via Google -> return to your original page with an auth token -> complete any more processes in your application.

Preparing for the Next Session

Now that all of the files are downloaded, you will need to prepare for the next post.  In the next few posts, we will be editing and configuring code, getting this code on to our server, and running the code.

You will need the following programs to complete the next section:

A text editor such as Notepad++ or Notepad.
A FTP client such as FileZilla.
A live webserver.  If a live webserver is out of the question, those of you who are comfortable with some development may be familiar with a locally hosted server such as WAMP, MAMP for Mac, or XAMPP.

I completed this testing on a protected subfolder of a live domain.  If you do not have a testing environment that is live to work with one of the AMP programs will suffice.  If you do have a live environment to work with, this is when you can edit or set up a new directory to work with.  Either that, or install and configure your FTP client to work with your server.

In addition to having the proper software setup, you should familiarize yourself with the files in the analytics folder, such as the Hello Analytics file or the authorization files.  Once downloading and installing the text editor, open the files and browse through them.

At this point, if you have not already done so, you will need to create a Google account.  Once that has been completed, you are now ready to begin development.  We’ll see you at the next tutorial!

Stay tuned for the next part of the series where we will begin to edit and understand the code!  Questions?  Comments? Post them below!

Leave a Reply

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

RSS
Facebook
Facebook
Google+
Google+
https://dabrianmarketing.com/blog/analytics-api-pt3">
YOUTUBE
LinkedIn