importCSV.py 文件源码

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

项目:pyGACustomDimensions 作者: analyticsftw 项目源码 文件源码
def main():
    # 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']
    sourceCSV = 'inputs/cdims20.csv'
    propertyId = input('Enter the property ID (in the format UA-XXXXXXXX-Y): ')
    separator = input('Which separator are you using in the CSV file? (,/;): ')
    accountBits = propertyId.split('-')
    accountId = accountBits[1]

    print("Updating custom dimensions from CSV")
    scope = ['https://www.googleapis.com/auth/analytics.edit']
    service = get_service('analytics', 'v3', scope, key_file_location, service_account_email)

    with open(sourceCSV) as csvfile:
        numline = len(csvfile.readlines())
        csvfile.seek(0)
        myreader = csv.reader(csvfile, delimiter=separator)

        next(myreader)
        i = 0
        for row in myreader:
            customDimensionId = ("ga:dimension" + str(row[0]))
            newDim = service.management().customDimensions().update(
              accountId=accountId,
              webPropertyId=propertyId,
              customDimensionId= customDimensionId,
              body={  
                  'name':row[1],
                  'scope':row[2],
                  'active': row[3]
              }            
            ).execute()
            progress(i,numline)
            i += 1
        print ("\n\nDone.")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号