INVALID_ARGUMENT: Metric names must only contain letters, numbers, or _.

So, you're encountering an error while trying to retrieve GA4 data using the Google Analytics Data API, correct? I understand your frustration because I've been in the same situation multiple times. However, this time I have a solution for you because I also needed that data urgently. Let's not waste any time and dive right into the details of how to resolve this issue.

To start from the beginning, I was working on a GA4 account for one of my clients who requested me to retrieve data from an additional GA4 property using the API. Initially, it seemed like an easy task since we had prepared scripts before. However, things got complicated when I started encountering this error.

Error message:

google.api_core.exceptions.InvalidArgument: 400 Metric names must only contain letters, numbers, or *. Received a metric name = conversions[wysłanie_formularza_event]

I was puzzled about how to make the API recognize the UTF-8 characters in the metric name so that I could retrieve the data. If anyone here has faced a similar problem or can provide guidance on resolving this issue, I would greatly appreciate it (just kidding, below is the solution prepared for you).

I understand that changing the event name could be a possible solution, but since it's not my account, I need to download the historical data for now.

In the event editor, we have validation that limits us to using letters, numbers, and underscores. However, there are events with names containing polish characters (who uses them in GA4). When I query these conversion events, I receive the following error: INVALID_ARGUMENT: Metric names must only contain letters, numbers, or _.

So, like me, I started exploring using the ga-dev-tool, and eventually, I came up with an idea. I decided to download all the eventName and count the conversions separately, which allowed the API to display the necessary data. Surprisingly, it worked!

Once I had downloaded these files, the next step was to merge them. Personally, I prefer using Python, so I utilized Pandas to accomplish this task. Below is the code I want to share with you all so that you can save time.

If you found this post helpful and would like to connect on LinkedIn, that would be much appreciated!

Unlock the power of Data & Webflow with Digi2. We tailor websites that convert and empower data-driven decisions. Get started today!

Table of contents
How to configure dataLayer in GTM?