Skip to content

Commit 202113e

Browse files
committed
Add runRealtimeReport sample for minute ranges
1 parent 203abc1 commit 202113e

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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));
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// https://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
//
14+
// ** This file is automatically generated by gapic-generator-typescript. **
15+
// ** https://github.com/googleapis/gapic-generator-typescript **
16+
// ** All changes to this file may be overwritten. **
17+
18+
'use strict';
19+
20+
const cp = require('child_process');
21+
const {assert} = require('chai');
22+
const {describe, it} = require('mocha');
23+
24+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
25+
26+
const GA4_PROPERTY_ID = process.env.GA_TEST_PROPERTY_ID || '222596558';
27+
28+
describe('Realtime report with minute ranges', () => {
29+
it('should run realtime', async () => {
30+
// eslint-disable-next-line no-unused-vars
31+
const stdout = execSync(`node ./runRealtimeReportWithMinuteRanges.js ${GA4_PROPERTY_ID}`);
32+
assert.match(stdout, /Report result/);
33+
});
34+
});

0 commit comments

Comments
 (0)