top of page
  • Writer's pictureBadrish Shriniwas

Data extraction from GA4 with PowerShell

Introduction to GA4

Google Analytics 4 (GA4) is the next generation of Analytics that collects event-based data from both websites and apps. It is the future data collection standard and replaces the current Universal Analytics. The critical functions of GA4 are:

  • Collects both website and app data to understand the customer journey better

  • Uses event-based data instead of session-based

  • Includes privacy controls such as cookieless measurement and behavioural and conversion modelling

  • Predictive capabilities offer guidance without complex models

  • Direct integrations to media platforms help drive actions on your website or app.

Introduction to GA4 Properties

A property is a set of Google Analytics reports and data associated with one or more websites and apps. If data should generally be analysed together (product line, brand, application), data should be in one property.

The reports and interface vary depending on what kind of property you are using. For example, a Universal Analytics property has different reports from a Google Analytics 4 property.

Setting up GA4 Properties

In Admin, look at the Account column to ensure you've selected the right account. Then, in the Property column, click Create Property.

  • Enter a name for the property (e.g. "My Business, Inc website") and select the reporting time zone and currency.

  • Click Next. Select your industry category and business size.

  • Click Create and accept the Analytics Terms of Service and the Data Processing Amendment.

Setting up the Google Analytics API

Step 1: Navigate to Google Cloud Console ( Make sure that you select the relevant project from the top.

Step 2: Navigate to Credentials

Step 3: Click Create Credentials

Step 4: Click OAuth client ID to create client credentials

Step 5: Fill in the necessary details like application type, name, redirect URI and then create the credentials

Step 6: Once created, you will be prompted with the Client ID and Client Credentials. Copy them, you will need them later for integration.

Step 7: Enable the following APIs from the Enable APIs and Services Page

Authentication Flow

The Google OAuth 2.0 endpoint supports web server applications that use languages and frameworks such as PHP, Java, Python, Ruby, and ASP.NET (and Powershell)

The authorization sequence begins when your application redirects a browser to a Google URL; the URL includes query parameters that indicate the requested access type. Google handles the user authentication, session selection, and user consent. The result is an authorization code, which the application can exchange for access and refresh tokens.

The application should store the refresh token for future use and use the access token to access a Google API. Once the access token expires, the application uses the refresh token to obtain a new one.


Obtaining Refresh Token

This script will use the client id, client secret and redirect URI to obtain a “Refresh Token”.

Please note that this code will redirect you to Google URI to obtain an Authorisation Code. The Authorisation code will be returned along with the redirect URI. You must copy the Authorisation code from the redirect URI to the Powershell prompt to get the Refresh Token.

Running this code will prompt you to log in to Google Credentials. Once authenticated, you will redirect to your redirect URI with the Authentication Code appended to the end of the redirect URI.

Copy the Authentication code (text after “?Code=”) and paste it into the PowerShell console.

This will return the Refresh Token and Access Token and save it in a local file.

Please secure your Refresh Token appropriately.

Querying Data

Google Analytics Data API supports dimensions and metrics. The list of dimensions and metrics in the Analytics Data API queries can be found in

Step 1: Ensure you provide the client id, secret, and property.

Step 2: Ensure you provide the necessary dimensions and metrics per your requirement.

Recent Posts

See All


bottom of page