Skip to content

Commit

Permalink
Add ability to choose dimensions and metrics to google analytics repo…
Browse files Browse the repository at this point in the history
…rt (#9156)

* Metrics and Dimensions from Vector

* Add dropdowns

* Small cleanup

* Updated list from Cass
  • Loading branch information
AdRiley authored Feb 24, 2024
1 parent 245ec7a commit 4420a40
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions distribution/lib/Standard/Google_Api/0.0.0-dev/src/Main.enso
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from Standard.Base import all
import Standard.Base.Data.Array_Proxy.Array_Proxy

import Standard.Base.Metadata.Display
from Standard.Base.Metadata import make_single_choice, Widget
from Standard.Base.Metadata.Widget import Single_Choice, Vector_Editor
from Standard.Table import Table

polyglot java import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
Expand Down Expand Up @@ -68,29 +70,46 @@ initialize secret_file =
json_factory = GsonFactory.getDefaultInstance
Google_Api_Client.Value credential json_factory http_transport

## PRIVATE
make_dimensions_vector_selector : Widget
make_dimensions_vector_selector =
item_editor = make_single_choice ['country', 'date', 'isConversionEvent', 'language', 'userAgeBracket', 'userGender']
Vector_Editor item_editor=item_editor item_default=item_editor.values.first.value display=Display.Always

## PRIVATE
make_metrics_vector_selector : Widget
make_metrics_vector_selector =
item_editor = make_single_choice ['activeUsers', 'bounceRate', 'conversions', 'newUsers', 'sessionsPerUser', 'userConversionRate']
Vector_Editor item_editor=item_editor item_default=item_editor.values.first.value display=Display.Always


## PLACEHOLDER, performs google analytics call
This method calls the google reporting v4 api. Presently it specifically returns activeUsers by City
This method calls the google reporting v4 api.
Arguments:
- property_id: The Google Analytics property_id that is being queried
- start_date: The beginning date of the query. Default is 2020-03-31
- end_date: The end date being queried. Defaults to today.
run_analytics_report : Text -> Date -> Date -> Table
run_analytics_report property_id:Text start_date:Date=(Date.new 2020 3 31) end_date:Date=Date.today -> Table =
@dimensions make_dimensions_vector_selector
@metrics make_metrics_vector_selector
run_analytics_report : Text -> Date -> Date -> (Vector Text) -> (Vector Text) -> Table
run_analytics_report property_id:Text start_date:Date=(Date.new 2020 3 31) end_date:Date=Date.today dimensions:Vector=['userType'] metrics:Vector=['users'] -> Table =
analytics_data = BetaAnalyticsDataClient.create
request = RunReportRequest.newBuilder
request_builder = RunReportRequest.newBuilder
. setProperty ("properties/"+property_id)
. addDimensions (Dimension.newBuilder.setName "city")
. addMetrics (Metric.newBuilder.setName "activeUsers")
. addDateRanges (DateRange.newBuilder.setStartDate start_date.to_text . setEndDate end_date.to_text)
. build
dimensions.distinct.each dimension->
request_builder.addDimensions (Dimension.newBuilder.setName dimension)
metrics.distinct.each metric->
request_builder.addMetrics (Metric.newBuilder.setName metric)

request = request_builder.build
response = analytics_data.runReport request

dimension_count = response.getDimensionHeadersCount
dimensions = 0.up_to dimension_count . map i-> response.getDimensionHeaders i . getName
dimension_headers = 0.up_to dimension_count . map i-> response.getDimensionHeaders i . getName
metric_count = response.getMetricHeadersCount
metrics = 0.up_to metric_count . map i-> response.getMetricHeaders i . getName
headers = dimensions + metrics
metric_headers = 0.up_to metric_count . map i-> response.getMetricHeaders i . getName
headers = dimension_headers + metric_headers

row_count = response.getRowCount
row_proxy = Array_Proxy.new row_count i->
Expand Down

0 comments on commit 4420a40

Please sign in to comment.