如何在cron作业中访问Google Analytics(分析)数据而无需登录? [英] How can access google analytics data in cron job without login?

查看:47
本文介绍了如何在cron作业中访问Google Analytics(分析)数据而无需登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想访问Google Analytics(分析)页面视图,并使用cron作业将其存储在数据库中.我如何无需登录即可访问Google Analytics(分析).

I want to access google analytics page views and store them in database using a cron job. How can i access google analytics without login.

基本上,我正在cron作业中访问google帐户.所以我不知道如何离线访问它.

Besically i am accessing google account in cron job. so i have no idea how to access it google apps offline.

请在这里帮助我.

谢谢.

推荐答案

服务帐户将允许您的应用访问您的Google Analytics(分析)数据,而不会提示用户进行访问.

A service account will allow your application to access your Google Analytics data without prompting a user for access.

授予访问权限

服务帐户不需要提示用户访问权限,因为您必须对其进行设置.转到您要从中检索数据的帐户的管理"部分中的Google Analytics(分析)网站.这一点非常重要,必须在帐户级别将此电子邮件地址添加为新用户.只需授予他们读取权限即可.

A service account doesn’t need to prompt a user for access because you have to set it up. Go to the Google Analytics website in the Admin section for the Account you want to retrieve data from. This is very important it must be at the account level add this email address as a new user. just give them read access.

代码

<?php
   require_once 'Google/autoload.php';
   session_start();     
/************************************************   
 The following 3 values an befound in the setting   
 for the application you created on Google      
 Developers console.         Developers console.
 The Key file should be placed in a location     
 that is not accessable from the web. outside of 
 web root.       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 = '[Your client id]';
    $Email_address = '[YOur Service account email address Address]';     
    $key_file_location = '[Locatkon of key file]';      

    $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);



    //Adding Dimensions
    $params = array('dimensions' => 'ga:userType'); 
    // requesting the data  
    $data = $service->data_ga->get("ga:89798036", "2014-12-14", "2014-12-14", "ga:users,ga:sessions", $params );     
?>

<html>   
Results for date:  2014-12-14<br>
    <table border="1">   
        <tr>     
        <?php    
        //Printing column headers
        foreach($data->getColumnHeaders() as $header){
             print "<td><b>".$header['name']."</b></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>";   
        }    
?>      
<tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr>     
</table>     
</html>  

Google Analytics(分析)服务帐户教程

这篇关于如何在cron作业中访问Google Analytics(分析)数据而无需登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆