簡體   English   中英

使用 Google Analytics 數據 API 在 GA4 屬性之間復制 Google Analytics 自定義定義/維度?

[英]Copy Google Analytics custom definitions/dimensions between GA4 properties with the Google Analytics Data API?

我有許多 GA4 Google Analytics 屬性需要共享相同的自定義維度(自定義定義)。 使用 web UI 手動復制它們速度慢且容易出錯。 我怎樣才能自動化這個過程?

使用Google Apps 腳本Google Analytics Data API v1我創建了一個幾乎可以做到的 function:

https://script.google.com/d/1Tcf7L4nxU5zAdFiLB2GmVe2R20g33aiy0mhUd8Y851JLipluznoAWpB_/edit?usp=sharing

但是 API 處於測試階段,似乎沒有辦法將尺寸添加到目標 GA 屬性。

有沒有人有辦法完成這最后一步?

function main(){
  gaSourcePropertyId = '12345678';
  gaDestinationPropertyId = '87654321';
  copyCustomDefinitions(gaSourcePropertyId, gaDestinationPropertyId);
}

function copyCustomDefinitions(gaSourcePropertyId, gaDestinationPropertyId) {
  let sourceDimensions = getCustomDimensions(gaSourcePropertyId);
  addCustomDimensions(gaDestinationPropertyId, sourceDimensions);
}

function getCustomDimensions(gaPropertyId) {
  var sourceDimensions = AnalyticsData.Properties.getMetadata(`properties/${gaPropertyId}/metadata`)['dimensions'];
  var sourceCustomDefinitions = [];
  sourceDimensions.forEach((dimension)=>{
    if (dimension['customDefinition'] == true){ sourceCustomDefinitions.push(dimension)}
  });
  return sourceCustomDefinitions;
}

function addCustomDimensions(gaPropertyId, dimensions) {
  const destinationDimensions = getCustomDimensions(gaPropertyId);
  const destinationDimensionApiNames = destinationDimensions.map(dimension=>dimension['apiName']);
  dimensions.forEach((dimension)=>{
    // Check if the new dimension already exists in the destination.
    if ( !destinationDimensionApiNames.includes(dimension['apiName']) ) {

      let newDimension = AnalyticsData.newDimension(dimension['apiName']); // The newDimension method doesn't seem to work yet!
      // TODO - add the dimension to the destination property
      console.warn('There is not yet an API function for adding dimensions to a property. Something like AnalyticsData.Properties.setMetadata is needed!');

    } else {
      console.info(`Dimension with apiName '${ dimension['apiName'] }' already present in destination! Dimension not added to destination`);
    }
  })
  
}




原來有Google Analytics Admin API ,它提供了操作屬性自定義維度的方法。

這包括列出自定義維度創建自定義維度的功能

function main(){
  gaSourcePropertyId = '1234';
  gaDestinationPropertyId = '4321'; 
  copyCustomDimensions(gaSourcePropertyId, gaDestinationPropertyId);
}

function copyCustomDimensions(gaSourcePropertyId, gaDestinationPropertyId) {
  let sourceDimensions = getCustomDimensions(gaSourcePropertyId);
  addCustomDimensions(gaDestinationPropertyId, sourceDimensions);
}

function getCustomDimensions(gaPropertyId) {
  try {
    return AnalyticsAdmin.Properties.CustomDimensions.list(`properties/${gaPropertyId}`)['customDimensions'];
  } catch (error) {
    console.error(error);
  }
}

function addCustomDimensions(gaPropertyId, dimensions) {
  let destinationCustomDimensions = getCustomDimensions(gaPropertyId);

  // The destination may not have any custom dimensions.
  if (typeof destinationCustomDimensions == 'undefined') {
    console.info(`Could not get custom definitions for property ID '${gaPropertyId}'.`, destinationCustomDimensions);
    destinationCustomDimensions = [];
  };

  const destinationDimensionParameterNames = destinationCustomDimensions.map(dimension=>dimension['parameterName']);
  
  dimensions.forEach((sourceDimension)=>{
    // Check if the new dimension already exists in the destination.
    if ( !destinationDimensionParameterNames.includes(sourceDimension['parameterName']) ) {
      let destinationDimension = { 
        "parameterName": sourceDimension['parameterName'], 
        "displayName": sourceDimension['displayName'], 
        "description": sourceDimension['description'], 
        "scope": sourceDimension['scope']
      };
      let result = null;
      try {
        result = AnalyticsAdmin.Properties.CustomDimensions.create(destinationDimension, `properties/${gaPropertyId}`);
        console.log('[COPIED]',result);
      } catch (error) {
        console.log('[FAILED]', destinationDimension)
        console.error(error);
      }
      
    } else {
      console.info(`[NO ACTION] Dimension with apiName '${ sourceDimension['parameterName'] }' already present in destination! Dimension not added to destination.`, sourceDimension);
    }
  });
  
}



暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM