Skip to content

Commit c80ab46

Browse files
committedNov 2, 2023
Update README with basics and add Data API quickstart
1 parent de43436 commit c80ab46

File tree

3 files changed

+130
-1
lines changed

3 files changed

+130
-1
lines changed
 

‎README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
# nodejs-docs-samples
1+
# Google Analytics Node.js Samples
2+
3+
Node.js samples for [Google Analytics APIs][ga].
4+
5+
Check out the `README.md` in one of the following directories to get started:
6+
7+
- Admin API: [README.md](google-analytics-admin/README.md)
8+
- Data API: [README.md](google-analytics-data/README.md)
9+
10+
## Contributing
11+
12+
Contributions welcome! See the [Contributing Guide](CONTRIBUTING.md).
13+
14+
[ga]: https://developers.google.com/analytics
215

‎google-analytics-data/package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "nodejs-analytics-data",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"author": "Google LLC",
6+
"engines": {
7+
"node": ">=14.0.0"
8+
},
9+
"files": [
10+
"*.js"
11+
],
12+
"scripts": {
13+
"test": "c8 mocha --timeout 600000 test/*.js"
14+
},
15+
"dependencies": {
16+
"@google-analytics/data": "^4.0.1",
17+
"google-auth-library": "^9.0.0",
18+
"google-gax": "^3.0.0",
19+
"http": "^0.0.1-security",
20+
"lint": "^0.8.0",
21+
"open": "^8.0.0",
22+
"server-destroy": "^1.0.1",
23+
"url": "^0.11.0"
24+
},
25+
"devDependencies": {
26+
"c8": "^7.1.0",
27+
"chai": "^4.2.0",
28+
"mocha": "^8.0.0"
29+
}
30+
}

‎google-analytics-data/quickstart.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/** Google Analytics Data API sample quickstart application.
18+
This application demonstrates the usage of the Analytics Data API using
19+
service account credentials.
20+
21+
Before you start the application, please review the comments starting with
22+
"TODO(developer)" and update the code to use correct values.
23+
24+
Usage:
25+
npm install
26+
node quickstart.js
27+
*/
28+
29+
function main(propertyId = 'YOUR-GA4-PROPERTY-ID') {
30+
// [START analyticsdata_quickstart]
31+
/**
32+
* TODO(developer): Uncomment this variable and replace with your
33+
* Google Analytics 4 property ID before running the sample.
34+
*/
35+
// propertyId = 'YOUR-GA4-PROPERTY-ID';
36+
37+
// [START analyticsdata_run_report_initialize]
38+
// Imports the Google Analytics Data API client library.
39+
const {BetaAnalyticsDataClient} = require('@google-analytics/data');
40+
41+
// Using a default constructor instructs the client to use the credentials
42+
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
43+
const analyticsDataClient = new BetaAnalyticsDataClient();
44+
// [END analyticsdata_run_report_initialize]
45+
46+
// Runs a simple report.
47+
async function runReport() {
48+
// [START analyticsdata_run_report]
49+
const [response] = await analyticsDataClient.runReport({
50+
property: `properties/${propertyId}`,
51+
dateRanges: [
52+
{
53+
startDate: '2020-03-31',
54+
endDate: 'today',
55+
},
56+
],
57+
dimensions: [
58+
{
59+
name: 'city',
60+
},
61+
],
62+
metrics: [
63+
{
64+
name: 'activeUsers',
65+
},
66+
],
67+
});
68+
// [END analyticsdata_run_report]
69+
70+
// [START analyticsdata_run_report_response]
71+
console.log('Report result:');
72+
response.rows.forEach(row => {
73+
console.log(row.dimensionValues[0], row.metricValues[0]);
74+
});
75+
// [END analyticsdata_run_report_response]
76+
}
77+
78+
runReport();
79+
// [END analyticsdata_quickstart]
80+
}
81+
82+
process.on('unhandledRejection', err => {
83+
console.error(err.message);
84+
process.exitCode = 1;
85+
});
86+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)
Please sign in to comment.