fillAllCustomDimensions.py 文件源码

python
阅读 27 收藏 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(10)  
  nbDims = dimensions.get("totalResults")
  print("Found " + str(nbDims) + " custom dims")
  if nbDims < dimRange+1:
      nbNewDims = dimRange - nbDims
      print("Creating " + str(nbNewDims) + " 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(nbDims+1,dimRange)
          newDim = service.management().customDimensions().insert(
            accountId=accountID,
            webPropertyId=propertyId,
            body={
                'name':"Spare custom dimension #"+ str(i),
                'scope': isType,
                'active': dimActive
            }            
          ).execute()
          if i %10 == 0:
              time.sleep(10)
          progress(i,nbNewDims)
      print "\n\nDone."
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号