从脚本中获取访问者数量
Get the number of visitors from script
我知道如何使用 Data Studio 或 with Google Apps script 在 Javascript 中访问 Google 分析数据:
var account = Analytics.Management.Accounts.list().items[0];
var webProperties = Analytics.Management.Webproperties.list(account.id);
...
var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric,
options);
但在 PHP 中,如何从 Google Analytics 帐户/[=30= 中检索特定网站或特定页面的访问者数量] / view? 即:
输入:分析账户login/password/website代码'UA-XXXXX-Y'
输出:[19873, 17873, 13999, 21032, ..., 16321](即过去 30 天中每一天在 www.example.com
上的访问次数,作为整数列表或 JSON)
您可以在 PHP 中使用 Google Analytics API 客户端。
Google analytic api client library
您可以使用 Query Explorer 创建要检查的查询。
代码示例:
$analytics = new analytics('username', 'password');
$analytics->setProfileByName('user.name');
//set the date range for which you want stats for
$analytics->setMonth(date('n'), date('Y'));
// it could also be $analytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD'))
print_r($analytics->getVisitors());
print_r($analytics->getPageviews());
以上示例在 PHP 中使用了 Google Analytics API 客户端。它是 PHP 中发布的第一个库。六年后,这个软件已经过时了。 Google 更改了 API。
作为替代方案,您可以使用 GAPI 库。
以上是它如何工作的示例,您可以包含 gapi class 以使其正常运行。
另一种方法是,您可以使用 Google Analytics Reporting API v4 for PHP。
您可以使用 composer:
获得它
composer require google/apiclient:^2.0
我使用这个包:
https://github.com/google/google-api-php-client
您可以使用它访问来自 PHP 的所有 Google API,当然包括 Google Analytics
这是一个如何使用它的例子:
// create client object and set app name
$client = new Google_Client();
$client->setApplicationName('Your app name'); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
new Google_AssertionCredentials(
'your_analytics_email@gmail.com', // email you added to GA
[
'https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents('/your/key/file.p12') // keyfile you downloaded
]
)
);
// other settings
$client->setClientId('your-client-id'); // from API console
$client->setAccessType('offline_access'); // this may be unnecessary?
// create service and get data
$service = new Google_AnalyticsService($client);
$from_date = date("Y-m-d",strtotime("-30 days")); // A month
$to_date = date("Y-m-d");
$response = $service->data_ga->get(
"ga:profile_id", // profile id
"$from_date", // start date
"$to_date", // end date
"ga:uniquePageviews",
[
'dimensions' => 'ga:pagePath', // Dimensions you want to include, pagePath in this example
'sort' => '-ga:uniquePageviews', // Sort order, order by unique page views from high to low in this case
'filters' => 'ga:pagePath=~\/articles\/[a-zA-Z0-9\-]+', // example url filter
'max-results' => '50' // Max results
]
);
foreach ($response["rows"] as $row) {
// ...do whatever you want with the results
}
另外,这里有一个关于如何使用 Google APIs 的指南:
https://developers.google.com/api-client-library/php/start/get_started
编辑:您需要创建凭据才能访问 Analytics API。你在这里做:https://console.cloud.google.com/flows/enableapi?apiid=analyticsreporting.googleapis.com&credential=client_key。您需要先注册一个项目,然后创建凭据。共有三个选项:API key、OAuth client ID 和 Service Account Key。我不想使用 OAuth,所以我使用了服务帐户密钥。您可以尝试使用 API 键,在这种情况下,将 $client->setAssertionCredentials(...)
调用替换为 $client->setDeveloperKey(your_api_key)
。您不能直接使用用户名和密码 AFAIK。
我知道如何使用 Data Studio 或 with Google Apps script 在 Javascript 中访问 Google 分析数据:
var account = Analytics.Management.Accounts.list().items[0];
var webProperties = Analytics.Management.Webproperties.list(account.id);
...
var report = Analytics.Data.Ga.get(tableId, startDate, endDate, metric,
options);
但在 PHP 中,如何从 Google Analytics 帐户/[=30= 中检索特定网站或特定页面的访问者数量] / view? 即:
输入:分析账户login/password/website代码'UA-XXXXX-Y'
输出:[19873, 17873, 13999, 21032, ..., 16321](即过去 30 天中每一天在 www.example.com
上的访问次数,作为整数列表或 JSON)
您可以在 PHP 中使用 Google Analytics API 客户端。 Google analytic api client library
您可以使用 Query Explorer 创建要检查的查询。
代码示例:
$analytics = new analytics('username', 'password');
$analytics->setProfileByName('user.name');
//set the date range for which you want stats for
$analytics->setMonth(date('n'), date('Y'));
// it could also be $analytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD'))
print_r($analytics->getVisitors());
print_r($analytics->getPageviews());
以上示例在 PHP 中使用了 Google Analytics API 客户端。它是 PHP 中发布的第一个库。六年后,这个软件已经过时了。 Google 更改了 API。 作为替代方案,您可以使用 GAPI 库。 以上是它如何工作的示例,您可以包含 gapi class 以使其正常运行。
另一种方法是,您可以使用 Google Analytics Reporting API v4 for PHP。 您可以使用 composer:
获得它composer require google/apiclient:^2.0
我使用这个包:
https://github.com/google/google-api-php-client
您可以使用它访问来自 PHP 的所有 Google API,当然包括 Google Analytics
这是一个如何使用它的例子:
// create client object and set app name
$client = new Google_Client();
$client->setApplicationName('Your app name'); // name of your app
// set assertion credentials
$client->setAssertionCredentials(
new Google_AssertionCredentials(
'your_analytics_email@gmail.com', // email you added to GA
[
'https://www.googleapis.com/auth/analytics.readonly'),
file_get_contents('/your/key/file.p12') // keyfile you downloaded
]
)
);
// other settings
$client->setClientId('your-client-id'); // from API console
$client->setAccessType('offline_access'); // this may be unnecessary?
// create service and get data
$service = new Google_AnalyticsService($client);
$from_date = date("Y-m-d",strtotime("-30 days")); // A month
$to_date = date("Y-m-d");
$response = $service->data_ga->get(
"ga:profile_id", // profile id
"$from_date", // start date
"$to_date", // end date
"ga:uniquePageviews",
[
'dimensions' => 'ga:pagePath', // Dimensions you want to include, pagePath in this example
'sort' => '-ga:uniquePageviews', // Sort order, order by unique page views from high to low in this case
'filters' => 'ga:pagePath=~\/articles\/[a-zA-Z0-9\-]+', // example url filter
'max-results' => '50' // Max results
]
);
foreach ($response["rows"] as $row) {
// ...do whatever you want with the results
}
另外,这里有一个关于如何使用 Google APIs 的指南:
https://developers.google.com/api-client-library/php/start/get_started
编辑:您需要创建凭据才能访问 Analytics API。你在这里做:https://console.cloud.google.com/flows/enableapi?apiid=analyticsreporting.googleapis.com&credential=client_key。您需要先注册一个项目,然后创建凭据。共有三个选项:API key、OAuth client ID 和 Service Account Key。我不想使用 OAuth,所以我使用了服务帐户密钥。您可以尝试使用 API 键,在这种情况下,将 $client->setAssertionCredentials(...)
调用替换为 $client->setDeveloperKey(your_api_key)
。您不能直接使用用户名和密码 AFAIK。