Learn business growth with Google Analytics 4 Forums Google Analytics 4 Obtaining segmented data from GA4 property report using the Google Analytics API

  • Wyatt

    Member
    29 May 2023 at 11:25 pm

    Dealing with the v1Beta API can feel like you’re trying to have a chat with a stubborn mule, mate. But it’s not all bad, I’ve got a neat PHP workaround that might just do the trick for you.

    Check out this PHP class called ‘analytics’. With it, I can set my property ID with setPropertyId, grab the credentials file using private function getCredentials, and get my client set up with getClient.

    The clincher is in this ‘get’ function, where I make a ‘runReport’ request. You can input your property id, start and end dates, dimensions, and metrics. It’s a bit like ordering a meal – asking it nicely (obviously) to fetch your desired data within a certain timeframe.

    After the response comes in, I use a simple foreach loop to go through the rows, returning the values I need.

    So, give this a stab, mate. Incorporating segments is a bit like requesting extra gravy on your chips. Customize as needed and look at your result in different ways by changing the dimensions and metrics. Happy coding!

  • Noah

    Member
    3 June 2023 at 2:49 pm

    Sure thing! While the v1 Beta API deprecates some of the features synonymous with the older Google Analytics API, including the ability to use ‘segments’, it does introduce some powerful new features that might help you out: Filters and Cohorts. The use of the ‘filters’ parameter can essentially recreate much of the ‘segment’ functionality. You can apply these filters to define subsets of your data by including or excluding data based on dimensions or metrics. You’d define filters within the ‘dimensionFilter’ and ‘metricFilter’ parameters of your ‘runReport’ request. Also, the new ‘cohortSpec’ feature can produce cohort analysis reports to help track the behavior of a group of users in your defined cohort over time. Finally, potentially key to your request, you could look into setting up ‘audiences’ in Google Analytics 4 properties, which work like a charm with the new API. You’d use the ‘audience’ feature in your ‘runReport’ request. Check out more in the Google Developers guide.

Log in to reply.