Skip to content

Commit 156b850

Browse files
authored
docs: add a sample for the Data API v1 beta
docs: add a sample for the Data API v1 beta
2 parents faeca18 + 3e9cd6c commit 156b850

File tree

11 files changed

+480
-0
lines changed

11 files changed

+480
-0
lines changed

.kokoro/secrets-example.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,6 @@ export SYMFONY_DB_PASSWORD=$CLOUDSQL_PASSWORD
140140

141141
# Functions
142142
export BLURRED_BUCKET_NAME=$GCLOUD_PROJECT-functions
143+
144+
# Google Analytics APIs
145+
export GA_TEST_PROPERTY_ID=

.kokoro/secrets.sh.enc

62 Bytes
Binary file not shown.

analyticsdata/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Google Analytics Data API Samples
2+
3+
[![Open in Cloud Shell][shell_img]][shell_link]
4+
5+
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.svg
6+
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googlecloudplatform/php-docs-samples&page=editor&working_dir=analyticsdata
7+
8+
## Description
9+
10+
These samples show how to use the [Google Analytics Data API][analyticsdata-api]
11+
from PHP.
12+
13+
[analyticsdata-api]: https://developers.google.com/analytics/devguides/reporting/data/v1
14+
15+
## Build and Run
16+
1. **Enable APIs** - [Enable the Analytics Data API](https://console.cloud.google.com/flows/enableapi?apiid=analyticsdata.googleapis.com)
17+
and create a new project or select an existing project.
18+
2. **Download The Credentials** - Configure your project using [Application Default Credentials][adc].
19+
Click "Go to credentials" after enabling the APIs. Click "Create Credentials"
20+
and select "Service Account Credentials" and download the credentials file. Then set the path to
21+
this file to the environment variable `GOOGLE_APPLICATION_CREDENTIALS`:
22+
```sh
23+
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials.json
24+
```
25+
3. **Clone the repo** and cd into this directory
26+
```sh
27+
$ git clone https://github.com/GoogleCloudPlatform/php-docs-samples
28+
$ cd php-docs-samples/analyticsdata
29+
```
30+
4. **Install dependencies** via [Composer](http://getcomposer.org/doc/00-intro.md).
31+
Run `php composer.phar install` (if composer is installed locally) or `composer install`
32+
(if composer is installed globally).
33+
5. **Replace `$property_id` variable** if present in the snippet with the
34+
value of the Google Analytics 4 property id you want to access.
35+
6. **Run** with the command `php SNIPPET_NAME.php`. For example:
36+
```sh
37+
$ php quickstart.php
38+
```
39+
40+
## Contributing changes
41+
42+
* See [CONTRIBUTING.md](../CONTRIBUTING.md)
43+
44+
## Licensing
45+
46+
* See [LICENSE](../LICENSE)
47+
48+
[adc]: https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually

analyticsdata/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"google/analytics-data": "^0.4.0"
4+
}
5+
}

analyticsdata/quickstart.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/* Google Analytics Data API sample quickstart application.
19+
20+
This application demonstrates the usage of the Analytics Data API using
21+
service account credentials.
22+
23+
Before you start the application, please review the comments starting with
24+
"TODO(developer)" and update the code to use the correct values.
25+
26+
Usage:
27+
composer update
28+
php quickstart.php
29+
*/
30+
31+
// [START analytics_data_quickstart]
32+
require 'vendor/autoload.php';
33+
34+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
35+
use Google\Analytics\Data\V1beta\DateRange;
36+
use Google\Analytics\Data\V1beta\Dimension;
37+
use Google\Analytics\Data\V1beta\Metric;
38+
39+
/**
40+
* TODO(developer): Replace this variable with your Google Analytics 4
41+
* property ID before running the sample.
42+
*/
43+
$property_id = 'YOUR-GA4-PROPERTY-ID';
44+
45+
// [START analyticsdata_initialize]
46+
// Using a default constructor instructs the client to use the credentials
47+
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
48+
$client = new BetaAnalyticsDataClient();
49+
// [END analyticsdata_initialize]
50+
51+
// [START analyticsdata_run_report]
52+
// Make an API call.
53+
$response = $client->runReport([
54+
'property' => 'properties/' . $property_id,
55+
'dateRanges' => [
56+
new DateRange([
57+
'start_date' => '2020-03-31',
58+
'end_date' => 'today',
59+
]),
60+
],
61+
'dimensions' => [new Dimension(
62+
[
63+
'name' => 'city',
64+
]
65+
),
66+
],
67+
'metrics' => [new Metric(
68+
[
69+
'name' => 'activeUsers',
70+
]
71+
)
72+
]
73+
]);
74+
// [END analyticsdata_run_report]
75+
76+
// [START analyticsdata_run_report_response]
77+
// Print results of an API call.
78+
print 'Report result: ' . PHP_EOL;
79+
80+
foreach ($response->getRows() as $row) {
81+
print $row->getDimensionValues()[0]->getValue()
82+
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
83+
// [END analyticsdata_run_report_response]
84+
}
85+
// [END analytics_data_quickstart]
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
/**
3+
* Copyright 2021 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/* Google Analytics Data API sample quickstart application.
19+
20+
This application demonstrates the usage of the Analytics Data API using
21+
service account credentials from a JSON file downloaded from
22+
the Google Cloud Console.
23+
24+
Before you start the application, please review the comments starting with
25+
"TODO(developer)" and update the code to use the correct values.
26+
27+
Usage:
28+
composer update
29+
php quickstart_json_credentials.php
30+
*/
31+
32+
// [START analytics_data_quickstart]
33+
require 'vendor/autoload.php';
34+
35+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
36+
use Google\Analytics\Data\V1beta\DateRange;
37+
use Google\Analytics\Data\V1beta\Dimension;
38+
use Google\Analytics\Data\V1beta\Metric;
39+
40+
/**
41+
* TODO(developer): Replace this variable with your Google Analytics 4
42+
* property ID before running the sample.
43+
*/
44+
$property_id = 'YOUR-GA4-PROPERTY-ID';
45+
46+
47+
// [START analyticsdata_json_credentials_initialize]
48+
/* TODO(developer): Replace this variable with a valid path to the
49+
* credentials.json file for your service account downloaded from the
50+
* Cloud Console.
51+
*/
52+
$credentials_json_path = '/path/to/credentials.json';
53+
54+
// Explicitly use service account credentials by specifying
55+
// the private key file.
56+
$client = new BetaAnalyticsDataClient(['credentials' =>
57+
$credentials_json_path]);
58+
// [END analyticsdata_json_credentials_initialize]
59+
60+
// [START analyticsdata_json_credentials_run_report]
61+
// Make an API call.
62+
$response = $client->runReport([
63+
'property' => 'properties/' . $property_id,
64+
'dateRanges' => [
65+
new DateRange([
66+
'start_date' => '2020-03-31',
67+
'end_date' => 'today',
68+
]),
69+
],
70+
'dimensions' => [new Dimension(
71+
[
72+
'name' => 'city',
73+
]
74+
),
75+
],
76+
'metrics' => [new Metric(
77+
[
78+
'name' => 'activeUsers',
79+
]
80+
)
81+
]
82+
]);
83+
// [END analyticsdata_json_credentials_run_report]
84+
85+
// [START analyticsdata_json_credentials_run_report_response]
86+
// Print results of an API call.
87+
print 'Report result: ' . PHP_EOL;
88+
89+
foreach ($response->getRows() as $row) {
90+
print $row->getDimensionValues()[0]->getValue()
91+
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
92+
// [END analyticsdata_json_credentials_run_report_response]
93+
}
94+
95+
// [END analytics_data_quickstart]
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
This application demonstrates the usage of the Analytics Data API using
2+
OAuth2 credentials.
3+
4+
Please familiarize yourself with the OAuth2 flow guide at
5+
https://developers.google.com/identity/protocols/oauth2
6+
7+
For more information on authenticating as an end user, see
8+
https://cloud.google.com/docs/authentication/end-user
9+
10+
In a nutshell, you need to:
11+
12+
1. Create your OAuth2 client credentials in Google Cloud Console.
13+
Choose "Web application" when asked for an application type.
14+
https://support.google.com/cloud/answer/6158849
15+
16+
2. When configuring the web application credentials, add
17+
"http://localhost:3000/" to "Authorized redirect URIs".
18+
19+
3. Download a credentials file using "Download JSON" button in the credentials
20+
configuration dialog and save it as `oauth2.keys.json` in the same
21+
directory with this sample app.
22+
23+
4. Replace `$property_id` variable with the value of the Google Analytics 4
24+
property id you want to access.
25+
26+
5. Install the PHP bcmath extension (due to https://github.com/protocolbuffers/protobuf/issues/4465):
27+
28+
```
29+
sudo -s apt-get install php-bcmath
30+
```
31+
32+
6. Run the following commands from the current directory in order to install
33+
dependencies and run the sample app:
34+
35+
```
36+
composer update
37+
php -S localhost:3000 -t .
38+
```
39+
40+
7. In a browser, open the following url to start the sample:
41+
42+
http://localhost:3000/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"require": {
3+
"google/analytics-data": "^0.4.0",
4+
"ext-bcmath": "*"
5+
}
6+
}

0 commit comments

Comments
 (0)