|
| 1 | +// Copyright 2022 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 application demonstrating the creation of |
| 18 | +a realtime report using minute ranges. |
| 19 | +
|
| 20 | +See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport#body.request_body.FIELDS.minute_ranges |
| 21 | +for more information. |
| 22 | +
|
| 23 | + Before you start the application, please review the comments starting with |
| 24 | + "TODO(developer)" and update the code to use correct values. |
| 25 | +
|
| 26 | + Usage: |
| 27 | + npm install |
| 28 | + node runRealtimeReportWithMinuteRanges.js |
| 29 | + */ |
| 30 | + |
| 31 | +function main(propertyId = 'YOUR-GA4-PROPERTY-ID') { |
| 32 | + // [START analyticsdata_run_realtime_report_with_minute_ranges] |
| 33 | + |
| 34 | + // TODO(developer): Uncomment this variable and replace with your |
| 35 | + // Google Analytics 4 property ID before running the sample. |
| 36 | + // propertyId = 'YOUR-GA4-PROPERTY-ID'; |
| 37 | + |
| 38 | + // Imports the Google Analytics Data API client library. |
| 39 | + const {BetaAnalyticsDataClient} = require('@google-analytics/data'); |
| 40 | + |
| 41 | + // Initialize client that will be used to send requests. This client only |
| 42 | + // needs to be created once, and can be reused for multiple requests. |
| 43 | + const analyticsDataClient = new BetaAnalyticsDataClient(); |
| 44 | + |
| 45 | + // Runs a report using two date ranges. |
| 46 | + async function runRealtimeReportWithMinuteRanges() { |
| 47 | + const [response] = await analyticsDataClient.runRealtimeReport({ |
| 48 | + property: `properties/${propertyId}`, |
| 49 | + minuteRanges: [ |
| 50 | + { |
| 51 | + name: '0-4 minutes ago', |
| 52 | + start_minutes_ago: 4, |
| 53 | + end_minutes_ago: 0, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: '25-29 minutes ago', |
| 57 | + start_minutes_ago: 29, |
| 58 | + end_minutes_ago: 25, |
| 59 | + }, |
| 60 | + ], |
| 61 | + metrics: [ |
| 62 | + { |
| 63 | + name: 'activeUsers', |
| 64 | + }, |
| 65 | + ], |
| 66 | + }); |
| 67 | + printRunReportResponse(response); |
| 68 | + } |
| 69 | + |
| 70 | + runRealtimeReportWithMinuteRanges(); |
| 71 | + |
| 72 | + // Prints results of a runReport call. |
| 73 | + function printRunReportResponse(response) { |
| 74 | + console.log(`${response.rowCount} rows received`); |
| 75 | + response.dimensionHeaders.forEach(dimensionHeader => { |
| 76 | + console.log(`Dimension header name: ${dimensionHeader.name}`); |
| 77 | + }); |
| 78 | + response.metricHeaders.forEach(metricHeader => { |
| 79 | + console.log( |
| 80 | + `Metric header name: ${metricHeader.name} (${metricHeader.type})` |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + console.log('Report result:'); |
| 85 | + response.rows.forEach(row => { |
| 86 | + console.log( |
| 87 | + `${row.dimensionValues[0].value}, ${row.metricValues[0].value}` |
| 88 | + ); |
| 89 | + }); |
| 90 | + } |
| 91 | + // [END analyticsdata_run_realtime_report_with_minute_ranges] |
| 92 | +} |
| 93 | + |
| 94 | +process.on('unhandledRejection', err => { |
| 95 | + console.error(err.message); |
| 96 | + process.exitCode = 1; |
| 97 | +}); |
| 98 | +main(...process.argv.slice(2)); |
0 commit comments