Learn business growth with Google Analytics 4 Forums Google Analytics 4 . How to retrieve the property_id for a Google Analytics account using the GA4 Data API and Python Reply To: . How to retrieve the property_id for a Google Analytics account using the GA4 Data API and Python

  • Elijah

    Member
    9 July 2023 at 5:24 am

    The error you’re seeing indicates an issue with the authentication credentials. The service account credentials that you’ve generated need to be correctly configured to access Google Analytics data. Make sure the service account is assigned the correct permissions in your Google Cloud Console and that it’s added as a user in your Google Analytics property with the appropriate access level.

    In terms of pageToken, you might not need it unless you’re working with a use case that involves pagination. This is typically used when the API response is too large and it’s split across multiple pages. The pageToken would be employed to access subsequent pages in such cases.

    Check the following:
    1. When you generated your service account key, did you include the right scopes? That is, did you ensure that the scope ‘https://www.googleapis.com/auth/analytics.readonly’ is included when the service account key was generated?
    2. Did you add the service account to your Google Analytics Account and assign it the correct access permissions? You need to add it as a user to the respective Google Analytics Account and provide at least read and analyze permissions for the account.
    3. Is your Google Analytics Account indeed a GA4 one? GA4 accounts have a different set of APIs than Universal Analytics accounts and are not interchangeable.

    One more thing to note is that credentials.token is potentially not initialized by default. Instead, use credentials.get_access_token().access_token. So change your header to headers = {'Authorization': 'Bearer {}'.format(credentials.get_access_token().access_token)}.

    If all these conditions are met and you’re still facing the issue, you might want to regenerate the service account key and try again.