updateAllCustomDimensions.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:pyGACustomDimensions 作者: analyticsftw 项目源码 文件源码
def main():
  # Define the auth scopes to request.
  scope = ['https://www.googleapis.com/auth/analytics.readonly']

  # Refer to the config.py settings file for credentials
  service_account_email = config.apiSettings['service_account_email']
  key_file_location = config.apiSettings['key_file_location']

  propertyId = input('Enter the property ID (in the format UA-XXXXXXXX-Y): ')
  accountBits = propertyId.split('-')
  accountId = accountBits[1]
  isPremium = input('Is this a Premium property? (y/n): ')
  dimRange = 200 if isPremium == "y" else 20
  isActive = input('Leave all custom dimensions active? (y/n): ')
  dimActive = True if (isActive == "y") else False
  isType = input('Default dimension scope? (HIT, SESSION, USER, PRODUCT): ')

  # Authenticate and construct service.
  print("Connecting to Google Analytics API for authentication")
  service = get_service('analytics', 'v3', scope, key_file_location, service_account_email)

  print("Pulling dimensions")
  dimensions = service.management().customDimensions().list(
    accountId=accountId,
    webPropertyId=propertyId
  ).execute()

  time.sleep(1)  
  nbDims = dimensions.get("totalResults")
  print("Found " + str(nbDims) + " custom dims")
  print("Updating custom dimensions")
  scope = ['https://www.googleapis.com/auth/analytics.edit']
  service = get_service('analytics', 'v3', scope, key_file_location, service_account_email)
  for i in range(1,dimRange+1):
      # print("Deactivating custom dimension #"+ str(i))
      newDim = service.management().customDimensions().update(
        accountId=accountId,
        webPropertyId=propertyId,
        customDimensionId="ga:dimension" + str(i),
        body={
            'name':'Custom dimension #'+ str(i),
            'active': dimActive,
            'scope': isType
        }            
      ).execute()

      ''' Account for API quotas and pause every so often'''
      if i %10 == 0:
          time.sleep(5)
      progress(i,dimRange+1)
  print ("\n\nDone.")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号