PHP: Google URL Shortener: analytics

Once you have created a short URL and verified that it is rightly pointing to the desired page. With analytics, you can find a pretty lot of information about various characteristics of the user like the country, browsers of the visitors.

Check the following program to get the analytics

<?php

/*Base URL of the Service*/
$BASEURL = "https://www.googleapis.com/urlshortener/";

/*Version of the service*/
$VERSION = "v1";

/*Service*/
$SERVICE = "url";

/*Type of the content, e.g. text/xml, application/json*/
$CONTENT_TYPE = "Content-Type: application/json";

/*API name*/
$REQUEST = "shortUrl";

function printDetails($hash) {
    foreach($hash as $key => $value){
        if (is_array($value)) {
            print "$key:n";
            foreach($value as $val) {
                if(is_object($val) || is_array($val)){
                     printDetails($val);
                }
                else{
                     print "$val,";
                }
            }
            print "n";
        }
        else if (!is_object($value)) {
            print "$key: $valuen";
        }
        else {
            print "$key:n";
            printDetails($value);
        }
    }
}

function get_response($URL, $type) {

    if(!function_exists('curl_init')) {
    die ("Curl PHP package not installedn");
    }

    /*Initializing CURL*/
    $curlHandle = curl_init();

    /*The URL to be downloaded is set*/
    curl_setopt($curlHandle, CURLOPT_URL, $URL);
    curl_setopt($curlHandle, CURLOPT_HEADER, false);
    curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array($type));
    curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);

    /*Now execute the CURL, download the URL specified*/
    $response = curl_exec($curlHandle);

    /*Return the response as it is, let the application process it accordingly*/
    return $response;
}

/*Specify the url to be expanded*/

if (sizeof($argv) != 2) {
  echo "Usage: $argv[0] urln";
  exit;
}

$url = "$BASEURL$VERSION/$SERVICE";

print "URL: $urln";
print "Analytics of the URL to be found: $argv[1]n";

$response = get_response($url."?$REQUEST=$argv[1]&projection=FULL", $CONTENT_TYPE);

/*Printing the response on to the console*/
if (isset($response)) {
   $dresponse = json_decode($response);
   if (json_last_error() == JSON_ERROR_NONE){
       if (isset($dresponse->{'longUrl'})) {
           print "======================================================n";
           print "More Details:n";
           print "======================================================n";
           printDetails($dresponse);
           print "======================================================n";
       }
   }
}
else {
   print "Failed to get a responsen";
   exit(1);
}
?>

Let’s execute the program with a short URL

$ php analytics.php http://goo.gl/nhfUT
URL: https://www.googleapis.com/urlshortener/v1/url
Analytics of the URL to be found: http://goo.gl/nhfUT
======================================================
More Details:
======================================================
kind: urlshortener#url
id: http://goo.gl/nhfUT
longUrl: http://example.com/
status: OK
created: 2011-01-11T17:04:27.649+00:00
analytics:
allTime:
shortUrlClicks: 1
longUrlClicks: 1
referrers:
count: 1
id: Unknown/empty

countries:
count: 1
id: ABC

browsers:
count: 1
id: Firefox

platforms:
count: 1
id: Linux

month:
shortUrlClicks: 1
longUrlClicks: 1
referrers:
count: 1
id: Unknown/empty

countries:
count: 1
id: ABC
browsers:
count: 1
id: Firefox

platforms:
count: 1
id: Linux

week:
shortUrlClicks: 1
longUrlClicks: 1
referrers:
count: 1
id: Unknown/empty

countries:
count: 1
id: XYZ

browsers:
count: 1
id: Firefox

platforms:
count: 1
id: Linux

day:
shortUrlClicks: 0
longUrlClicks: 0
twoHours:
shortUrlClicks: 0
longUrlClicks: 0
======================================================
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s