How to use googleads - 10 common examples

To help you get started, we’ve selected a few googleads examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kmjennison / dfp-prebid-setup / tests_integration / helpers / archive_order_by_name.py View on Github external
def archive_order_by_name(order_name):
  """
  Archives an order by name in DFP.

  Args:
    order_name (str): the name of the DFP order to archive
  Returns:
    None
  """
  client = get_client()
  order_service = client.GetService('OrderService', version='v201908')

  statement = (ad_manager.StatementBuilder()
    .Where('name = :name')
    .WithBindVariable('name', order_name))
  response = order_service.performOrderAction(
    {'xsi_type': 'ArchiveOrders'},
    statement.ToStatement())
github googleads / googleads-python-lib / examples / ad_manager / v201905 / custom_targeting_service / update_custom_targeting_keys.py View on Github external
def main(client, key_id):
  # Initialize appropriate service.
  custom_targeting_service = client.GetService(
      'CustomTargetingService', version='v201905')

  statement = (ad_manager.StatementBuilder(version='v201905')
               .Where('id = :keyId')
               .WithBindVariable('keyId', int(key_id))
               .Limit(1))

  # Get custom targeting keys by statement.
  response = custom_targeting_service.getCustomTargetingKeysByStatement(
      statement.ToStatement())

  # Update each local custom targeting key object by changing its display name.
  if 'results' in response and len(response['results']):
    updated_keys = []
    for key in response['results']:
      if not key['displayName']:
        key['displayName'] = key['name']
      key['displayName'] += ' (Deprecated)'
      updated_keys.append(key)
github googleads / googleads-python-lib / examples / ad_manager / v201808 / contact_service / update_contacts.py View on Github external
def main(client, contact_id):
  # Initialize appropriate service.
  contact_service = client.GetService('ContactService', version='v201808')

  # Create statement object to select the single contact by ID.
  statement = (ad_manager.StatementBuilder(version='v201808')
               .Where('id = :id')
               .WithBindVariable('id', long(contact_id))
               .Limit(1))

  # Get contacts by statement.
  response = contact_service.getContactsByStatement(
      statement.ToStatement())

  if 'results' in response and len(response['results']):
    updated_contacts = []
    for contact in response['results']:
      contact['address'] = '123 New Street, New York, NY, 10011'
      updated_contacts.append(contact)

    # Update the contact on the server.
    contacts = contact_service.updateContacts(updated_contacts)
github googleads / googleads-python-lib / examples / ad_manager / v201811 / report_service / run_saved_query.py View on Github external
def main(client, saved_query_id):
  # Initialize appropriate service.
  report_service = client.GetService('ReportService', version='v201811')

  # Initialize a DataDownloader.
  report_downloader = client.GetDataDownloader(version='v201811')

  # Create statement object to filter for an order.
  statement = (ad_manager.StatementBuilder(version='v201811')
               .Where('id = :id')
               .WithBindVariable('id', int(saved_query_id))
               .Limit(1))

  response = report_service.getSavedQueriesByStatement(
      statement.ToStatement())

  if 'results' in response and len(response['results']):
    saved_query = response['results'][0]

    if saved_query['isCompatibleWithApiVersion']:
      report_job = {}

      # Set report query and optionally modify it.
      report_job['reportQuery'] = saved_query['reportQuery']
github googleads / googleads-python-lib / examples / dfp / v201711 / product_service / publish_programmatic_products_to_marketplace.py View on Github external
def main(client, product_id):
  # Initialize appropriate service.
  product_service = client.GetService('ProductService', version='v201711')

  # Create query.
  statement = (dfp.StatementBuilder()
               .Where('id = :id')
               .WithBindVariable('id', long(product_id))
               .Limit(1))

  products_published = 0

  # Get products by statement.
  while True:
    response = product_service.getProductsByStatement(
        statement.ToStatement())
    if 'results' in response:
      for product in response['results']:
        print ('Product with id "%s" and name "%s" will be '
               'published.' % (product['id'],
                               product['name']))
github googleads / googleads-python-lib / examples / dfp / v201802 / line_item_service / update_line_items.py View on Github external
def main(client, order_id):
  # Initialize appropriate service.
  line_item_service = client.GetService('LineItemService', version='v201802')

  # Create statement object to only select line items with even delivery rates.

  statement = (dfp.StatementBuilder()
               .Where(('deliveryRateType = :deliveryRateType AND '
                       'orderId = :orderId'))
               .WithBindVariable('orderId', long(order_id))
               .WithBindVariable('deliveryRateType', 'EVENLY')
               .Limit(500))

  # Get line items by statement.
  response = line_item_service.getLineItemsByStatement(
      statement.ToStatement())

  if 'results' in response and len(response['results']):
    # Update each local line item by changing its delivery rate type.
    updated_line_items = []
    for line_item in response['results']:
      if not line_item['isArchived']:
        line_item['deliveryRateType'] = 'AS_FAST_AS_POSSIBLE'
github googleads / googleads-python-lib / examples / adwords / v201802 / basic_operations / get_campaigns_with_awql.py View on Github external
def main(client):
  # Initialize appropriate service.
  campaign_service = client.GetService('CampaignService', version='v201802')

  # Construct query and get all campaigns.
  query = (adwords.ServiceQueryBuilder()
           .Select('Id', 'Name', 'Status')
           .Where('Status').EqualTo('ENABLED')
           .OrderBy('Name')
           .Limit(0, PAGE_SIZE)
           .Build())

  for page in query.Pager(campaign_service):
    # Display results.
    if 'entries' in page:
      for campaign in page['entries']:
        print ('Campaign with id "%s", name "%s", and status "%s" was '
               'found.' % (campaign['id'], campaign['name'],
                           campaign['status']))
    else:
      print 'No campaigns were found.'
github googleads / googleads-python-lib / examples / adwords / v201609 / reporting / download_criteria_report_with_awql.py View on Github external
'FROM CRITERIA_PERFORMANCE_REPORT '
                  'WHERE Status IN [ENABLED, PAUSED] '
                  'DURING LAST_7_DAYS')

  with open(path, 'w') as output_file:
    report_downloader.DownloadReportWithAwql(
        report_query, 'CSV', output_file, skip_report_header=False,
        skip_column_header=False, skip_report_summary=False,
        include_zero_impressions=True)

  print 'Report was downloaded to "%s".' % path


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

  main(adwords_client, PATH)
github googleads / googleads-python-lib / examples / adwords / v201609 / basic_operations / update_keyword.py View on Github external
if 'value' in ad_group_criteria:
    for criterion in ad_group_criteria['value']:
      if criterion['criterion']['Criterion.Type'] == 'Keyword':
        print ('Ad group criterion with ad group id \'%s\' and criterion id '
               '\'%s\' currently has bids:'
               % (criterion['adGroupId'], criterion['criterion']['id']))
        for bid in criterion['biddingStrategyConfiguration']['bids']:
          print '\tType: \'%s\', value: %s' % (bid['Bids.Type'],
                                               bid['bid']['microAmount'])
  else:
    print 'No ad group criteria were updated.'


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

  main(adwords_client, AD_GROUP_ID, CRITERION_ID)
github googleads / googleads-python-lib / examples / adwords / v201601 / campaign_management / validate_text_ad.py View on Github external
'displayUrl': 'example.com',
              'description1': 'Visit the Red Planet in style.',
              'description2': 'Low-gravity fun for all astronauts in orbit',
              'headline': 'Luxury Cruise to Mars'
          }
      }
  }]
  try:
    ad_group_ad_service.mutate(operations)
  except suds.WebFault, e:
    print 'Validation correctly failed with \'%s\'.' % str(e)


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

  main(adwords_client, AD_GROUP_ID)