Learn business growth with Google Analytics 4 › Forums › Google Analytics 4 › How to implement dimension filters using GA4 APIs with Ruby? › Reply To: How to implement dimension filters using GA4 APIs with Ruby?
-
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!