def get_campaign():
    client = get_client()
    customer_id = '5397526643'
    ga_service = client.get_service('GoogleAdsService', version='v3')

    query = ('SELECT campaign.id, campaign.name FROM campaign '
             'ORDER BY campaign.id')

    # Issues a search request using streaming.
    response = ga_service.search_stream(customer_id, query=query)

    ads_model = ''

    try:
        for batch in response:
            for row in batch.results:
                print(f'Campaign with ID {row.campaign.id.value} and name '
                      f'"{row.campaign.name.value}" was found.')
    except 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 ads_model
def remove_campaigns(data):
    c = models.campaign.objects.get(model_id=data)
    ads_model = c.ads_model
    client = get_client()
    customer_id = '5397526643'

    campaign_service = client.get_service('CampaignService', version='v3')
    campaign_operation = client.get_type('CampaignOperation', version='v3')

    resource_name = campaign_service.campaign_path(customer_id, ads_model)
    campaign_operation.remove = resource_name

    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('Removed campaign %s.' % campaign_response.results[0].resource_name)
    c.delete()
    return None
Exemple #3
0
def enable_campaign(data):
    client = get_client()
    customer_id = '5397526643'

    m = models.campaign.objects.get(model_id=data)
    ads_model = m.ads_model

    campaign_service = client.get_service('CampaignService', version='v3')
    campaign_operation = client.get_type('CampaignOperation', version='v3')
    campaign = campaign_operation.update
    campaign.resource_name = campaign_service.campaign_path(
        customer_id, ads_model)
    campaign.status = client.get_type('CampaignStatusEnum',
                                      version='v3').ENABLED
    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)

    m.pause = 0
    m.approved = 1
    m.save()
    return None
Exemple #4
0
def update_campaign(data):
    client = get_client()
    customer_id = '5397526643'
    m = models.campaign.objects.get(model_id=data)
    ads_model = m.ads_model

    campaign_service = client.get_service('CampaignService', version='v3')
    campaign_operation = client.get_type('CampaignOperation', version='v3')
    campaign = campaign_operation.update
    campaign.resource_name = campaign_service.campaign_path(
        customer_id, ads_model)
    campaign.status = client.get_type('CampaignStatusEnum',
                                      version='v3').ENABLED
    campaign.network_settings.target_search_network.value = False

    start_time = datetime.date.today() + datetime.timedelta(days=1)
    campaign.start_date.value = datetime.date.strftime(start_time,
                                                       _DATE_FORMAT)

    if m.end_date != datetime.date.today() + datetime.timedelta(days=1):
        end_time = m.end_date
        campaign.end_date.value = datetime.date.strftime(
            end_time, _DATE_FORMAT)
    else:
        end_time = start_time + datetime.timedelta(weeks=4)
        campaign.end_date.value = datetime.date.strftime(
            end_time, _DATE_FORMAT)

    # Retrieve a FieldMask for the fields configured in the campaign.
    fm = protobuf_helpers.field_mask(None, campaign)
    campaign_operation.update_mask.CopyFrom(fm)

    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)

    ad_group_id = m.ad_group_id
    ad_group_service = client.get_service('AdGroupService', version='v3')
    ad_group_operation = client.get_type('AdGroupOperation', version='v3')
    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='v3').ENABLED

    # do the field updation here

    fm = protobuf_helpers.field_mask(None, ad_group)
    ad_group_operation.update_mask.CopyFrom(fm)

    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)

    m.update_pending = 0
    m.pause = 0
    m.approved = 1
    m.save()
    if m.keywords is not None:
        return add_keywords(client, customer_id, a[3],
                            campaign_details.keywords)
    else:
        return None