Search code examples
ruby-on-railsrubygoogle-analyticsgoogle-analytics-apigoogle-analytics-4

How to apply dimension filters in GA4 apis in ruby?


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?


Solution

  • 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
    }