dongshuao2309 2014-05-26 08:11
浏览 72
已采纳

如何使用谷歌分析api通过PHP客户端拉网络统计

How do I retrieve Google Analytics data through the Google Analytics API using PHP?

Is it possible to get a page wise status through API?

I am working with a website having 30K pages and I need to create a dashboard showing page wise statistics for corresponding user.

  • 写回答

1条回答 默认 最新

  • duan4739 2014-05-26 08:48
    关注

    Yes it is possible to get the stats you are talking about though the Google Analytics API using PHP.

    There is a client library for php that I recommend it can be found on GitHub

    Because you will only be accessing your own data I recommend you go with a service account for authentication.

    Simple example:

    <?php
    session_start();
    require_once 'Google/Client.php';
    require_once 'Google/Service/Analytics.php';
    
    /************************************************
      The following 3 values an befound in the setting
      for the application you created on  Google 
      Developers console.
      The Key file should be placed in a location
      that is not accessable from the web. outside of 
      web root.
    
      In order to access your GA account you must
      Add the Email address as a user at the 
      ACCOUNT Level in the GA admin. 
     ************************************************/
    $client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
    $Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';
    $key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';
    
    $client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    
    $key = file_get_contents($key_file_location);
    
    // seproate additional scopes with a comma
    $scopes ="https://www.googleapis.com/auth/analytics.readonly";  
    
    $cred = new Google_Auth_AssertionCredentials(
        $Email_address,
        array($scopes),
        $key
        );
    
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }
    
    $service = new Google_Service_Analytics($client);  
    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
    
    //calulating start date
    $date = new DateTime(date("Y-m-d"));
    $date->sub(new DateInterval('P10D'));
    
    //Adding Dimensions
    $params = array('dimensions' => 'ga:userType');
    // requesting the data
    $data = $service->data_ga->get("ga:78110423", $date->format('Y-m-d'),  date("Y-m-d"), "ga:users,ga:sessions", $params );
    
    
    ?><html>
    <?php echo $date->format('Y-m-d') . " - ".date("Y-m-d"). "
    ";?>
    <table>
    <tr>
    <?php
    //Printing column headers
    foreach($data->getColumnHeaders() as $header){  
        print "<td>".$header['name']."</td>";   
    }
    ?>
    </tr>
    <?php
    //printing each row.
    foreach ($data->getRows() as $row) {    
        print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";  
    }
    
    //printing the total number of rows
    ?>
    <tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr>
    </table>
    </html>
    <?php
    
    ?>
    

    I you can find a tutorial for that code at Google Service account php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法