|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2022 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Google Analytics Data API sample application demonstrating the creation of |
| 18 | +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 | +# [START analyticsdata_run_funnel_report] |
| 24 | +from google.analytics.data_v1alpha import AlphaAnalyticsDataClient |
| 25 | +from google.analytics.data_v1alpha.types import DateRange |
| 26 | +from google.analytics.data_v1alpha.types import Dimension |
| 27 | +from google.analytics.data_v1alpha.types import Funnel |
| 28 | +from google.analytics.data_v1alpha.types import FunnelBreakdown |
| 29 | +from google.analytics.data_v1alpha.types import FunnelEventFilter |
| 30 | +from google.analytics.data_v1alpha.types import FunnelFieldFilter |
| 31 | +from google.analytics.data_v1alpha.types import FunnelFilterExpression |
| 32 | +from google.analytics.data_v1alpha.types import FunnelFilterExpressionList |
| 33 | +from google.analytics.data_v1alpha.types import FunnelStep |
| 34 | +from google.analytics.data_v1alpha.types import RunFunnelReportRequest |
| 35 | +from google.analytics.data_v1alpha.types import StringFilter |
| 36 | + |
| 37 | + |
| 38 | +def run_sample(): |
| 39 | + """Runs the sample.""" |
| 40 | + # TODO(developer): Replace this variable with your Google Analytics 4 |
| 41 | + # property ID before running the sample. |
| 42 | + property_id = "YOUR-GA4-PROPERTY-ID" |
| 43 | + run_funnel_report(property_id) |
| 44 | + |
| 45 | + |
| 46 | +def run_funnel_report(property_id="YOUR-GA4-PROPERTY-ID"): |
| 47 | + """Runs a funnel query to build a report with 5 funnel steps. |
| 48 | + Step 1: First open/visit (event name is `first_open` or `first_visit`). |
| 49 | + Step 2: Organic visitors (`firstUserMedium` dimension contains the term |
| 50 | + "organic"). |
| 51 | + Step 3: Session start (event name is `session_start`). |
| 52 | + Step 4: Screen/Page view (event name is `screen_view` or `page_view`). |
| 53 | + Step 5: Purchase (event name is `purchase` or `in_app_purchase`). |
| 54 | +
|
| 55 | + The report configuration reproduces the default funnel report provided in |
| 56 | + the Funnel Exploration template of the Google Analytics UI. |
| 57 | + See more at https://support.google.com/analytics/answer/9327974 |
| 58 | + """ |
| 59 | + client = AlphaAnalyticsDataClient() |
| 60 | + |
| 61 | + request = RunFunnelReportRequest( |
| 62 | + property=f"properties/{property_id}", |
| 63 | + date_ranges=[DateRange(start_date="30daysAgo", end_date="today")], |
| 64 | + funnel_breakdown=FunnelBreakdown( |
| 65 | + breakdown_dimension=Dimension(name="deviceCategory") |
| 66 | + ), |
| 67 | + funnel=Funnel( |
| 68 | + steps=[ |
| 69 | + FunnelStep( |
| 70 | + name="First open/visit", |
| 71 | + filter_expression=FunnelFilterExpression( |
| 72 | + or_group=FunnelFilterExpressionList( |
| 73 | + expressions=[ |
| 74 | + FunnelFilterExpression( |
| 75 | + funnel_event_filter=FunnelEventFilter( |
| 76 | + event_name="first_open" |
| 77 | + ) |
| 78 | + ), |
| 79 | + FunnelFilterExpression( |
| 80 | + funnel_event_filter=FunnelEventFilter( |
| 81 | + event_name="first_visit" |
| 82 | + ) |
| 83 | + ), |
| 84 | + ] |
| 85 | + ) |
| 86 | + ), |
| 87 | + ), |
| 88 | + FunnelStep( |
| 89 | + name="Organic visitors", |
| 90 | + filter_expression=FunnelFilterExpression( |
| 91 | + funnel_field_filter=FunnelFieldFilter( |
| 92 | + field_name="firstUserMedium", |
| 93 | + string_filter=StringFilter( |
| 94 | + match_type=StringFilter.MatchType.CONTAINS, |
| 95 | + case_sensitive=False, |
| 96 | + value="organic", |
| 97 | + ), |
| 98 | + ) |
| 99 | + ), |
| 100 | + ), |
| 101 | + FunnelStep( |
| 102 | + name="Session start", |
| 103 | + filter_expression=FunnelFilterExpression( |
| 104 | + funnel_event_filter=FunnelEventFilter( |
| 105 | + event_name="session_start" |
| 106 | + ) |
| 107 | + ), |
| 108 | + ), |
| 109 | + FunnelStep( |
| 110 | + name="Screen/Page view", |
| 111 | + filter_expression=FunnelFilterExpression( |
| 112 | + or_group=FunnelFilterExpressionList( |
| 113 | + expressions=[ |
| 114 | + FunnelFilterExpression( |
| 115 | + funnel_event_filter=FunnelEventFilter( |
| 116 | + event_name="screen_view" |
| 117 | + ) |
| 118 | + ), |
| 119 | + FunnelFilterExpression( |
| 120 | + funnel_event_filter=FunnelEventFilter( |
| 121 | + event_name="page_view" |
| 122 | + ) |
| 123 | + ), |
| 124 | + ] |
| 125 | + ) |
| 126 | + ), |
| 127 | + ), |
| 128 | + FunnelStep( |
| 129 | + name="Purchase", |
| 130 | + filter_expression=FunnelFilterExpression( |
| 131 | + or_group=FunnelFilterExpressionList( |
| 132 | + expressions=[ |
| 133 | + FunnelFilterExpression( |
| 134 | + funnel_event_filter=FunnelEventFilter( |
| 135 | + event_name="purchase" |
| 136 | + ) |
| 137 | + ), |
| 138 | + FunnelFilterExpression( |
| 139 | + funnel_event_filter=FunnelEventFilter( |
| 140 | + event_name="in_app_purchase" |
| 141 | + ) |
| 142 | + ), |
| 143 | + ] |
| 144 | + ) |
| 145 | + ), |
| 146 | + ), |
| 147 | + ] |
| 148 | + ), |
| 149 | + ) |
| 150 | + response = client.run_funnel_report(request) |
| 151 | + print_run_funnel_report_response(response) |
| 152 | + |
| 153 | + |
| 154 | +# [START analyticsdata_print_run_funnel_report_response] |
| 155 | +def print_funnel_sub_report(funnel_sub_report): |
| 156 | + """Prints the contents of a FunnelSubReport object.""" |
| 157 | + print("Dimension headers:") |
| 158 | + for dimension_header in funnel_sub_report.dimension_headers: |
| 159 | + print(dimension_header.name) |
| 160 | + |
| 161 | + print("\nMetric headers:") |
| 162 | + for metric_header in funnel_sub_report.metric_headers: |
| 163 | + print(metric_header.name) |
| 164 | + |
| 165 | + print("\nDimensions and metric values for each row in the report:") |
| 166 | + for row_idx, row in enumerate(funnel_sub_report.rows): |
| 167 | + print("\nRow #{}".format(row_idx)) |
| 168 | + for field_idx, dimension_value in enumerate(row.dimension_values): |
| 169 | + dimension_name = funnel_sub_report.dimension_headers[field_idx].name |
| 170 | + print("{}: '{}'".format(dimension_name, dimension_value.value)) |
| 171 | + |
| 172 | + for field_idx, metric_value in enumerate(row.metric_values): |
| 173 | + metric_name = funnel_sub_report.metric_headers[field_idx].name |
| 174 | + print("{}: '{}'".format(metric_name, metric_value.value)) |
| 175 | + |
| 176 | + print("\nSampling metadata for each date range:") |
| 177 | + for metadata_idx, metadata in enumerate( |
| 178 | + funnel_sub_report.metadata.sampling_metadatas |
| 179 | + ): |
| 180 | + print( |
| 181 | + "Sampling metadata for date range #{}: samplesReadCount={}, " |
| 182 | + "samplingSpaceSize={}".format( |
| 183 | + metadata_idx, metadata.samples_read_count, metadata.sampling_space_size |
| 184 | + ) |
| 185 | + ) |
| 186 | + |
| 187 | + |
| 188 | +def print_run_funnel_report_response(response): |
| 189 | + """Prints results of a runFunnelReport call.""" |
| 190 | + print("Report result:") |
| 191 | + print("=== FUNNEL VISUALIZATION ===") |
| 192 | + print_funnel_sub_report(response.funnel_visualization) |
| 193 | + |
| 194 | + print("=== FUNNEL TABLE ===") |
| 195 | + print_funnel_sub_report(response.funnel_table) |
| 196 | + |
| 197 | + |
| 198 | +# [END analyticsdata_print_run_funnel_report_response] |
| 199 | + |
| 200 | + |
| 201 | +# [END analyticsdata_run_funnel_report] |
| 202 | + |
| 203 | + |
| 204 | +if __name__ == "__main__": |
| 205 | + run_sample() |
0 commit comments