自定义 Google 分析 API 脚本正在运行,但 运行 非常慢
Custom Google Analytics API script is working, but running extremely slow
我正在为 PHP.
使用 Google Analytics API 开展一个项目
这是我需要做的:我们在 Google Analytics 帐户中跟踪了大约 320 个客户网站。我们正在 运行 解决 Google 分析代码在某些网站上停止跟踪(出于各种原因)的问题,我们希望在我们的客户发现之前解决这个问题。
所以,我想完成的是编写一个脚本来检查 Google Analytics 中的所有客户站点,并调用报告 API 并查询上次的会话数7天。如果过去 7 天没有数据,则代码很可能没有在网站上正确跟踪。
我在 Google 中创建了一个新的 API 项目,并获得了一封客户电子邮件,我已将其添加为对我在 Google Analytics 中的每个帐户具有查看权限的用户。在按州(纽约客户、弗吉尼亚客户、俄勒冈客户等)组织的 Google 分析中,我总共有 63 个帐户,每个帐户下都有多个 sub-accounts/profiles,总共 320 个。
我有一个正在运行的脚本,但是 运行 需要很长时间。这个脚本确实有效,因为它为我提供了我的 Google 分析中在过去 7 天内没有数据的每个配置文件,但 运行 需要大约 65 秒,有时我会收到超时错误因为 运行 需要多长时间。我确定这是因为 foreach 循环对 API 进行了大量调用。我觉得如果我减少调用 API 的次数,脚本会 运行 快得多。但是,我不确定如何编写脚本以减少对 API.
的调用
有什么方法可以通过减少对报告 API 的调用来加快下面的脚本?也许我以错误的方式解决这个问题,有更简单的方法来完成我想完成的事情?
这是我目前的工作代码:
// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';
//initialize analytics
$analytics = initializeAnalytics();
//an array that I will be using below in the getProfileIds() function
$ProfileIDWithDomain = array();
//Calls a function to obtain all of the profile IDs to query against the Core reporting API
$profiles = getProfileIds($analytics);
//for each profile in the $ProfileIDWithDomain array query the reporting API to get data over
last 7 days:
foreach ($profiles as $key => $value){
//$key is profile id $value is domain
$results = getResults($analytics, $key);
//print the results
printResults($results,$key,$value);
}
function initializeAnalytics()
{
// Creates and returns the Analytics Reporting service object.
// Use the developers console and download your service account
// credentials in JSON format. Place them in this directory or
// change the key file location if necessary.
$KEY_FILE_LOCATION = __DIR__ . '/{KEY_FILE}';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("GA Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
return $analytics;
}
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions');
}
function printResults($results, $profile,$domain) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) < 1) {
echo "<div class='item'>";
print "No results found for $profile, domain name: $domain";
echo "</div>";
}
}
function getProfileIds($analytics) {
// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
//get all 63 accounts in GA
$items = $accounts->getItems();
//array to store accounts
$AccountIds = array();
foreach ($items as $item) {
//for each of the 63 accounts, store the account ID in an array
$AccountIds[] = $item->getId();
}
//now for each Account ID, we will obtain the properties
foreach ($AccountIds as $id){
// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties->listManagementWebproperties($id);
//if there are more than on item in the properties list (multuple profiles under it)
if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$i = 0;
if(count($items) > 1){
foreach($items as $item){
//for each item in the property list, get the id
$currentPropertyID = $item->getId();
//list management profiles for the id
$profiles = $analytics->management_profiles->listManagementProfiles($id, $currentPropertyID);
if (count($profiles->getItems()) > 0) {
$user = $profiles->getItems();
// Store the ID with an associated URL/domain name
$ProfileIDWithDomain[$user[0]->getId()] = str_replace($removeChar, "", $items[$i]["websiteUrl"]);
} else {
throw new Exception('No views (profiles) found for this user.');
}
$i++;
}
}
else{
//only one item in the properties list
$currentPropertyID = $items[0]->getId();
//list management profiles for the id
$profiles = $analytics->management_profiles->listManagementProfiles($id, $currentPropertyID);
if (count($profiles->getItems()) > 0) {
$user = $profiles->getItems();
// Store the ID with an associated URL/domain name
$ProfileIDWithDomain[$user[0]->getId()] = str_replace($removeChar, "", $items[0]["websiteUrl"]);
} else {
throw new Exception('No views (profiles) found for this user.');
}
}
} else {
throw new Exception('No properties found for this user.');
}
}
} else {
throw new Exception('No accounts found for this user.');
}
//return an associative array with ID => URL/Domain name for each profile
return $ProfileIDWithDomain;
}
你在那里打了很多电话。
列出所有帐户
列出每个帐户中的所有网络属性
列出每个网络媒体资源中的所有视图。
您只需调用一次 account summaries list
即可获得相同的结果
像这样。
$service->accountSummaries->ListAccountSummaries($optParams);
+1 使用帐户摘要。
您还可以通过使用较新的 Analytics Reporting API v4
来减少调用次数
reports.batchGet 方法一次可以接受 5 个请求。但是我不知道它是否比你现在使用的v3方法更快。
我正在为 PHP.
使用 Google Analytics API 开展一个项目这是我需要做的:我们在 Google Analytics 帐户中跟踪了大约 320 个客户网站。我们正在 运行 解决 Google 分析代码在某些网站上停止跟踪(出于各种原因)的问题,我们希望在我们的客户发现之前解决这个问题。
所以,我想完成的是编写一个脚本来检查 Google Analytics 中的所有客户站点,并调用报告 API 并查询上次的会话数7天。如果过去 7 天没有数据,则代码很可能没有在网站上正确跟踪。
我在 Google 中创建了一个新的 API 项目,并获得了一封客户电子邮件,我已将其添加为对我在 Google Analytics 中的每个帐户具有查看权限的用户。在按州(纽约客户、弗吉尼亚客户、俄勒冈客户等)组织的 Google 分析中,我总共有 63 个帐户,每个帐户下都有多个 sub-accounts/profiles,总共 320 个。
我有一个正在运行的脚本,但是 运行 需要很长时间。这个脚本确实有效,因为它为我提供了我的 Google 分析中在过去 7 天内没有数据的每个配置文件,但 运行 需要大约 65 秒,有时我会收到超时错误因为 运行 需要多长时间。我确定这是因为 foreach 循环对 API 进行了大量调用。我觉得如果我减少调用 API 的次数,脚本会 运行 快得多。但是,我不确定如何编写脚本以减少对 API.
的调用有什么方法可以通过减少对报告 API 的调用来加快下面的脚本?也许我以错误的方式解决这个问题,有更简单的方法来完成我想完成的事情?
这是我目前的工作代码:
// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';
//initialize analytics
$analytics = initializeAnalytics();
//an array that I will be using below in the getProfileIds() function
$ProfileIDWithDomain = array();
//Calls a function to obtain all of the profile IDs to query against the Core reporting API
$profiles = getProfileIds($analytics);
//for each profile in the $ProfileIDWithDomain array query the reporting API to get data over
last 7 days:
foreach ($profiles as $key => $value){
//$key is profile id $value is domain
$results = getResults($analytics, $key);
//print the results
printResults($results,$key,$value);
}
function initializeAnalytics()
{
// Creates and returns the Analytics Reporting service object.
// Use the developers console and download your service account
// credentials in JSON format. Place them in this directory or
// change the key file location if necessary.
$KEY_FILE_LOCATION = __DIR__ . '/{KEY_FILE}';
// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("GA Analytics Reporting");
$client->setAuthConfig($KEY_FILE_LOCATION);
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new Google_Service_Analytics($client);
return $analytics;
}
function getResults($analytics, $profileId) {
// Calls the Core Reporting API and queries for the number of sessions
// for the last seven days.
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:sessions');
}
function printResults($results, $profile,$domain) {
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) < 1) {
echo "<div class='item'>";
print "No results found for $profile, domain name: $domain";
echo "</div>";
}
}
function getProfileIds($analytics) {
// Get the list of accounts for the authorized user.
$accounts = $analytics->management_accounts->listManagementAccounts();
if (count($accounts->getItems()) > 0) {
//get all 63 accounts in GA
$items = $accounts->getItems();
//array to store accounts
$AccountIds = array();
foreach ($items as $item) {
//for each of the 63 accounts, store the account ID in an array
$AccountIds[] = $item->getId();
}
//now for each Account ID, we will obtain the properties
foreach ($AccountIds as $id){
// Get the list of properties for the authorized user.
$properties = $analytics->management_webproperties->listManagementWebproperties($id);
//if there are more than on item in the properties list (multuple profiles under it)
if (count($properties->getItems()) > 0) {
$items = $properties->getItems();
$i = 0;
if(count($items) > 1){
foreach($items as $item){
//for each item in the property list, get the id
$currentPropertyID = $item->getId();
//list management profiles for the id
$profiles = $analytics->management_profiles->listManagementProfiles($id, $currentPropertyID);
if (count($profiles->getItems()) > 0) {
$user = $profiles->getItems();
// Store the ID with an associated URL/domain name
$ProfileIDWithDomain[$user[0]->getId()] = str_replace($removeChar, "", $items[$i]["websiteUrl"]);
} else {
throw new Exception('No views (profiles) found for this user.');
}
$i++;
}
}
else{
//only one item in the properties list
$currentPropertyID = $items[0]->getId();
//list management profiles for the id
$profiles = $analytics->management_profiles->listManagementProfiles($id, $currentPropertyID);
if (count($profiles->getItems()) > 0) {
$user = $profiles->getItems();
// Store the ID with an associated URL/domain name
$ProfileIDWithDomain[$user[0]->getId()] = str_replace($removeChar, "", $items[0]["websiteUrl"]);
} else {
throw new Exception('No views (profiles) found for this user.');
}
}
} else {
throw new Exception('No properties found for this user.');
}
}
} else {
throw new Exception('No accounts found for this user.');
}
//return an associative array with ID => URL/Domain name for each profile
return $ProfileIDWithDomain;
}
你在那里打了很多电话。
列出所有帐户 列出每个帐户中的所有网络属性 列出每个网络媒体资源中的所有视图。
您只需调用一次 account summaries list
即可获得相同的结果像这样。
$service->accountSummaries->ListAccountSummaries($optParams);
+1 使用帐户摘要。
您还可以通过使用较新的 Analytics Reporting API v4
来减少调用次数reports.batchGet 方法一次可以接受 5 个请求。但是我不知道它是否比你现在使用的v3方法更快。