Learn business growth with Google Analytics 4 Forums Google Analytics 4 How to implement dimension filters using GA4 APIs with Ruby?

  • How to implement dimension filters using GA4 APIs with Ruby?

    Posted by Matthew on 20 May 2023 at 2:08 am

    If you’re having problems adding filters to the requests for the GA4 Custom Dimension, you’re getting multiple errors. Notably, these errors aren’t showing up when you send your requests without any filters. The issue seems to be with the syntax, which you’re finding somewhat puzzling. You’ve already looked into this documentation – https://googleapis.dev/ruby/google-api-client/v0.53.0/Google/Apis/AnalyticsreportingV4/DimensionFilterClause.html, but you’re still encountering difficulty.

    Here’s the code block you’ve been using in Ruby,

    {
     :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
    }
    

    Would you be able to help identify what the correct syntax for this request should look like?

    Sophie replied 1 year, 4 months ago 3 Members · 2 Replies
  • 2 Replies
  • Charlotte

    Member
    3 June 2023 at 2:43 pm

    Sure! Looks like you’re looking to use dimension filters with GA4 APIs. Not a problem! In GA4 APIs, we use a ‘filters’ parameter to filter the data for specific dimensions.

    Alright, let’s dive into some code. In Ruby, you’ll first need to include this string require "google/apis/analyticsdata_v1alpha" at the very start. What this does is it pulls in the necessary Google modules for our use.

    Next, we’ll create an instance of the Google::Apis::AnalyticsdataV1alpha::AnalyticsDataService.

    Then, things start getting interesting! We create the ‘request’ object. In there, we’re specifying what we want the report request to consist of. You can see that there are ‘metric_aggregations’, ‘dimensions’ and ‘filters’. For ‘metric_aggregations’, we’re just using count, ‘dimensions’ is using a sample dimension, ‘eventName’.

    To filter that dimension, the ‘filters’ bit comes into play. In your case, it would be the equivalent of “customUser:type==Customer”.

    Finally, we call the ‘run_realtime_report’ function using our ‘request’ object, and print out the response in a readable JSON format.

    And there you have it! I hope this helps lighten the load. Happy coding!

  • Sophie

    Member
    24 June 2023 at 11:20 am

    It appears you’ve identified a solution through trial and error. Here is the corrected syntax for your request for v1beta:

    `ruby
    {
    :property=>”properties/”,
    :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=>””}}},
    :keep_empty_rows=>false,
    :return_property_quota=>true
    }
    `

    This should resolve the syntax errors you’ve been encountering in your GA4 Custom Dimension request. The changes mainly pertain to how the dimension filter is structured. The field_name should match the name from the dimensions array (e.g. customUser:type) and the match_type and value will determine the records to include in the results.

Log in to reply.