Beispiel #1
0
def main(client, customer_id, campaign_id):
    ad_group_service = client.get_service('AdGroupService')
    campaign_service = client.get_service('CampaignService')

    # Create ad group.
    ad_group_operation = client.get_type('AdGroupOperation')
    ad_group = ad_group_operation.create
    ad_group.name.value = 'Earth to Mars cruises %s' % uuid.uuid4()
    ad_group.status = client.get_type('AdGroupStatusEnum').ENABLED
    ad_group.campaign.value = campaign_service.campaign_path(
        customer_id, campaign_id)
    ad_group.type = client.get_type('AdGroupTypeEnum').SEARCH_STANDARD
    ad_group.cpc_bid_micros.value = 10000000

    # Add the ad group.
    try:
        ad_group_response = ad_group_service.mutate_ad_groups(
            customer_id, [ad_group_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    print('Created ad group %s.' % ad_group_response.results[0].resource_name)
def main(client, customer_id, billing_setup_id):
    account_budget_proposal_service = client.get_service(
        'AccountBudgetProposalService')
    billing_setup_service = client.get_service('BillingSetupService',
                                               version='v1')

    account_budget_proposal_operation = client.get_type(
        'AccountBudgetProposalOperation')
    proposal = account_budget_proposal_operation.create

    proposal.proposal_type = client.get_type(
        'AccountBudgetProposalTypeEnum').CREATE
    proposal.billing_setup.value = billing_setup_service.billing_setup_path(
        customer_id, billing_setup_id)
    proposal.proposed_name.value = 'Account Budget Proposal (example)'

    # Specify the account budget starts immediately
    proposal.proposed_start_time_type = client.get_type('TimeTypeEnum',
                                                        version='v1').NOW
    # Alternatively you can specify a specific start time. Refer to the
    # AccountBudgetProposal resource documentation for allowed formats.
    #
    # proposal.proposed_start_date_time = '2020-01-02 03:04:05'

    # Specify that the budget runs forever
    proposal.proposed_end_time_type = client.get_type('TimeTypeEnum',
                                                      version='v1').FOREVER
    # Alternatively you can specify a specific end time. Allowed formats are as
    # above.
    #
    # proposal.proposed_end_date_time = '2021-01-02 03:04:05'

    # Optional: set notes for the budget. These are free text and do not effect
    # budget delivery.
    #
    # proposal.proposed_notes = client.wrapper
    #     .string('Received prepayment of $0.01')
    proposal.proposed_spending_limit_micros.value = 10000

    try:
        account_budget_proposal_response = (
            account_budget_proposal_service.mutate_account_budget_proposal(
                customer_id, account_budget_proposal_operation))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    print('Created account budget proposal "%s".' %
          account_budget_proposal_response.result.resource_name)
Beispiel #3
0
def main(client, customer_id, ad_group_id, number_of_ads):
    ad_group_ad_service = client.get_service("AdGroupAdService", version="v5")
    ad_group_service = client.get_service("AdGroupService", version="v5")

    ad_group_ad_operations = []

    for i in range(number_of_ads):

        # Create ad group ad.
        ad_group_ad_operation = client.get_type(
            "AdGroupAdOperation", version="v5"
        )
        ad_group_ad = ad_group_ad_operation.create
        ad_group_ad.ad_group = ad_group_service.ad_group_path(
            customer_id, ad_group_id
        )
        ad_group_ad.status = client.get_type(
            "AdGroupAdStatusEnum", version="v5"
        ).PAUSED

        # Set expanded text ad info
        ad_group_ad.ad.final_urls.append("http://www.example.com")
        ad_group_ad.ad.expanded_text_ad.description = "Buy your tickets now!"
        ad_group_ad.ad.expanded_text_ad.headline_part1 = "Cruise {} to Mars {}".format(
            i, str(uuid.uuid4())[:8]
        )
        ad_group_ad.ad.expanded_text_ad.headline_part2 = (
            "Best space cruise line"
        )
        ad_group_ad.ad.expanded_text_ad.path1 = "all-inclusive"
        ad_group_ad.ad.expanded_text_ad.path2 = "deals"

        ad_group_ad_operations.append(ad_group_ad_operation)

    try:
        ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
            customer_id, ad_group_ad_operations
        )
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print(
            'Request with ID "{}" failed with status "{}" and includes the '
            "following errors:".format(ex.request_id, ex.error.code().name)
        )
        for error in ex.failure.errors:
            print('\tError with message "{}".'.format(error.message))
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(
                        "\t\tOn field: {}".format(field_path_element.field_name)
                    )
        sys.exit(1)

    for result in ad_group_ad_response.results:
        print("Created ad group ad {}.".format(result.resource_name))
def main(client, customer_id, campaign_id, bid_modifier_value):
    campaign_service = client.get_service("CampaignService", version="v6")
    campaign_bm_service = client.get_service("CampaignBidModifierService",
                                             version="v6")

    # Create campaign bid modifier for call interactions with the specified
    # campaign ID and bid modifier value.
    campaign_bid_modifier_operation = client.get_type(
        "CampaignBidModifierOperation")
    campaign_bid_modifier = campaign_bid_modifier_operation.create

    # Set the campaign.
    campaign_bid_modifier.campaign = campaign_service.campaign_path(
        customer_id, campaign_id)

    # Set the bid modifier.
    campaign_bid_modifier.bid_modifier = bid_modifier_value

    # Sets the interaction type.
    campaign_bid_modifier.interaction_type.type = client.get_type(
        "InteractionTypeEnum", version="v6").CALLS

    # [START mutable_resource]
    # Add the campaign bid modifier. Here we pass the optional parameter
    # response_content_type=MUTABLE_RESOURCE so that the response contains
    # the mutated object and not just its resource name.
    try:
        campaign_bm_response = campaign_bm_service.mutate_campaign_bid_modifiers(
            customer_id,
            [campaign_bid_modifier_operation],
            response_content_type=client.get_type(
                "ResponseContentTypeEnum", version="v6").MUTABLE_RESOURCE,
        )
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print(f'Request with ID "{ex.request_id}" failed with status '
              f'"{ex.error.code().name}" and includes the following errors:')
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)

    # The resource returned in the response can be accessed directly in the
    # results list. Its fields can be read directly, and it can also be mutated
    # further and used in subsequent requests, without needing to make
    # additional Get or Search requests.
    mutable_resource = campaign_bm_response.results[0].campaign_bid_modifier
    print("Created campaign bid modifier with resource_name "
          f"'{mutable_resource.resource_name}', criterion ID "
          f"'{mutable_resource.criterion_id}', and bid modifier value "
          f"'{mutable_resource.bid_modifier}', under the campaign with "
          f"resource_name '{mutable_resource.campaign}', ")
Beispiel #5
0
    def test_get_service(self):
        # Retrieve service names for all defined service clients.
        service_names = [
            '%s%s' % (name.rsplit('ServiceClient')[0], 'Service')
            for name in dir(google.ads.google_ads.v0)
            if 'ServiceClient' in name
        ]

        client = self._create_test_client()

        # Iterate through retrieval of all service clients by name.
        for service_name in service_names:
            client.get_service(service_name)
def main(client, customer_id, ad_group_id):
    ad_group_service = client.get_service("AdGroupService", version="v6")
    ag_bm_service = client.get_service("AdGroupBidModifierService",
                                       version="v6")

    # Create ad group bid modifier based on hotel check-in day.
    check_in_ag_bm_operation = client.get_type("AdGroupBidModifierOperation",
                                               version="v6")
    check_in_ag_bid_modifier = check_in_ag_bm_operation.create
    check_in_ag_bid_modifier.hotel_check_in_day.day_of_week = client.get_type(
        "DayOfWeekEnum", version="v6").MONDAY
    check_in_ag_bid_modifier.ad_group = ad_group_service.ad_group_path(
        customer_id, ad_group_id)
    # Sets the bid modifier value to 150%.
    check_in_ag_bid_modifier.bid_modifier = 1.5

    # Create ad group bid modifier based on hotel length of stay info.
    los_ag_bm_operation = client.get_type("AdGroupBidModifierOperation",
                                          version="v6")
    los_ag_bid_modifier = los_ag_bm_operation.create
    los_ag_bid_modifier.ad_group = ad_group_service.ad_group_path(
        customer_id, ad_group_id)
    # Creates the hotel length of stay info.
    hotel_length_of_stay_info = los_ag_bid_modifier.hotel_length_of_stay
    hotel_length_of_stay_info.min_nights = 3
    hotel_length_of_stay_info.max_nights = 7
    # Sets the bid modifier value to 170%.
    los_ag_bid_modifier.bid_modifier = 1.7

    # Add the bid modifiers
    try:
        ag_bm_response = ag_bm_service.mutate_ad_groups(
            customer_id, [check_in_ag_bm_operation, los_ag_bm_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              "following errors:" % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    # Print out resource names of the added ad group bid modifiers.
    print("Added %d hotel ad group bid modifiers:" %
          len(ag_bm_response.results))

    for result in ag_bm_response.results:
        print(result.resource_name)
Beispiel #7
0
    def test_get_service(self):
        # Retrieve service names for all defined service clients.
        for ver in valid_versions:
            services_path = 'google.ads.google_ads.%s' % ver
            service_names = [
                '%s%s' % (name.rsplit('ServiceClient')[0], 'Service')
                for name in dir(import_module(services_path))
                if 'ServiceClient' in name
            ]

            client = self._create_test_client()

            # Iterate through retrieval of all service clients by name.
            for service_name in service_names:
                client.get_service(service_name)
def main(client, customer_id, campaign_id):
    campaign_service = client.get_service('CampaignService', version='v4')
    # Create campaign operation.
    campaign_operation = client.get_type('CampaignOperation', version='v4')
    campaign = campaign_operation.update
    campaign.resource_name = campaign_service.campaign_path(
        customer_id, campaign_id)
    campaign.status = client.get_type('CampaignStatusEnum',
                                      version='v4').PAUSED
    campaign.network_settings.target_search_network.value = False
    # Retrieve a FieldMask for the fields configured in the campaign.
    fm = protobuf_helpers.field_mask(None, campaign)
    campaign_operation.update_mask.CopyFrom(fm)

    # Update the campaign.
    try:
        campaign_response = campaign_service.mutate_campaigns(
            customer_id, [campaign_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    print('Updated campaign %s.' % campaign_response.results[0].resource_name)
Beispiel #9
0
def main(client, customer_id, billing_setup_id):
    billing_setup_service = client.get_service("BillingSetupService",
                                               version="v5")

    # Create billing setup operation.
    billing_setup_operation = client.get_type("BillingSetupOperation",
                                              version="v5")
    billing_setup_operation.remove = billing_setup_service.billing_setup_path(
        customer_id, billing_setup_id)

    # Remove the billing setup.
    try:
        billing_setup_response = billing_setup_service.mutate_billing_setup(
            customer_id, billing_setup_operation)
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              "following errors:" % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    print("Removed billing setup %s." %
          billing_setup_response.results[0].resource_name)
Beispiel #10
0
def main(client, customer_id):
    customer_service = client.get_service("CustomerService", version="v6")

    resource_name = customer_service.customer_path(customer_id)

    try:
        customer = customer_service.get_customer(resource_name=resource_name)
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print(
            'Request with ID "%s" failed with status "%s" and includes the '
            "following errors:" % (ex.request_id, ex.error.code().name)
        )
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    print("Customer ID: %d" % customer.id)
    print("\tDescriptive name: %s" % customer.descriptive_name)
    print("\tCurrency code: %s" % customer.currency_code)
    print("\tTime zone: %s" % customer.time_zone)
    print("\tTracking URL template: %s" % customer.tracking_url_template)
    print("\tAuto tagging enabled: %s" % customer.auto_tagging_enabled)
def main(client, customer_id, page_size, ad_group_id=None):
    ga_service = client.get_service('GoogleAdsService', version='v3')

    query = ('SELECT campaign.id, ad_group.id, '
             'ad_group_bid_modifier.criterion_id, '
             'ad_group_bid_modifier.bid_modifier, '
             'ad_group_bid_modifier.device.type FROM ad_group_bid_modifier')

    if ad_group_id:
        query = '%s WHERE ad_group.id = %s' % (query, ad_group_id)

    results = ga_service.search(customer_id, query=query, page_size=page_size)

    # Use the enum type to determine the enum name from the value.
    device_enum = client.get_type('DeviceEnum', version='v3').Device

    try:
        for row in results:
            print('Ad group bid modifier with criterion ID "%s", bid modifier '
                  'value "%s", device type "%s" was found in ad group ID "%s" '
                  'of campaign with ID "%s".' %
                  (row.ad_group_bid_modifier.criterion_id.value,
                   row.ad_group_bid_modifier.bid_modifier.value,
                   device_enum.Name(row.ad_group_bid_modifier.device.type),
                   row.ad_group.id.value, row.campaign.id.value))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)
Beispiel #12
0
def main(client, customer_id, page_size, ad_group_id=None):
    ga_service = client.get_service('GoogleAdsService', version='v2')

    query = ('SELECT ad_group.id, ad_group_ad.ad.id, '
             'ad_group_ad.ad.expanded_text_ad.headline_part1, '
             'ad_group_ad.ad.expanded_text_ad.headline_part2, '
             'ad_group_ad.status FROM ad_group_ad '
             'WHERE ad_group_ad.ad.type = EXPANDED_TEXT_AD')

    if ad_group_id:
        query = '%s AND ad_group.id = %s' % (query, ad_group_id)

    results = ga_service.search(customer_id, query=query, page_size=page_size)

    try:
        for row in results:
            ad = row.ad_group_ad.ad

            if ad.expanded_text_ad:
                expanded_text_ad_info = ad.expanded_text_ad

            print('Expanded text ad with ID %s, status %s, and headline '
                  '%s - %s was found in ad group with ID %s.' %
                  (ad.id, row.ad_group_ad.status,
                   expanded_text_ad_info.headline_part1,
                   expanded_text_ad_info.headline_part2, row.ad_group.id))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)
Beispiel #13
0
def add_hotel_ad(client, customer_id, ad_group_resource_name):
    ad_group_ad_service = client.get_service('AdGroupAdService', version='v3')

    # Creates a new ad group ad and sets the hotel ad to it.
    ad_group_ad_operation = client.get_type('AdGroupAdOperation', version='v3')
    ad_group_ad = ad_group_ad_operation.create
    ad_group_ad.ad_group.value = ad_group_resource_name
    # Set the ad group ad to enabled.  Setting this to paused will cause an error
    # for hotel campaigns.  For hotels pausing should happen at either the ad group or
    # campaign level.
    ad_group_ad.status = client.get_type('AdGroupAdStatusEnum',
                                         version='v3').ENABLED
    ad_group_ad.ad.hotel_ad.CopyFrom(client.get_type('HotelAdInfo',
                                                     version='v3'))

    # Add the ad group ad.
    try:
        ad_group_ad_response = ad_group_ad_service.mutate_ad_group_ads(
            customer_id, [ad_group_ad_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    ad_group_ad_resource_name = ad_group_ad_response.results[0].resource_name

    print('Created hotel ad %s.' % ad_group_ad_resource_name)

    return ad_group_resource_name
def main(client, customer_id, recommendation_id):
    recommendation_service = client.get_service("RecommendationService")

    dismiss_recommendation_request = client.get_type(
        "DismissRecommendationRequest")

    dismiss_recommendation_operation = (
        dismiss_recommendation_request.DismissRecommendationOperation())

    dismiss_recommendation_operation.resource_name = recommendation_service.recommendation_path(
        customer_id, recommendation_id)

    try:
        dismissal_response = recommendation_service.dismiss_recommendation(
            customer_id, [dismiss_recommendation_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              "following errors:" % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    print('Dismissed recommendation with resource name: "%s".' %
          dismissal_response.results[0].resource_name)
Beispiel #15
0
def main(client, customer_id, campaign_id, keyword, location_id):
    campaign_criterion_service = client.get_service('CampaignCriterionService')

    operations = [
        create_location_op(client, customer_id, campaign_id, location_id),
        create_negative_keyword_op(client, customer_id, campaign_id, keyword),
        create_proximity_op(client, customer_id, campaign_id)
    ]

    try:
        campaign_criterion_response = (
            campaign_criterion_service.mutate_campaign_criteria(
                customer_id, operations))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    for result in campaign_criterion_response.results:
        print('Added campaign criterion "%s".' % result.resource_name)
Beispiel #16
0
def main(client, manager_customer_id):
    customer_service = client.get_service('CustomerService', version='v3')
    customer = client.get_type('Customer', version='v3')
    today = datetime.today().strftime('%Y%m%d %H:%M:%S')
    customer.descriptive_name.value = ('Account created with '
                                       'CustomerService on %s' % today)
    # For a list of valid currency codes and time zones see this documentation:
    # https://developers.google.com/adwords/api/docs/appendix/codes-formats
    customer.currency_code.value = 'USD'
    customer.time_zone.value = 'America/New_York'
    # The below values are optional. For more information about URL
    # options see: https://support.google.com/google-ads/answer/6305348
    customer.tracking_url_template.value = '{lpurl}?device={device}'
    customer.final_url_suffix.value = (
        'keyword={keyword}&matchtype={matchtype}'
        '&adgroupid={adgroupid}')
    customer.has_partners_badge.value = False

    try:
        response = customer_service.create_customer_client(
            manager_customer_id, customer)
        print(
            ('Customer created with resource name "%s" under manager account '
             'with customer ID "%s"') %
            (response.resource_name, manager_customer_id))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)
Beispiel #17
0
def add_budget(client, customer_id):
    campaign_budget_service = client.get_service("CampaignBudgetService",
                                                 version="v6")

    # Create a budget, which can be shared by multiple campaigns.
    campaign_budget_operation = client.get_type("CampaignBudgetOperation",
                                                version="v6")
    campaign_budget = campaign_budget_operation.create
    campaign_budget.name = "Interplanetary Budget %s" % uuid.uuid4()
    campaign_budget.delivery_method = client.get_type(
        "BudgetDeliveryMethodEnum").STANDARD
    campaign_budget.amount_micros = 500000

    # Add budget.
    try:
        campaign_budget_response = campaign_budget_service.mutate_campaign_budgets(
            customer_id, [campaign_budget_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              "following errors:" % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    budget_resource_name = campaign_budget_response.results[0].resource_name

    print("Created budget %s" % budget_resource_name)

    return budget_resource_name
Beispiel #18
0
def main(client, customer_id, recommendation_id):
    recommendation_service = client.get_service("RecommendationService",
                                                version="v6")

    apply_recommendation_operation = client.get_type(
        "ApplyRecommendationOperation", version="v6")

    apply_recommendation_operation.resource_name = recommendation_service.recommendation_path(
        customer_id, recommendation_id)

    # Each recommendation type has optional parameters to override the
    # recommended values. Below is an example to override a recommended ad when
    # a TextAdRecommendation is applied. For details, please read:
    # https://developers.google.com/google-ads/api/reference/rpc/latest/ApplyRecommendationOperation

    # apply_recommendation_operation.text_ad.ad.id = int(INSERT_AD_ID_HERE)

    try:
        recommendation_response = recommendation_service.apply_recommendation(
            customer_id, [apply_recommendation_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              "following errors:" % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print("\t\tOn field: %s" % field_path_element.field_name)
        sys.exit(1)

    print('Applied recommendation with resource name: "%s".' %
          recommendation_response.results[0].resource_name)
Beispiel #19
0
def main(client, customer_id, page_size):
    ga_service = client.get_service('GoogleAdsService', version='v2')

    query = ('SELECT recommendation.type, recommendation.campaign, '
             'recommendation.text_ad_recommendation FROM recommendation '
             'WHERE recommendation.type = TEXT_AD')

    results = ga_service.search(customer_id, query=query, page_size=page_size)

    try:
        for row in results:
            recommendation = row.recommendation
            recommended_ad = recommendation.text_ad_recommendation.ad
            print('Recommendation ("%s") was found for campaign "%s".' %
                  (recommendation.resource_name, recommendation.campaign))

            if recommended_ad.display_url:
                print('\tDisplay URL = "%s"' % recommended_ad.display_url)

            for url in recommended_ad.final_urls:
                print('\tFinal URL = "%s"' % url)

            for url in recommended_ad.final_mobile_urls:
                print('\tFinal Mobile URL = "%s"' % url)
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)
def _create_budget(client, customer_id):
    campaign_budget_operation = client.get_type('CampaignBudgetOperation',
                                                version='v2')
    campaign_budget = campaign_budget_operation.create
    campaign_budget.name.value = f'Interplanetary Cruise Budget #{uuid4()}'
    campaign_budget.delivery_method = client.get_type(
        'BudgetDeliveryMethodEnum').STANDARD
    campaign_budget.amount_micros.value = 500000

    campaign_budget_service = client.get_service('CampaignBudgetService',
                                                 version='v2')

    try:
        campaign_budget_response = (
            campaign_budget_service.mutate_campaign_budgets(
                customer_id, [campaign_budget_operation]))
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print(f'Request with ID "{ex.request_id}" failed with status '
              f'"{ex.error.code().name}" and includes the following errors:')
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f'\t\tOn field: {field_path_element.field_name}')
        sys.exit(1)

    return campaign_budget_response.results[0].resource_name
Beispiel #21
0
def _add_remarketing_action(client, customer_id):
    remarketing_action_service = client.get_service(
        "RemarketingActionService", version="v5"
    )
    remarketing_action_operation = client.get_type(
        "RemarketingActionOperation", version="v5"
    )

    remarketing_action = remarketing_action_operation.create
    remarketing_action.name = f"Remarketing action #{uuid4()}"

    try:
        remarketing_action_response = remarketing_action_service.mutate_remarketing_actions(
            customer_id, [remarketing_action_operation]
        )
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print(
            f'Request with ID "{ex.request_id}" failed with status '
            f'"{ex.error.code().name}" and includes the following errors:'
        )
        for error in ex.failure.errors:
            print(f'\tError with message "{error.message}".')
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print(f"\t\tOn field: {field_path_element.field_name}")
        sys.exit(1)

    return remarketing_action_response.results[0].resource_name
Beispiel #22
0
def main(client, customer_id, recommendation_id):
    recommendation_service = client.get_service('RecommendationService',
                                                version='v4')

    apply_recommendation_operation = client.get_type(
        'ApplyRecommendationOperation')

    apply_recommendation_operation.resource_name = (
        recommendation_service.recommendation_path(customer_id,
                                                   recommendation_id))

    try:
        recommendation_response = recommendation_service.apply_recommendation(
            customer_id, [apply_recommendation_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    print('Applied recommendation with resource name: "%s".' %
          recommendation_response.results[0].resource_name)
def main(client, customer_id, ad_group_id, bid_micro_amount):
    ad_group_service = client.get_service('AdGroupService', version='v1')

    # Create ad group operation.
    ad_group_operation = client.get_type('AdGroupOperation', version='v1')
    ad_group = ad_group_operation.update
    ad_group.resource_name = ad_group_service.ad_group_path(
        customer_id, ad_group_id)
    ad_group.status = client.get_type('AdGroupStatusEnum', version='v1').PAUSED
    ad_group.cpc_bid_micros.value = bid_micro_amount
    fm = protobuf_helpers.field_mask(None, ad_group)
    ad_group_operation.update_mask.CopyFrom(fm)

    # Update the ad group.
    try:
        ad_group_response = ad_group_service.mutate_ad_groups(
            customer_id, [ad_group_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    print('Updated ad group %s.' % ad_group_response.results[0].resource_name)
Beispiel #24
0
def add_hotel_ad_group(client, customer_id, campaign_resource_name):
    ad_group_service = client.get_service('AdGroupService', version='v3')

    # Create ad group.
    ad_group_operation = client.get_type('AdGroupOperation', version='v3')
    ad_group = ad_group_operation.create
    ad_group.name.value = 'Earth to Mars cruise %s' % uuid.uuid4()
    ad_group.status = client.get_type('AdGroupStatusEnum', version='v3').ENABLED
    ad_group.campaign.value = campaign_resource_name
    # Sets the ad group type to HOTEL_ADS. This cannot be set to other types.
    ad_group.type = client.get_type('AdGroupTypeEnum', version='v3').HOTEL_ADS
    ad_group.cpc_bid_micros.value = 10000000

    # Add the ad group.
    try:
        ad_group_response = ad_group_service.mutate_ad_groups(
            customer_id, [ad_group_operation])
    except google.ads.google_ads.errors.GoogleAdsException as ex:
        print('Request with ID "%s" failed with status "%s" and includes the '
              'following errors:' % (ex.request_id, ex.error.code().name))
        for error in ex.failure.errors:
            print('\tError with message "%s".' % error.message)
            if error.location:
                for field_path_element in error.location.field_path_elements:
                    print('\t\tOn field: %s' % field_path_element.field_name)
        sys.exit(1)

    ad_group_resource_name = ad_group_response.results[0].resource_name

    print('Added a hotel ad group with resource name "%s".'
          % ad_group_resource_name)

    return ad_group_resource_name