Skip to content

Commit f138480

Browse files
committed
update to the latest version of the Data API v1
1 parent 40620be commit f138480

17 files changed

+225
-69
lines changed

analyticsdata/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"require": {
3-
"google/analytics-data": "^0.1.0"
3+
"google/analytics-data": "^0.4.0"
44
}
55
}

analyticsdata/quickstart.php

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,58 @@
11
<?php
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.
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+
*/
1517

16-
$property_id = 'YOUR-GA4-PROPERTY-ID';
17-
// [START analytics_data_quickstart]
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.
1825
26+
Usage:
27+
composer update
28+
php quickstart.php
29+
*/
30+
31+
// [START analytics_data_quickstart]
1932
require 'vendor/autoload.php';
2033

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;
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\Entity;
38+
use Google\Analytics\Data\V1beta\Metric;
2639

2740
/**
28-
* TODO(developer): Uncomment this variable and replace with your GA4
41+
* TODO(developer): Replace this variable with your Google Analytics 4
2942
* property ID before running the sample.
3043
*/
31-
// $property_id = 'YOUR-GA4-PROPERTY-ID';
44+
$property_id = 'YOUR-GA4-PROPERTY-ID';
3245

46+
// [START google_analytics_data_initialize]
47+
// Using a default constructor instructs the client to use the credentials
48+
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
49+
$client = new BetaAnalyticsDataClient();
50+
// [END google_analytics_data_initialize]
3351

52+
// [START google_analytics_data_run_report]
3453
// Make an API call.
35-
$client = new AlphaAnalyticsDataClient();
3654
$response = $client->runReport([
37-
'entity' => new Entity([
38-
'property_id' => $property_id
39-
]),
55+
'property' => 'properties/' . $property_id,
4056
'dateRanges' => [
4157
new DateRange([
4258
'start_date' => '2020-03-31',
@@ -54,14 +70,15 @@
5470
])
5571
]
5672
]);
73+
// [END google_analytics_data_run_report]
5774

75+
// [START google_analytics_data_run_report_response]
5876
// Print results of an API call.
5977
print 'Report result: ' . PHP_EOL;
6078

6179
foreach ($response->getRows() as $row) {
6280
print $row->getDimensionValues()[0]->getValue()
6381
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
82+
// [END google_analytics_data_run_report_response]
6483
}
65-
66-
6784
// [END analytics_data_quickstart]
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
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\Entity;
39+
use Google\Analytics\Data\V1beta\Metric;
40+
41+
/**
42+
* TODO(developer): Replace this variable with your Google Analytics 4
43+
* property ID before running the sample.
44+
*/
45+
$property_id = 'YOUR-GA4-PROPERTY-ID';
46+
47+
48+
// [START google_analytics_data_initialize]
49+
/* TODO(developer): Replace this variable with a valid path to the
50+
* credentials.json file for your service account downloaded from the
51+
* Cloud Console.
52+
*/
53+
$credentials_json_path = '/path/to/credentials.json';
54+
55+
// Explicitly use service account credentials by specifying
56+
// the private key file.
57+
$client = new BetaAnalyticsDataClient(['credentials' =>
58+
$credentials_json_path]);
59+
// [END google_analytics_data_initialize]
60+
61+
// [START google_analytics_data_run_report]
62+
// Make an API call.
63+
$response = $client->runReport([
64+
'property' => 'properties/' . $property_id,
65+
'dateRanges' => [
66+
new DateRange([
67+
'start_date' => '2020-03-31',
68+
'end_date' => 'today',
69+
]),
70+
],
71+
'dimensions' => [new Dimension(
72+
[
73+
'name' => 'city',
74+
]),
75+
],
76+
'metrics' => [new Metric(
77+
[
78+
'name' => 'activeUsers',
79+
])
80+
]
81+
]);
82+
// [END google_analytics_data_run_report]
83+
84+
// [START google_analytics_data_run_report_response]
85+
// Print results of an API call.
86+
print 'Report result: ' . PHP_EOL;
87+
88+
foreach ($response->getRows() as $row) {
89+
print $row->getDimensionValues()[0]->getValue()
90+
. ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
91+
// [END google_analytics_data_run_report_response]
92+
}
93+
94+
// [END analytics_data_quickstart]

analyticsdata/quickstart_oauth2/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ value of the GA4 property id you want to access.
2525

2626
5. Install the PHP bcmath extension (due to https://github.com/protocolbuffers/protobuf/issues/4465):
2727

28-
```
29-
sudo -s apt-get install php-bcmath
30-
```
28+
```
29+
sudo -s apt-get install php-bcmath
30+
```
3131
3232
6. Run the following commands from the current directory in order to install
3333
dependencies and run the sample app:
3434
35-
```
36-
composer install
37-
php -S localhost:3000 -t .
38-
```
35+
```
36+
composer update
37+
php -S localhost:3000 -t .
38+
```
39+
3940
7. In a browser, open the following url to start the sample:
4041
4142
http://localhost:3000/
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"require": {
3-
"google/analytics-data": "^0.1.0",
4-
"google/auth": "^1.14",
3+
"google/analytics-data": "^0.4.0",
54
"ext-bcmath": "*"
65
}
76
}

analyticsdata/quickstart_oauth2/index.php

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
<?php
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.
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+
*/
1517

16-
$property_id = 'YOUR-GA4-PROPERTY-ID';
1718
// [START analytics_data_quickstart_oauth2]
18-
1919
require 'vendor/autoload.php';
2020

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;
21+
use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
22+
use Google\Analytics\Data\V1beta\DateRange;
23+
use Google\Analytics\Data\V1beta\Dimension;
24+
use Google\Analytics\Data\V1beta\Entity;
25+
use Google\Analytics\Data\V1beta\Metric;
2626
use Google\ApiCore\ApiException;
2727
use Google\Auth\OAuth2;
2828

2929
/**
30-
* TODO(developer): Uncomment this variable and replace with your GA4
30+
* TODO(developer): Replace this variable with your Google Analytics 4
3131
* property ID before running the sample.
3232
*/
33-
// $property_id = 'YOUR-GA4-PROPERTY-ID';
33+
$property_id = 'YOUR-GA4-PROPERTY-ID';
3434

3535
// Start a session to persist credentials.
3636
session_start();
@@ -47,19 +47,18 @@
4747
'redirectUri' => 'http://' . $_SERVER['HTTP_HOST'] . '/',
4848
]);
4949

50-
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
50+
if (isset($_SESSION['access_token']) && $_SESSION['access_token']
51+
&& isset($_SESSION['refresh_token']) && $_SESSION['refresh_token']) {
5152
// This is the final step of the OAuth2 authorization process, where an
5253
// OAuth2 access token is available and can be used to set up a client.
5354
$oauth->setAccessToken($_SESSION['access_token']);
5455
$oauth->setRefreshToken($_SESSION['refresh_token']);
5556

56-
// Make an API call.
57-
$client = new AlphaAnalyticsDataClient(['credentials' => $oauth]);
5857
try {
58+
// Make an API call.
59+
$client = new BetaAnalyticsDataClient(['credentials' => $oauth]);
5960
$response = $client->runReport([
60-
'entity' => new Entity([
61-
'property_id' => $property_id
62-
]),
61+
'property' => 'properties/' . $property_id,
6362
'dateRanges' => [
6463
new DateRange([
6564
'start_date' => '2020-03-31',
@@ -89,7 +88,6 @@
8988
// Print an error message.
9089
print $e->getMessage();
9190
}
92-
9391
} elseif (isset($_GET['code']) && $_GET['code']) {
9492
// If an OAuth2 authorization code is present in the URL, exchange it for
9593
// an access token.
@@ -110,5 +108,4 @@
110108
$auth_url = $oauth->buildFullAuthorizationUri();
111109
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
112110
}
113-
114111
// [END analytics_data_quickstart_oauth2]

analyticsdata/src/cohorts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

analyticsdata/src/expressions.php

Whitespace-only changes.

analyticsdata/src/filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

analyticsdata/src/metadata.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

0 commit comments

Comments
 (0)