styk924
styk924

Reputation: 21

Custom dimensions on Google Analytics Data API (GA4) Python

I am trying to make a report using the quickstart code described Here, it works perfectly, but when I try to add a custom dimension, I obtain the following Error

google.api_core.exceptions.InvalidArgument: 400 Field uid is not a valid dimension. For a list of valid dimensions and metrics, see https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema

I am able to make reports using this custom dimension in google analytics hub, so I dont understand why I am getting this error, this is the code (the custom dimension is uid)

def sample_run_report(property_id):
    """Runs a simple report on a Google Analytics App+Web property."""

    # Using a default constructor instructs the client to use the credentials
    # specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
    client = AlphaAnalyticsDataClient(credentials=credentials)
    request = RunReportRequest(
        entity=Entity(property_id=property_id),
        dimensions=[Dimension(name='uid')],
        metrics=[Metric(name='userEngagementDuration')],
        date_ranges=[DateRange(start_date='2020-07-01',end_date='today')])

    response = client.run_report(request)

    print("Report result:")
    
    for row in response.rows:
        print(row.dimension_values[0].value, row.metric_values[0].value)

def main():
  sample_run_report(xxxxxx)

if __name__ == '__main__':
  main()

EDIT: I Was able to create the query using name="customUser:uid"

Upvotes: 2

Views: 4332

Answers (2)

mayuran.si
mayuran.si

Reputation: 31

If you're using custom dimension then you need to give in this format:

dimensions=[Dimension(name="sessionDefaultChannelGroup"), Dimension(name="sessionSourceMedium"),
                    Dimension(name="sessionCampaignName"), Dimension(name="date")
                    Dimension(name="customEvent:plan_name")]

Dimension(name="customEvent:yourCustomDimension"). Some time we need to use customUser instead of customEvent
For more reference: https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema#custom_dimensions

Upvotes: 1

Brett
Brett

Reputation: 1289

Try updating from uid to customEvent:uid in your request. The syntax for custom dimensions is documented here.

You can query the Metadata API method to list the registered custom dimensions for this property. See an example of the query. The metadata method will likely return a dimension similar to:

"dimensions": [
...
    {
      "apiName": "customEvent:uid",
      "uiName": "uid",
      "description": "An event scoped custom dimension for your Analytics property."
    },
...
],

If the Metadata method does not return a custom dimension, one is not registered for this property. Because you say "I am able to make reports using this custom dimension in google analytics hub", you likely have registered this dimension.

Upvotes: 1

Related Questions