|
| 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