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
- Go to the Google Cloud Console: Visit the Google Cloud Console website (https://console.cloud.google.com) and sign in using your Google account.
- 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”.
- 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”.
- Search for the Google Analytics Reporting API: In the library search bar, type “Google Analytics Reporting API” and select it from the results.
- Enable the API: Click on the “Enable” button to enable the Google Analytics Reporting API for your project.
Step 2: Set Up API Credentials
- Navigate to API Credentials: In the Google Cloud Console, go to “APIs & Services” -> “Credentials”.
- Click on “Create Credentials”: In the Credentials section, click on the “Create Credentials” button and select “OAuth client ID”.
- Choose application type:
- Select “Web application” as the application type.
- Configure the OAuth client ID:
- Enter a name for your credentials (e.g., “Google Analytics API Access”).
http://localhostas the origin and
http://localhost/redirectas the redirect URI.
- Click the “Create” button to generate the OAuth 2.0 Client ID.
- 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
- 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.
- 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
- 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')
- Build the Google Analytics Reporting service:
analytics = build('analyticsreporting', 'v4', credentials=credentials)
- 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:
import datetime today = datetime.date.today()
start_date = datetime.date(today.year, today.month – 1, 1).strftime(‘%Y-%