Category: Google

Google’s Analytics API with PHP

I was recently asked about my experience with Google’s Analytics API, my reply was bla, bla, bla, I do not have any experience with their API’s.  But to be honest I couldn’t remember why at that moment I didn’t. I think the only reason was because of their documentation, for some of their API’s it’s a pretty long, difficult read. But overall that wasn’t a very good reason, so I decided to take some time and dive in and whoop that trick (line from the hustle and flow movie).
The goal for this post to help anyone who may be having a hard time understanding how to build a connection to Google’s Analytics API.
So first there are a couple of things you need to know.

There are only two files needed in this script but the connection part is the only part we will be going over, it’s in the gaClass.php file.
So basically I’m still new to  Google’s Analytics API but what I’ve learned so far is that once you’ve established a connection the rest is pretty simple.  So basically that’s why we will be focusing on the establishing a connection part only.

Your first major goal while trying to establish a connection is to collect the authentication string. Within that string is your authentication token, and without that token you can’t get anywhere so remember that. The overall objective is to get the authentication token.

So open up the gaClass.php file and take a look at the construct method, this is where the connection happens. Please contact me if I confuse your somewhere, I try to code so that it’s easy to follow.

  • Within the construct method we start off by collection your email and password that was set on the form page. They of course are needed to connect to your account.
  • We set the url that we will be trying to making a connection to. In this case it’s the ClientLogin URL ( . Take note that the protocol starts with HTTPS.
  • Within our POST request we have a few additional parameters that will need to be pass along including your email and password. A short description of these parameters can be found at ( scroll down until you see the HTTP Protocol Details heading
  • Now once we have all the parameters set we will try to actually make the connection. We do this within the next method. The curlConnection method, and it does exactly what it says, tries to makes the connection via cURL
  • The curlConnection method will take the parameters we talked about earlier and send them to the ClientLogin URL via cURL using a POST method. The answer we receive from this post will or will not include our authentication token.
  • Now let’s suppose it does and the header status code equal 200, which means it’s A-OK. Within that connection response output you will see a encrypted string. But look closers you’ll see three variables with three values assigned to it. The variables are labeled , “SID”, “LSID” and  “AUTH”. Well you want the AUTH and it value.  Our next task is to parse out the AUTH variable and its value, and we do this with the setAccessToken method.
  • The setAccessToken is fairly simple we take that string and convert it to an array using PHP’s built in explode function.  The access token should be in the second element. I then assign this value to a token variable to be used later for accessing other parts of the API. If you recall when we started this post the authentication token is essential to other parts of the API. Without it you basically can’t do anything.

So that you can get a idea of what the output data look like.  I’ve included a screen shot of the header response output, the authentication output, when the authentication output is converted into a array, and the authentication token.

Googles Analytics API Header Response













I’m new to Mercurial so please don’t give me a hard time about my commits or the lack of.

Google Analytics API with PHP



Google XML Sitemap: Export Title, Description and Keywords to Excel

Google XML Sitemap: Download the Title, Description & Keywords tags from your sites webpage via your Google XML sitemap and then export the data into a excel spreadsheet. I’m not totally sure what to call this tool yet. I started out creating something else and took a totally different direction. That’s not to say this is not a useful tool. Basically if you’ve ever been in a situation and wonder how many URL’s are included in a Google XML sitemap, then this application will help you. Or if you’ve ever been in a pickle and wondered how well my Titles, Descriptions, or Keywords Tags are optimized, then this application will help yuo also. Once you submit your Google XML sitemap the application will download all of the URL’s included in your XML Sitemap and then one URL at a time goes out and fetches the Title and Meta Information. It then runs the character count from this data against what is deemed by Yahoo as being a well formatted Title and Meta Tag by YSSP standards. Title 70 characters in length, Description 170 characters in length, and 12 Keywords. I took a look at the Google Webmaster rule set also and added in some padding.
So for example the application will color the character count number Green if the Title falls in-between 60 and 70 characters. Then Red if the count is over 70 characters and Purple if it’s under 60 characters. This color theme is used also for the Description and the Keywords.

The complete thresholds includes.
Title: >=60 <=70 Good; > 70 Red; < 60 Purple
Description: >=150 <=170 Good; > 170 Red; < 150 Purple
Keywords:   >=10 <=12 Good; > 12 Red; < 10 Purple

Google Sitemap Tool
Creating and submitting Sitemaps

Google Favicon API, PHP Code

I ran into something very, very interesting today. Google Favicon API
(A Favorite Icon API)
. It’s an API that allows you to pull websites Favicons, plus the API dynamically converts the image into a PNG format. This actually comes at a great time because I was just thinking the other day that since I have the option to add offsite URL’s to my site now. Would’nt it would be supa cool if I could also included their Favicon? To beak it down if you look into my site’s right side navigation you will see the “Links” category. Well I thought it would be nice, I mean supa cool if I had a script that could automatically include the corresponding sites Favicons.  And BAM! Thanks Google Favicon API. I wrapped this APIs up with some cURL and a little PHP and now I have a working PHP function. I don’t know much about the Google Favicon API but it looks like it’s been around for about a year. So it’s a good chance that this API could suddenly up and move or disappear all togehter. Basically what I’m saying is I don’t know how long the good times will last. So in my opinion I suggest you consider the use of this script temporary until you can get the time to build a much reliable one yourself.

function getFavicon($var){
$fp = fopen ('folder/'.$filName.'.png', 'w+');
$ch = curl_init(''.$var);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);



Copyright © 2024 Joseph Tinsley

Theme by Anders NorenUp ↑