How to create multiple custom dimensions in multiple GA4 properties automatically


In the previous article, “Automatic Creation of Multiple Properties in GA4,” I showed how to batch create GA4 properties. In this article, I will continue the process and show you how to batch create custom dimensions for each property.

The steps are as follows

  1. Create a list of custom dimensions
  2. Load the list of target GA4 properties (omitted this time)
  3. Load the list of custom dimensions and create a batch

Create a list of custom dimensions

Create a Google Sheet listing the custom dimensions you wish to create.

  • Sheet name: customdimensions
  • Column A: custom dimension name
  • Column B: Scope (EVENT, USER)

Create a script in Google Apps Script

Again, we will use the Google Analytics Admin API. Please refer to the previous article for details on how to set this up.

Import the list of target GA4 properties

Since this one only creates a list of GA4 properties in a Google Sheet and reads the sheet, I omit it this time and put it directly into an array variable in Google Apps Script.

Load a list of custom dimensions

Load the list of custom dimensions you just created into Google Sheet.

const FILEKEY ="abcdefghijklmno"; // a part of GoogleSheet's URL :

function getCustomDimensionsFromSheet(){
  var sheetName = "customdimensions";
  var spreadsheet = SpreadsheetApp.openById(FILEKEY);
  var sheet = spreadsheet.getSheetByName(sheetName);
  var lastRow = sheet.getLastRow(); 
  var ar = sheet.getRange(2,1,lastRow,2).getValues();
  var ret = [];
  for(var i =0; i < ar.length; i++){
    if(ar[i][0] == null || ar[i][0] ==""){
    var a = {
    "displayName": ar[i][0], 
    "parameterName": ar[i][0],
    "scope": ar[i][1] //"EVENT"
  return ret;

This function returns an array of variables including the following attributes Example

  • displayName: “customer_status”
  • parameterName: “customer_status”
  • scope: “USER”

Create custom dimensions in GA4 properties

The information obtained above is created one by one for each GA4 property. Here is a function that creates one custom dimension.

function createCustomDimension(propertyId, customDimensionData){
propertyId = 3234568;
customDimensionData= {
  "displayName": "customer_status",
  "parameterName": "customer_status",
  "scope": "USER" //"EVENT"
  var ret = AnalyticsAdmin.Properties.CustomDimensions.create(customDimensionData, 'properties/' + property);
  return ret;

Finally, repeat the above for the number of GA4 properties times the number of custom dimensions.

function main(){
  var properties = [
  ]; // 今回はGAプロパティIDの読み込み箇所のスクリプトは省略して配列に直接入れています
  for(const property of properties){
    var cds = getCustomDimensionsFromSheet(property);
    for(const cd of cds) {
        console.log(createCustomDimension(property, cd));

After executing the above function, you can see multiple custom dimensions in each GA4 property.