Skip to content

Commit 539500b

Browse files
committed
Add runFunnelReport sample
Also fix a few typos where snake case was used instead of camel case.
1 parent 202113e commit 539500b

File tree

4 files changed

+228
-4
lines changed

4 files changed

+228
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ node_modules/
22
**/package-lock.json
33
coverage/
44
google-analytics-data/oauth2.keys.json
5+
/.vscode/
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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
18+
of a funnel report.
19+
20+
See https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/properties/runFunnelReport
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 runFunnelReport.js
29+
*/
30+
31+
function main(propertyId = 'YOUR-GA4-PROPERTY-ID') {
32+
// [START analyticsdata_run_funnel_report]
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 {AlphaAnalyticsDataClient} = require('@google-analytics/data').v1alpha;
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 AlphaAnalyticsDataClient();
44+
45+
// Runs a funnel query to build a report with 5 funnel steps.
46+
// Step 1: First open/visit (event name is `first_open` or `first_visit`).
47+
// Step 2: Organic visitors (`firstUserMedium` dimension contains the term
48+
// "organic").
49+
// Step 3: Session start (event name is `session_start`).
50+
// Step 4: Screen/Page view (event name is `screen_view` or `page_view`).
51+
// Step 5: Purchase (event name is `purchase` or `in_app_purchase`).
52+
53+
// The report configuration reproduces the default funnel report provided in
54+
// the Funnel Exploration template of the Google Analytics UI.
55+
// See more at https://support.google.com/analytics/answer/9327974
56+
async function runFunnelReport() {
57+
const [response] = await analyticsDataClient.runFunnelReport({
58+
property: `properties/${propertyId}`,
59+
dateRanges: [{startDate: '30daysAgo', endDate: 'today'}],
60+
funnelBreakdown: {
61+
breakdownDimension: { name: 'deviceCategory' },
62+
},
63+
funnel: {
64+
steps: [
65+
{
66+
name: 'First open/visit',
67+
filterExpression: {
68+
orGroup: {
69+
expressions: [
70+
{ funnelEventFilter: {eventName: 'first_open'}},
71+
{ funnelEventFilter: {eventName: 'first_visit'}}
72+
]
73+
}
74+
}
75+
},
76+
{
77+
name: 'Organic visitors',
78+
filterExpression: {
79+
funnelFieldFilter: {
80+
fieldName: 'firstUserMedium',
81+
stringFilter: {
82+
matchType: 'CONTAINS',
83+
caseSensitive: false,
84+
value: 'organic'
85+
}
86+
}
87+
}
88+
},
89+
{
90+
name: 'Session start',
91+
filterExpression: {
92+
funnelEventFilter: {
93+
eventName: 'session_start',
94+
}
95+
}
96+
},
97+
{
98+
name: 'Screen/Page view',
99+
filterExpression: {
100+
orGroup: {
101+
expressions: [
102+
{ funnelEventFilter: {eventName: 'screen_view'}},
103+
{ funnelEventFilter: {eventName: 'page_view'}},
104+
]
105+
}
106+
}
107+
},
108+
{
109+
name: 'Screen/Page view',
110+
filterExpression: {
111+
orGroup: {
112+
expressions: [
113+
{ funnelEventFilter: {eventName: 'screen_view'}},
114+
{ funnelEventFilter: {eventName: 'page_view'}},
115+
]
116+
}
117+
}
118+
},
119+
{
120+
name: 'Purchase',
121+
filterExpression: {
122+
orGroup: {
123+
expressions: [
124+
{ funnelEventFilter: {eventName: 'purchase'}},
125+
{ funnelEventFilter: {eventName: 'in_app_purchase'}},
126+
]
127+
}
128+
}
129+
},
130+
]
131+
}
132+
});
133+
printRunFunnelReportResponse(response);
134+
}
135+
136+
// [START analyticsdata_print_run_funnel_report_response]
137+
// Prints contents of a FunnelSubReport object.
138+
function printFunnelSubReport(funnelSubReport) {
139+
console.log('Dimension headers:');
140+
funnelSubReport.dimensionHeaders.forEach(dimensionHeader => {
141+
console.log(dimensionHeader.name);
142+
});
143+
console.log('\nMetric headers:');
144+
funnelSubReport.metricHeaders.forEach(metricHeader => {
145+
console.log(metricHeader.name);
146+
});
147+
console.log('\nDimensions and metric values for each row in the report:');
148+
funnelSubReport.rows.forEach((row, rowIndex) => {
149+
console.log(`\nRow #${rowIndex}`);
150+
row.dimensionValues.forEach((dimensionValue, dimensionIndex) => {
151+
const dimensionName = funnelSubReport.dimensionHeaders[dimensionIndex].name;
152+
console.log(`\n${dimensionName}: ${dimensionValue.value}`);
153+
})
154+
155+
row.metricValues.forEach((metricValue, metricIndex) => {
156+
const metricName = funnelSubReport.metricHeaders[metricIndex].name;
157+
console.log(`\n${metricName}: ${metricValue.value}`);
158+
})
159+
});
160+
161+
console.log('\nSampling metadata for each date range:');
162+
funnelSubReport.metadata.samplingMetadatas.forEach((metadata, metadataIndex) => {
163+
console.log(`Sampling metadata for date range #${metadataIndex}: `
164+
`samplesReadCount=${metadata.samplesReadCount}, `
165+
`samplingSpaceSize=${metadata.samplingSpaceSize}`
166+
);
167+
});
168+
}
169+
170+
// Prints results of a runFunnelReport call.
171+
function printRunFunnelReportResponse(response) {
172+
console.log('Report result:');
173+
console.log('=== FUNNEL VISUALIZATION ===');
174+
printFunnelSubReport(response.funnelVisualization);
175+
176+
console.log('=== FUNNEL TABLE ===');
177+
printFunnelSubReport(response.funnelTable);
178+
}
179+
// [END analyticsdata_print_run_funnel_report_response]
180+
181+
runFunnelReport();
182+
// [END analyticsdata_run_funnel_report]
183+
}
184+
185+
process.on('unhandledRejection', err => {
186+
console.error(err.message);
187+
process.exitCode = 1;
188+
});
189+
main(...process.argv.slice(2));

google-analytics-data/runRealtimeReportWithMinuteRanges.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ function main(propertyId = 'YOUR-GA4-PROPERTY-ID') {
4949
minuteRanges: [
5050
{
5151
name: '0-4 minutes ago',
52-
start_minutes_ago: 4,
53-
end_minutes_ago: 0,
52+
startMinutesAgo: 4,
53+
endMinutesAgo: 0,
5454
},
5555
{
5656
name: '25-29 minutes ago',
57-
start_minutes_ago: 29,
58-
end_minutes_ago: 25,
57+
startMinutesAgo: 29,
58+
endMinutesAgo: 25,
5959
},
6060
],
6161
metrics: [
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('RunFunnelReport', () => {
29+
it('should run runFunnelReport', async () => {
30+
// eslint-disable-next-line no-unused-vars
31+
const stdout = execSync(`node ./runFunnelReport.js ${GA4_PROPERTY_ID}`);
32+
assert.match(stdout, /Report result/);
33+
});
34+
});

0 commit comments

Comments
 (0)