我正在开发一个使用的网站。我遵循了本教程-> https://developers.google.com/analytics/solutions/articles/hello-analytics-api?hl=pt-PT
一切都很好。我得到“同意屏幕”,我给予“许可”,我被重定向到我的网站,所有的信息。
但是,我想稍微改变一下。我想获得谷歌分析的所有信息,而不使用“同意屏幕”,即只使用谷歌分析代码(UA-XXXXXXXX X)或任何其他方式。
有什么帮助吗?
谢谢
发布于 2014-02-12 09:39:42
为了使用Google,您需要授权(访问数据的权限)。因为您只想看到自己的数据,我建议您查看一个服务帐户。通过在Google控制台中设置一个服务帐户,它将允许您访问自己的数据,而无需随时登录和修改代码。
下面是如何在php ServiceAccount中使用服务帐户的示例
该示例项目是为PredictionService而不是谷歌分析服务提供的。你需要稍微编辑一下。
require_once '../../src/Google/Client.php';
require_once '../../src/Google/Service/Analytics.php';
// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID';
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '/super/secret/path/to/key.p12';
$client = new Google_Client();
$client->setApplicationName("Google Analytics Sample");
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME(Email),
array('https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents(KEY_FILE))
);
$client->setClientId(CLIENT_ID);
$service = new Google_Service_Analytics($client);现在,您可以在其余的调用中使用$service。
https://stackoverflow.com/questions/21708305
复制相似问题