Authentication code creation for google analytics API in PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Can sombody tell me how to create Authentication code for google
analytics API.
There are so many big big documentation on google but still I am not
getting it?

Do it requires SSL Certification as well.

Please help.


Re: Authentication code creation for google analytics API in PHP

On 9/24/2010 1:34 PM, neha rathi wrote:
Quoted text here. Click to load it

Are you looking for something like this?

And you'll have to read through it to get the idea.

Erwin Moller

"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare

Re: Authentication code creation for google analytics API in PHP


I am not getting any problem in installing Analytics code in websites.
I am trying a code in which I am collecting daily visits of all
domains of my google analytics account on one page.
It requires a google analytics API. In which it requires google
authentication code. my doubt is how to get it.
Without that code I am getting all visits on local xampp server but
not on dedicated server.

It gives me error as

Fatal error: Uncaught exception 'Exception' with message 'Permission
Denied - <HTML> <HEAD> <TITLE>Token invalid</TITLE> </HEAD> <BODY
BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Token invalid</H1> <H2>Error
401</H2> </BODY> </HTML> ' in /home/gauravhm/public_html/seo/admin/
domain_add/googleanalytics.class.php:266 Stack trace: #0 /home/
googleanalytics.class.php(195): GoogleAnalytics->_postTo('https://', Array, Array) #1 /home/gauravhm/public_html/seo/admin/
domain_add/googleanalytics.class.php(116): GoogleAnalytics-
Quoted text here. Click to load it
admin/domain_add/visits.php(76): GoogleAnalytics->getReport(Array) #3
thrown in /home/gauravhm/public_html/seo/admin/domain_add/
googleanalytics.class.php on line 266

I am using the code snippet as

private function _callAPI($url)
   return $this->_postTo($url,array(),array("Authorization:
GoogleLogin auth=".$this->_authCode));

    * Authenticate the email and password with Google, and set the
$_authCode return by Google
    * @param none
    * @return none
    private function _authenticate() {
        $postdata = array(
            'accountType' => 'GOOGLE',
            'Email' => $this->_email,
            'Passwd' => $this->_passwd,
            'service' => 'analytics',
            'source' => 'askaboutphp-v01'

        $response = $this->_postTo(" /
ClientLogin", $postdata);
        //process the response;
        if ($response) {
            preg_match('/Auth=(.*)/', $response, $matches);
            if(isset($matches[1])) {
                $this->_authCode = $matches[1];
                return TRUE;
        return FALSE;

    * Performs the curl calls to the $url specified.
    * @param string $url
    * @param array $data - specify the data to be 'POST'ed to $url
    * @param array $header - specify any header information
    * @return $response from submission to $url
    private function _postTo($url, $data=array(), $header=array()) {

        //check that the url is provided
        if (!isset($url)) {
            return FALSE;

        //send the data by curl
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        if (count($data)>0) {
            //POST METHOD
            curl_setopt($ch, CURLOPT_POST, TRUE);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        } else {
            $header[] = array("application/x-www-form-urlencoded");
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

        $response = curl_exec($ch);
        $info = curl_getinfo($ch);


        //print $response;
        if($info['http_code'] == 200) {
            return $response;
        } elseif ($info['http_code'] == 400) {
            throw new Exception('Bad request - '.$response);
        } elseif ($info['http_code'] == 401) {
            throw new Exception('Permission Denied - '.$response);
        } else {
            return FALSE;


Please help.

Site Timeline