In [6]:
#!/usr/bin/env python
from googleads import adwords

In [12]:
def get_ad_groups(client, campaign_id):
    ad_groups = []
    ad_group_service = client.GetService('AdGroupService', version='v201609')

    offset = 0
    query = "SELECT Id, BaseAdGroupId, Name, Status WHERE CampaignId= '{0}'".format(campaign_id)
    more_pages = True
    while more_pages:
        page = ad_group_service.query(query + ' LIMIT %s, %s' % (offset, PAGE_SIZE))
        if 'entries' in page:
            for ad_group in page['entries']:
                if ad_group['status'] == 'ENABLED':
                    ad_groups.append(str(ad_group['id']))
        offset += PAGE_SIZE
        more_pages = offset < int(page['totalNumEntries'])
    return ad_groups

In [13]:
adwords_client = adwords.AdWordsClient.LoadFromStorage()
adwords_client.SetClientCustomerId('644-709-8408')
PAGE_SIZE = 100

In [15]:
data = get_ad_groups(adwords_client, '874677791')

In [16]:
len(data)


Out[16]:
80

In [ ]: