Search code examples
pythongoogle-analytics-api

400 Cohort dimensions can only be used in requests with a CohortSpec but there is no sufficient information in GA4 or in API itself


request = RunReportRequest(
        property='properties/'+property_id, 
        dimensions=[Dimension(name="cohort"),Dimension(name='cohortNthDay')],metrics=[Metric(name='cohortActiveUsers')],      
       date_ranges=[DateRange(start_date="2022-11-20",end_date="2022-12-12")],)

df_r=pd.DataFrame(format_report(request))



This is the last part of codes

I know that another item called CohortSpec should be added with it's own parameters however neither Google Analytics Data v1beta API nor GA4 Data Api page is giving enough information or examples to run the query to be able to calculate cohort retention results. Does anyone know or did before how to run the request fort cohorts ?


Solution

  • There are some examples of creating cohort requests in Cohort Report Examples. There are some python examples of cohort requests in this run_report_with_cohorts.py sample on GitHub.

    Thanks, Brett

    The Google Analytics Team