Home > Blockchain >  How to apply dimension filters in GA4 apis in ruby?
How to apply dimension filters in GA4 apis in ruby?

Time:01-19

I am trying to add filters to the requests for the GA4 Custom Dimension but getting multiples error.

If I try to send without any filters I am able to get data so no issue with the custom dimension setting.

I am getting confused about the syntax.

referred from - https://googleapis.dev/ruby/google-api-client/v0.53.0/Google/Apis/AnalyticsreportingV4/DimensionFilterClause.html

{
 :property=>"properties/1234",
 :dimensions=>[{:name=>"date"}, {:name=>"customUser:type"}],
 :metrics=>[{:name=>"activeUsers", :invisible=>false}],
 :date_ranges=>[{:start_date=>"2023-01-01", :end_date=>"today", :name=>"date"}],
 :order_bys=>[{:dimension=>{:dimension_name=>"date"}}],
 :dimension_filter_clauses=>[{:dimension_name=>"customUser:type", :expressions=>["Customer"], :not=>false, :operator=>"OR"}],
 :keep_empty_rows=>false,
 :return_property_quota=>true
}

What should be the correct syntax to make the request?

CodePudding user response:

In Google Analytics 4 (GA4) APIs, you can apply dimension filters to retrieve data for specific dimensions. The GA4 APIs use the filters parameter to filter the data

require "google/apis/analyticsdata_v1alpha"

analyticsdata_service = Google::Apis::AnalyticsdataV1alpha::AnalyticsDataService.new

# request parameters
request = Google::Apis::AnalyticsdataV1alpha::RunRealtimeReportRequest.new(
  report_request: Google::Apis::AnalyticsdataV1alpha::ReportRequest.new(
    metric_aggregations: ["count"],
    dimensions: ["eventName"],
    filters: "eventName==ExampleEvent"
  )
)

response = analyticsdata_service.run_realtime_report(request)

puts response.to_json

CodePudding user response:

I got the answer after trial and error this will be for v1beta.

{
 :property=>"properties/<property_id>",
 :dimensions=>[{:name=>"date"}, {:name=>"customUser:type"}],
 :metrics=>[{:name=>"activeUsers", :invisible=>false}],
 :date_ranges=>[{:start_date=>"2023-01-01", :end_date=>"today", :name=>"date"}],
 :order_bys=>[{:dimension=>{:dimension_name=>"date"}}],
 :dimension_filter=>{:filter=>{:field_name=>"customUser:type", :string_filter=>{ :match_type=> "EXACT", :value=>"<value>"}}},
 :keep_empty_rows=>false,
 :return_property_quota=>true
}

  • Related