Automatic creation of multiple properties in GA4

アナリティクス
スポンサーリンク

Universal Analytics (UA) had views, so you would have utilized views when you wanted to show only certain directories to certain people, etc. However, GA4 does not have views, so many people will create separate properties.
By utilizing Stream ID, it is possible to do things like views with Stream ID using the Data Portal without letting the viewer access GA4.

An airline that operates worldwide used to separate their web traffic data by country by “view”, but with GA4, they had to separate by property, creating about 20 GA4 properties and about 30 custom dimensions for each property.

スポンサーリンク

Create a script with Google Analytics Admin API and Google Apps Script

Even if you create each custom dimension in 20 seconds, it will take more than 3 hours, and it is quite tiring because there is a big possibility of missing a mistake if you make a mistake somewhere. So this time, we will use Google Apps Script to create them all at once using Google Analytics Admin API.
It is possible to do everything at once, but for the sake of clarity, we will separate the creation of properties and the creation of custom dimensions.
The steps are as follows

  1. Create a list of information needed for GA4 properties in Google Sheet
  2. Load the above in Google Apps Script and create multiple GA4 properties
  3. Create a list of GA4 properties and a list of custom dimensions in Google Sheet
  4. Load the above in Google Apps Script and create custom dimensions for each property

Create a list of information needed for GA4 properties in Google Sheet

Create a “properties” sheet in Google Sheet and enter the following in columns A through E of the sheet.

  • Column A: Account ID (* should be written as accounts/account ID)
  • Column B: Property name
  • Column C: Category (Refer to the list of categories and fill in the appropriate one. In this case, TRAVEL because it is a trip)
  • Column D: Time zone
  • Column E: Currency
  • Column F: Blank (the property ID under which the property was created should be entered here)

Google Apps Scriptでプロパティを自動生成

Create a function to read the information you just created in the “properties” sheet of Google Sheet.
You can easily get the information by *sheet.getRange(2,1,tmpLastRow-1,5).getValues(), but since I want to append the property IDs to Google Sheet after creating the properties, I read them one by one as follows.

const FILEKEY ="abcdefghijklmno"; // GoogleSheetのキー(URLの一部分) 例:https://docs.google.com/spreadsheets/d/abcdefghijklmno/edit#gid=0

function getPropetiesConfig(){
  var sheetName = 'properties';
  var retConfig = [];
  var spreadsheet = SpreadsheetApp.openById(FILEKEY);
  //  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheetByName(sheetName);
  var tmpLastRow = sheet.getLastRow();
//  var retConfig= sheet.getRange(2,1,tmpLastRow-1,5).getValues();
//  return retConfig;
  for(var i = 2;i <= sheet.getLastRow();i++){
   var val = sheet.getRange(i, 1).getValue();
    if (!val) {
      break;
    }else{
      var tmp ={};
      tmp['row'] = i;
      tmp['parent'] = sheet.getRange(i,1).getValue();
      tmp['displayName'] = sheet.getRange(i,2).getValue();
      tmp['industryCategory'] = sheet.getRange(i,3).getValue();
      tmp['timeZone'] = sheet.getRange(i,4).getValue();
      tmp['currencyCode'] = sheet.getRange(i,5).getValue();
      retConfig.push(tmp);
    }
  } 
    return retConfig;
}

GA4プロパティを作成

ServiceからGoogle Analytics Admin APIを追加

Google Analytics Admin API” is required to create GA4 properties, etc. Google Analytics Admin API is available by adding it from “Service” in GAS.

Search for “Google Analytics Admin API” from Services and add it. (Leave the Identifier as “AnalyticsAdmin”.

Once you have successfully added the Google Analytics Admin API, write the following function: AnalyticsAdmin.Properties.create function to create properties.
Reference: https://developers.google.com/analytics/devguides/config/admin/v1/rest/v1alpha/properties/create

function createProperty(parent, displayName, industryCategory, timeZone, currencyCode){
  try{
    var property = {
      "parent": parent,
      "displayName": displayName,
      "industryCategory": industryCategory,
      "timeZone": timeZone,
      "currencyCode":currencyCode,
    }
    var property = AnalyticsAdmin.Properties.create(property);
    return property;
  }catch(e){
    Logger.log(e.message);
    throw ("createPropery:" + displayName + "\te.message");
  }
}

作成したプロパティのIDを取得

After executing the above var property = AnalyticsAdmin.Properties.create(property), the property variable will contain the newly generated GA4 property, so you can get the property ID with property.name, but However, “properties/” must be removed because it is included in front of it.

GA4プロパティの作成を繰り返して、プロパティIDをGoogle Sheetに追記

Execute the createProperty function for the number of property IDs obtained by the getPropetiesConfig() function and append the results to the Google Sheet again.

function main(){
  var properties= getPropetiesConfig();
  var retProps = [];
  for(var i=0;i<properties.length;i++){
    var property = properties[i];
    property = createProperty(property);
    retProps.push(property);
    var propertyId = property.name.replace("properties/",""); // プロパティIDからproperties/を削除
    Logger.log(p.row +"\t" + p.displayName +"\t" + propertyId);
    setPropertyIdToSheet(p.row, propertyId);
  }
 // Logger.log(retProps);
}
/*
* 結果をpropertiesシートのF列に追記する
*/
function setPropertyIdToSheet(row, propertyId){
  var sheetName = 'properties';
  var retConfig = [];
  var spreadsheet = SpreadsheetApp.openById(FILEKEY);
  var sheet = spreadsheet.getSheetByName(sheetName);
  sheet.getRange(row, 6).setValue(propertyId);
  return true;
}

After executing the main function, you can confirm that the GA4 properties have already been created under the account you input in the sheet.

And you also confirmed that the property ID was appended to column F of the Google Sheet.

Property names, categories, time zones, and currencies are also properly reflected.

コメント

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