【问题标题】:GoogleAds API - Java / How to get all existing Keyword Plans?GoogleAds API - Java / 如何获取所有现有的关键字计划?
【发布时间】:2022-10-19 20:41:57
【问题描述】:

我想出了如何创建和删除关键字计划,但我不知道如何获得所有现有关键字计划的列表(资源名称/计划 ID)?

final long customerId = Long.valueOf("XXXXXXXXXX");    
GoogleAdsClient googleAdsClient = new ...
KeywordPlanServiceClient client = googleAdsClient.getVersion8().createKeywordPlanServiceClient();

String[] allExistingKeywordPlans = client. ???

<dependency>
    <groupId>com.google.api-ads</groupId>
    <artifactId>google-ads</artifactId>
    <version>16.0.0</version>
</dependency>

更多资源: https://developers.google.com/google-ads/api/docs/samples/add-keyword-plan

非常感谢任何有关如何解决此问题的提示!提前谢谢了!

【问题讨论】:

  • 我面临同样的问题。你解决了这个问题吗?
  • 就我而言,我将这些引用存储在内部,以便稍后再次访问它们。如果您找到更方便的方法,请告诉我。

标签: java google-ads-api


【解决方案1】:

也许您可以尝试从您的帐户中获取关键字计划资源。 这就是我为所有现有关键字计划创建删除操作的方法。

GoogleAdsServiceClient.SearchPagedResponse response = client.search(SearchGoogleAdsRequest.newBuilder()
                    .setQuery("SELECT keyword_plan.resource_name FROM keyword_plan")
                    .setCustomerId(Objects.requireNonNull(googleAdsClient.getLoginCustomerId()).toString())
                    .build());
List<KeywordPlanOperation> keywordPlanOperations = response.getPage().getResponse().getResultsList().stream()
                    .map(x -> KeywordPlanOperation.newBuilder()
                            .setRemove(x.getKeywordPlan().getResourceName())
                            .build())
                    .collect(Collectors.toList());

当然,这也可以应用于您的用例。

【讨论】:

【解决方案2】:

如果您想删除所有现有的关键字计划,这适用于 PHP:

$googleAdsServiceClient = $this->googleAdsClient->getGoogleAdsServiceClient();

/** @var GoogleAdsServerStreamDecorator $stream */
$stream = $googleAdsServiceClient->searchStream(
    $linkedCustomerId,
    'SELECT keyword_plan.resource_name FROM keyword_plan'
);

$keywordPlanServiceClient = $this->googleAdsClient->getKeywordPlanServiceClient();

/** @var GoogleAdsRow $googleAdsRow */
foreach ($stream->iterateAllElements() as $googleAdsRow) {

    $keywordPlanOperation = new KeywordPlanOperation();
    $keywordPlanOperation->setRemove($googleAdsRow->getKeywordPlan()->getResourceName());
    $keywordPlanServiceClient->mutateKeywordPlans($this->linkedCustomerId, [$keywordPlanOperation]);
}

【讨论】:

    【解决方案3】:
    def main(client, customer_id):
        ga_service = client.get_service("GoogleAdsService")
        query = """
            SELECT keyword_plan.name, keyword_plan.id, 
            keyword_plan.forecast_period, keyword_plan.resource_name
             FROM keyword_plan
        """
    
        # Issues a search request using streaming.
        search_request = client.get_type("SearchGoogleAdsStreamRequest")
        search_request.customer_id = customer_id
        search_request.query = query
        stream = ga_service.search_stream(search_request)
        for batch in stream:
            for row in batch.results:
                resource_name = row.keyword_plan.resource_name
                forecast_period = row.keyword_plan.forecast_period
                id = row.keyword_plan.id
                name = row.keyword_plan.name
    
                print(
                    f'plan resource name "{resource_name}" with '
                    f'forecast period "{forecast_period.date_interval}" '
                    f"and ID {id} "
                    f' name "{name}" '
                )
    
    
    
    if __name__ == "__main__":
        # GoogleAdsClient will read the google-ads.yaml configuration file 
        in the
        # home directory if none is specified.
        googleads_client = GoogleAdsClient.load_from_storage(path='your- 
        google-ads.yml-file-path',version="v10")
    
        parser = argparse.ArgumentParser(
            description=("Retrieves a campaign's negative keywords.")
        )
        # The following argument(s) should be provided to run the example.
        parser.add_argument(
            "-c",
            "--customer_id",
            type=str,
            required=True,
            help="The Google Ads customer ID.",
        )
        args = parser.parse_args()
    
        try:
            main(googleads_client, args.customer_id)
        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'	Error with message "{error.message}".')
                if error.location:
                    for field_path_element in error.location.field_path_elements:
                        print(f"		On field: {field_path_element.field_name}")
            sys.exit(1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-14
      • 2018-02-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多