如何在给定ClientID的情况下使用MMC和python广告词库获取活动详细信息

如何在给定ClientID的情况下使用MMC和python广告词库获取活动详细信息,python,google-ads-api,Python,Google Ads Api,下面是我使用python api从adwords获取所有活动和活动详细信息的基本代码。我正在使用MCC。那么,如果给定客户ID,我如何循环帐户中的所有活动并检索所有活动设置 import os import datetime from adspygoogle.adwords.AdWordsClient import AdWordsClient from adspygoogle.common import Utils from pprint import pprint google_servic

下面是我使用python api从adwords获取所有活动和活动详细信息的基本代码。我正在使用MCC。那么,如果给定客户ID,我如何循环帐户中的所有活动并检索所有活动设置

import os
import datetime
from adspygoogle.adwords.AdWordsClient import AdWordsClient
from adspygoogle.common import Utils
from pprint import pprint

google_service = 'https://adwords.google.com'
headers = {
      'email': 'test@gmail.com',
      'password': 'test',
      'userAgent': 'Test',
      'developerToken': 'xxxxxxxxxxx',
    }


google_service = 'https://adwords.google.com'
api_version = 'v201109'

client = AdWordsClient(headers=headers)
client.use_mcc = True
client.SetDebug=True
client.SetClientCustomerId='11111111111'
campaign_service = client.GetCampaignService(google_service, api_version)

我对Python库不太熟悉,但我快速查看了示例,发现奇怪的是,您应该开始使用的示例代码已经放在了reporting文件夹中。如果您查看示例,您应该能够了解如何提取所需内容

您需要浏览以找到所需的字段,还需要记住,许多活动设置需要通过其他服务获得:即、和


如果你需要更多的帮助,那么最好的求助地点是;在办公时间,您通常会收到Google AdWords API工程师的回复。

我对Python库不太熟悉,但我快速查看了示例,发现奇怪的是,您应该开始使用的示例代码已经放在了reporting文件夹中。如果您查看示例,您应该能够了解如何提取所需内容

您需要浏览以找到所需的字段,还需要记住,许多活动设置需要通过其他服务获得:即、和


如果你需要更多的帮助,那么最好的求助地点是;在办公时间内,您通常会收到Google AdWords API工程师的回复。

以下内容将起作用,并作为示例提供。这将返回MCC帐户下所有客户端ID的列表。从那里,您可以编写循环来对每个客户机Id执行某些功能

from googleads import adwords

def DisplayAccountTree(account, accounts, links, depth=0):
  prefix = '-' * depth * 2
  print '%s%s, %s' % (prefix, account['customerId'], account['name'])
  if account['customerId'] in links:
    for child_link in links[account['customerId']]:
      child_account = accounts[child_link['clientCustomerId']]
      DisplayAccountTree(child_account, accounts, links, depth + 1)

def main(client):
  # Initialize appropriate service.
  managed_customer_service = client.GetService(
      'ManagedCustomerService', version='v201506')

  # Construct selector to get all accounts.
  selector = {
      'fields': ['CustomerId', 'Name']
  }
  # Get serviced account graph.
  graph = managed_customer_service.get(selector)
  if 'entries' in graph and graph['entries']:
    # Create map from customerId to parent and child links.
    child_links = {}
    parent_links = {}
    if 'links' in graph:
      for link in graph['links']:
        if link['managerCustomerId'] not in child_links:
          child_links[link['managerCustomerId']] = []
        child_links[link['managerCustomerId']].append(link)
        if link['clientCustomerId'] not in parent_links:
          parent_links[link['clientCustomerId']] = []
        parent_links[link['clientCustomerId']].append(link)
    # Create map from customerID to account and find root account.
    accounts = {}
    root_account = None
    for account in graph['entries']:
      accounts[account['customerId']] = account
      if account['customerId'] not in parent_links:
        root_account = account
    # Display account tree.
    if root_account:
      print 'CustomerId, Name'
      DisplayAccountTree(root_account, accounts, child_links, 0)
    else:
      print 'Unable to determine a root account'
  else:
    print 'No serviced accounts were found'

if __name__ == '__main__':
  # Initialize client object.
  adwords_client = adwords.AdWordsClient.LoadFromStorage()
  main(adwords_client)

以下内容将起作用,并作为中的示例提供。这将返回MCC帐户下所有客户端ID的列表。从那里,您可以编写循环来对每个客户机Id执行某些功能

from googleads import adwords

def DisplayAccountTree(account, accounts, links, depth=0):
  prefix = '-' * depth * 2
  print '%s%s, %s' % (prefix, account['customerId'], account['name'])
  if account['customerId'] in links:
    for child_link in links[account['customerId']]:
      child_account = accounts[child_link['clientCustomerId']]
      DisplayAccountTree(child_account, accounts, links, depth + 1)

def main(client):
  # Initialize appropriate service.
  managed_customer_service = client.GetService(
      'ManagedCustomerService', version='v201506')

  # Construct selector to get all accounts.
  selector = {
      'fields': ['CustomerId', 'Name']
  }
  # Get serviced account graph.
  graph = managed_customer_service.get(selector)
  if 'entries' in graph and graph['entries']:
    # Create map from customerId to parent and child links.
    child_links = {}
    parent_links = {}
    if 'links' in graph:
      for link in graph['links']:
        if link['managerCustomerId'] not in child_links:
          child_links[link['managerCustomerId']] = []
        child_links[link['managerCustomerId']].append(link)
        if link['clientCustomerId'] not in parent_links:
          parent_links[link['clientCustomerId']] = []
        parent_links[link['clientCustomerId']].append(link)
    # Create map from customerID to account and find root account.
    accounts = {}
    root_account = None
    for account in graph['entries']:
      accounts[account['customerId']] = account
      if account['customerId'] not in parent_links:
        root_account = account
    # Display account tree.
    if root_account:
      print 'CustomerId, Name'
      DisplayAccountTree(root_account, accounts, child_links, 0)
    else:
      print 'Unable to determine a root account'
  else:
    print 'No serviced accounts were found'

if __name__ == '__main__':
  # Initialize client object.
  adwords_client = adwords.AdWordsClient.LoadFromStorage()
  main(adwords_client)