googleapis / google-cloud-python

Google Cloud Client Library for Python
https://googleapis.github.io/google-cloud-python/
Apache License 2.0
4.81k stars 1.52k forks source link

dimension for customEvent is not working #12466

Open temuycro opened 7 months ago

temuycro commented 7 months ago

Hello.

Whenever I want to get any Event-Scoped Custom Dimension, the API doesn't recognizes the function. I followed the syntax as showin in the official documentation, but I still get this error:

ValueError: Protocol message Dimension has no "customEvent" field.

My goal is to get the eventCount (number of events) from a custom event I created AND currently measuring and making reports on Looker Studio and Explorations directly on the Google Analytics 4 interface.

Does anybody has the same problem?

These are the libraries I'm loading

# Load the libraries

from google.oauth2 import service_account
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import RunReportRequest, FilterExpression, Filter
import pandas as pd

And this is the request code:

# Path to service account key file.
key_path = 'credencial_ga4.json'

# Initialize client
credentials = service_account.Credentials.from_service_account_file(
    key_path, scopes=["https://www.googleapis.com/auth/analytics.readonly"]
)
client = BetaAnalyticsDataClient(credentials=credentials)

# Create a report request with a filter.
request = RunReportRequest(
    property=f"properties/XXXXXXXXX",
    dimensions=[{"customEvent": "click"}],
    metrics=[{"name": "eventCount"}],
    date_ranges=[{"start_date": "2020-01-01", "end_date": "today"}],
    dimension_filter=FilterExpression(
        filter=Filter(
            field_name="hostName",
            string_filter=Filter.StringFilter(value="subdomain.domain.com", match_type=Filter.StringFilter.MatchType.EXACT)
        )
    )
)

# Make the request.
response = client.run_report(request)
parthea commented 6 months ago

Hi @temuycro,

dimensions should be of type Dimension. Something like [{"name": "value"}] is expected, as an example.

dimensions=[{"name":"customEvent:click"}], could be used in your example

Try the following

request = RunReportRequest(
    property=f"properties/XXXXXXXXX",
    dimensions=[{"name":"customEvent:click"}],
    metrics=[{"name": "eventCount"}],
    date_ranges=[{"start_date": "2020-01-01", "end_date": "today"}],
    dimension_filter=FilterExpression(
        filter=Filter(
            field_name="hostName",
            string_filter=Filter.StringFilter(value="subdomain.domain.com", match_type=Filter.StringFilter.MatchType.EXACT)
        )
    )
)

Please also see https://developers.google.com/analytics/devguides/reporting/data/v1/advanced#event-scoped_custom_dimensions for more examples.

temuycro commented 6 months ago

Hi @parthea,

Nope, it didn't work :(

InvalidArgument: 400 Did you mean customEvent:list? Field customEvent:click 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

ikuleshov commented 6 months ago

temuycro@,

I suggest you try using the API Explorer to call the getMetadata method of the Data API and make sure this dimension is available. Chances are, "click" is not among the custom event parameters available for your property. For further questions, please contact the Google Analytics API Support team, as this question does not seem to be specific to the client library implementation.

temuycro commented 6 months ago

@ikuleshov hi! Thank you!

Is there a way to do this using Python?

parthea commented 6 months ago

@temuycro,

The client library was able to send the request to the API however the response from the API was InvalidArgument: 400 Did you mean customEvent:list? Field customEvent:click is not a valid dimension.

As mentioned in https://github.com/googleapis/google-cloud-python/issues/12466#issuecomment-2035615039, please use the Try it method on this page to confirm that click is among the custom event parameters available for your property. This is also mentioned in setup 1 here. Please let us know the result after querying the property.

temuycro commented 6 months ago

@parthea hello.

I tried using this endpoint GET https://analyticsdata.googleapis.com/v1beta/properties/GA4_PROPERTY_ID/metadata (changing my GA4_PROPERTY_ID with mine. But I don't know how to get the data, since my credentials is a .json file.

Sorry, I'm not a developer. I'm currently using Postman.

Would you guide me through this? Also, at least a little bit of code on Python, like the one I used in the first comment?

parthea commented 6 months ago

@temuycro , Can you try using the Try it interface on this page https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/getMetadata?

temuycro commented 6 months ago

@parthea Done! Sorry I didn't understand when you mentioned Try it because my documentation is in Spanish lol.

You were right: the customEvent:click is not available. Do you know why is that?

parthea commented 6 months ago

@temuycro , This is an API specific question rather than a client library question as mentioned in https://github.com/googleapis/google-cloud-python/issues/12466#issuecomment-2035615039. You'll get better quality answers from the API team. Please fill out this form to obtain technical support for the API: https://developers.google.com/analytics/devguides/support