简体   繁体   English

Google Analytics(分析)PHP5界面始终无法显示结果

[英]Google Analytics PHP5 Interface always fails to show result

I was trying to get pageviews and visits from my Google Analytics account, so I set an example like this: https://code.google.com/p/gapi-google-analytics-php-interface/ on my localhost, but somehow it isn't returning nothing... it always fails. 我试图从我的Google Analytics(分析)帐户获取综合浏览量和访问量,所以我设置了一个示例: https : //code.google.com/p/gapi-google-analytics-php-interface/在我的本地主机上,但是以某种方式它什么也不返回……它总是失败。

This is my code(password and email omitted on purpose): 这是我的代码(故意省略密码和电子邮件):

<?php 
    require 'gapi.class.php';

    $gaEmail = 'email';
    $gaPassword = 'pass';
    $profileId = 'UA-37213064-1';

    $dimensions = array('pagePath','country', 'region', 'city'); 
    $metrics = array('visits');
    $sortMetric=null;
    $filter=null;
    $startDate='2011-02-01';
    $endDate='2015-01-30';
    $startIndex=1;
    $maxResults=10000;

    $ga = new gapi($gaEmail, $gaPassword);

    $ga->requestReportData($profileId, $dimensions, $metrics, $sortMetric, $filter,        $startDate, $endDate, $startIndex, $maxResults);

    $totalPageviews = $ga->getPageviews();

    foreach ($ga->getResults() as $result) {
       $visits = $result->getVists();
       print $visits; 
    }

?>

It always fails here: 它总是在这里失败:

$ga = new gapi($gaEmail, $gaPassword);

ERROR: 错误:

PHP Fatal error:  Uncaught exception 'Exception' with message 'GAPI: Failed to authenticate user. Error: "Error=BadAuthentication
Url=https://www.google.com/accounts/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbttRRxx9DCBil669sL7kLRdff3nkCIN2ZZVwbU8erUozRAS9AUbn2wk7Rzjcotu7d3Hb7t3ihTxae_QFryWhZfF7uDfZDAL8GfF0w8CY8IopMZs9FEsmlkMlXczOKJ3QKLtEBGtPwCrjW69cI5U7NDe_WiPTWIZhXhf3znQjRU8TMrsNB6NNDMA_5zCwCMtTBYNB-tukpyRoFd3YS2HZfh4fJyDYA
Info=WebLoginRequired
"' in /Volumes/Macintosh Work/www/playground/gapi-1.3/gapi.class.php:418
Stack trace:
#0 /Volumes/Macintosh Work/www/playground/gapi-1.3/gapi.class.php(62): gapi->authenticateUser('Email...', 'password')
#1 /Volumes/Macintosh Work/www/playground/gapi-1.3/teste.php(17): gapi->__construct('EMAIL...', 'password')
#2 {main}
  thrown in /Volumes/Macintosh Work/www/playground/gapi-1.3/gapi.class.php on line 418

I am almost 100% sure that that is not going to work anymore. 我几乎100%确信那将不再起作用。 If you check you can see that the project hasn't been developed on since 2009 it allows you to login using a Login and password. 如果您检查一下,可以看到该项目自2009年以来就没有开发过,它使您可以使用登录名和密码登录。 Client login doesn't work with the current versions of Google analytics you must use Open Authentication. 客户端登录不适用于当前版本的Google Analytics(分析),您必须使用开放式身份验证。

It depends upon what you are doing but you could look into useing the Php client library. 这取决于您在做什么,但是您可以考虑使用Php客户端库。 https://github.com/google/google-api-php-client this will allow you to select your Google Analytics Data out using the Google Analytics API. https://github.com/google/google-api-php-client,这将允许您使用Google Analytics(分析)API来选择Google Analytics(分析)数据。 You can find a tutorial here 您可以在这里找到教程

You could also consider using the Embeded API this will allow you to display fancy graphics and its JavaScript based. 您还可以考虑使用Embeded API,这将允许您显示精美的图形及其基于JavaScript的图形。

$analytics = new Google_Service_Analytics($client);

$profileId = 'UA-37213064-1';

$startDate='2011-02-01';
$endDate='2015-01-30';

$metrics = 'visits';

$optParams = array(
    'max-results' => 1000,
    'dimensions' => 'ga:pagePath,ga:country,ga:region,ga:city',         
);

$results = $analytics->data_ga->get('ga:'.$profileId, $startDate, $endDate, 'ga:'.$metrics, $optParams);

Requires - https://github.com/google/google-api-php-client 要求-https: //github.com/google/google-api-php-client

GAPI version 2.0 has been released with OAuth2 and Google Analytics API v3 support. GAPI 2.0版已发布,支持OAuth2和Google Analytics API v3。 Your code above will work but OAuth2 will require you to create a 'service account' and then download a P12 file to upload to the server. 您上面的代码可以使用,但是OAuth2将要求您创建一个“服务帐户” ,然后下载P12文件上传到服务器。 Finally you will need to adjust the developers console, enable 'analytics API'. 最后,您需要调整开发人员控制台,启用“分析API”。 Finally give this new user 'Read and Analyse' permissions on the Google Analytics accounts you want to access. 最后,向您的新用户授予您要访问的Google Analytics(分析)帐户的“读取和分析”权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM