Google analytics reading content drilldown using php. Reading Google analytics using php.
1.Introduction 2.Authentication 3.Code and Explanation 4.Download Source Code 4.Comments
Introduction
A basic code and explanation for authorizing google analytics account and reading the content drilldown of a website. The rist step in the code is logging in google analytics. First we need to connect to google analytics to get a session token using your google account username and password. This can be an main account credentials or a sub account credentials. After getting a token we need make another request to google analytics including the token in the request. Subsequent requests should pass the session token to validate your login. In this code i have used it only for one request. The result is an xml file with google analytics reports format which can be manipulated using simple dom xml or by regular expressions.Google Analytics Authentication
$post_string = "accountType=GOOGLE&Email=sam@gmail.com&Passwd=yourpassword&service=analytics&source=jach-mann-inn"; $request = curl_init("https://www.google.com/accounts/ClientLogin"); // initiate curl object curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1) curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. $post_response = curl_exec($request); // execute curl post and store results in $post_response curl_close ($request); // close curl object
preg_match("/Auth=(.*)/",$post_response,$res); $auth = $res[1];
$today = date('Y-m-d',strtotime("-1 day",time())); $past30 = date('Y-m-d',strtotime("-1 month",time())); $theurl="https://www.google.com/analytics/feeds/data?ids=ga:12380176&dimensions=ga:pagePath&metrics=ga:pageviews&sort=-ga:pageviews&start-date=$past30&end-date=$today&start-index=1&max-results=10000&v=2&prettyprint=true"; $request = curl_init($theurl); // initiate curl object curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1) curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. curl_setopt($request, CURLOPT_HTTPHEADER,array("Authorization: GoogleLogin Auth=\"$auth\"")); $pr = curl_exec($request); // execute curl post and store results in $post_response curl_close ($request); // close curl object
Code and Explanation
$doc = new DOMDocument(); $doc->loadXML($pr); $dim = $doc>getElementsByTagName('dimension'); $met = $doc>getElementsByTagName('metric'); $len = $dim>length; $dz = array(); for($i=0;$i<$len;$i++) { $key = $dim>item($i)>getAttribute('value'); $value = $met>item($i+1)>getAttribute('value'); $sam = substr($key,1,strpos($key,'/',1)); $sam = rtrim($sam,'/'); if(empty($sam))$sam='ROOT'; // if(strpos($sam,'?',1))$sam = preg_replace("/\?(.*?)$/",'',$sam); if(array_key_exists($sam,$dz)) $dz[$sam] += $value; else $dz[$sam] = $value; } array_multisort($dz,SORT_DESC,SORT_NUMERIC); echo '<pre>'; print_r($dz);
Now we have created an object from the class dom document to parse the xml response. We need the dimension and metric tags.
Consider the dimension and metrics as key and value. First i am parsing the whole xml to fetch the dimension and its corresponding metrics. Now i create a associative array. I insert each item one by one. If the dimension already exists then i will just increment its count. If it does not exists then in insert.
Consider you have pages like the following
/php/array.html /css/selector.html /php/constants.html /css/id.html /js/static.html here in the final array php => 2 css=>2 js=>1
and this is how i get the content drill down.
Just play with a little and you will understand and it is very simple.
Reference
You can see all the combinations of dimension and metricsDimensions & Metrics Reference - Google Analytics
Download Source Code
The zip file contains a php file for the google analytics content drilldown script with a sample xml
Click here to download google analytics content drilldown php code
Comments, Suggestions, Objections, ...