How to use the Google Analytics Reporting API to retrieve GA4 visits

Here’s a detailed guide on how to use the Google Analytics Reporting API to retrieve GA4 visits for the last complete month:

Step 1: Set Up a Google Cloud Project and Enable the Reporting API

  1. Go to the Google Cloud Console: Visit the Google Cloud Console website (https://console.cloud.google.com) and sign in using your Google account.
  2. Create a new project: Click on the project dropdown menu at the top of the page and select “New Project”. Enter a name for your project and click “Create”.
  3. Enable the Google Analytics Reporting API: In the Google Cloud Console, navigate to your newly created project. On the left sidebar, click on “APIs & Services” and then “Library”.
  4. Search for the Google Analytics Reporting API: In the library search bar, type “Google Analytics Reporting API” and select it from the results.
  5. Enable the API: Click on the “Enable” button to enable the Google Analytics Reporting API for your project.

Step 2: Set Up API Credentials

  1. Navigate to API Credentials: In the Google Cloud Console, go to “APIs & Services” -> “Credentials”.
  2. Click on “Create Credentials”: In the Credentials section, click on the “Create Credentials” button and select “OAuth client ID”.
  3. Choose application type:
  • Select “Web application” as the application type.
  1. Configure the OAuth client ID:
  • Enter a name for your credentials (e.g., “Google Analytics API Access”).
  • Add the authorized JavaScript origins and redirect URIs as needed. For local development, you can use http://localhost as the origin and http://localhost/redirect as the redirect URI.
  • Click the “Create” button to generate the OAuth 2.0 Client ID.
  1. Retrieve the Client ID and Client Secret:
  • After creating the OAuth client ID, you will see a popup or a page displaying the Client ID and Client Secret values.
  • Take note of the Client ID and Client Secret as you will need them for authentication.

Step 3: Install the Required Libraries

  1. Choose your preferred programming language: Determine the programming language you’ll use to interact with the Google Analytics Reporting API. The following instructions cover Python as an example, but similar libraries are available for other languages.
  2. Install the necessary libraries: Use pip, the package installer for Python, to install the required libraries. Open your terminal or command prompt and run the following command:
   pip install google-api-python-client google-auth google-auth-oauthlib google-auth-httplib2

Step 4: Authenticate and Retrieve GA4 Visits for the Last Complete Month

  1. Set up the authentication:
  • In your code, import the required libraries: from google.oauth2 import service_account from googleapiclient.discovery import build
  • Load the JSON key file you downloaded when setting up the OAuth client ID:
    python credentials = service_account.Credentials.from_service_account_file('path/to/key_file.json')
  1. Build the Google Analytics Reporting service:
   analytics = build('analyticsreporting', 'v4', credentials=credentials)
  1. Retrieve GA4 visits for the last complete month:
  • Specify the GA4 property ID: property_id = 'your-ga4-property-id'
  • Define the date range for the last complete month:
    “`python
    import datetime today = datetime.date.today()
    start_date = datetime.date(today.year, today.month – 1, 1).strftime(‘%Y-%