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 : https://docs.google.com/spreadsheets/d/abcdefghijklmno/edit#gid=0

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] ==""){
      break;
    }
    var a = {
    "displayName": ar[i][0], 
    "parameterName": ar[i][0],
    "scope": ar[i][1] //"EVENT"
    }
    ret.push(a);
  }
  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 = [
    3234568,
    3234555,
    3234542,
    3234609,
    3234553,
    3234563,
    3234584
  ]; // 今回はGAプロパティIDの読み込み箇所のスクリプトは省略して配列に直接入れています
  for(const property of properties){
    var cds = getCustomDimensionsFromSheet(property);
    for(const cd of cds) {
      try{
        console.log(createCustomDimension(property, cd));
      }catch(e){
        console.log(e);
      }
    }
  }
}

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

コメント

タイトルとURLをコピーしました