|
| 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)); |
0 commit comments