Skip to content

Commit ec70b26

Browse files
committed
docs: add a quickstart sample using a service account
1 parent a0de1a4 commit ec70b26

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

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. **Uncomment `$property_id` variable** if present in the snippet and specify
34+
the value of the GA4 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.1.0"
4+
}
5+
}

analyticsdata/quickstart.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
2+
// Copyright 2021 Google LLC
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
$property_id = 'YOUR-GA4-PROPERTY-ID';
17+
// [START analytics_data_quickstart]
18+
19+
require 'vendor/autoload.php';
20+
21+
use Google\Analytics\Data\V1alpha\AlphaAnalyticsDataClient;
22+
use Google\Analytics\Data\V1alpha\DateRange;
23+
use Google\Analytics\Data\V1alpha\Dimension;
24+
use Google\Analytics\Data\V1alpha\Entity;
25+
use Google\Analytics\Data\V1alpha\Metric;
26+
27+
/**
28+
* TODO(developer): Uncomment this variable and replace with your GA4
29+
* property ID before running the sample.
30+
*/
31+
// $property_id = 'YOUR-GA4-PROPERTY-ID';
32+
33+
34+
// Make an API call.
35+
$client = new AlphaAnalyticsDataClient();
36+
$response = $client->runReport([
37+
'entity' => new Entity([
38+
'property_id' => $property_id
39+
]),
40+
'dateRanges' => [
41+
new DateRange([
42+
'start_date' => '2020-03-31',
43+
'end_date' => 'today',
44+
]),
45+
],
46+
'dimensions' => [new Dimension(
47+
[
48+
'name' => 'city',
49+
]),
50+
],
51+
'metrics' => [new Metric(
52+
[
53+
'name' => 'activeUsers',
54+
])
55+
]
56+
]);
57+
58+
// Print results of an API call.
59+
print 'Report result: ' . PHP_EOL;
60+
61+
foreach ($response->getRows() as $row) {
62+
print $row->getDimensionValues()[0]->getValue()
63+
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
64+
}
65+
66+
67+
// [END analytics_data_quickstart]

0 commit comments

Comments
 (0)