首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java /如何获取所有现有的关键字计划?

Java /如何获取所有现有的关键字计划?
EN

Stack Overflow用户
提问于 2021-11-19 21:44:03
回答 3查看 203关注 0票数 2

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

代码语言:javascript
复制
final long customerId = Long.valueOf("XXXXXXXXXX");    
GoogleAdsClient googleAdsClient = new ...
KeywordPlanServiceClient client = googleAdsClient.getVersion8().createKeywordPlanServiceClient();

String[] allExistingKeywordPlans = client. ???

代码语言:javascript
复制
<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

任何关于如何解决这个问题的提示都是非常感谢的!事先非常感谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-03-28 13:28:24

也许您可以尝试从您的帐户中获取keyword_plan资源。我就是这样为所有现有的keywordPlans创建删除操作的。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2022-03-29 05:43:30

如果您想删除所有现有的关键字计划,这是针对PHP的:

代码语言:javascript
复制
$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]);
}
票数 1
EN

Stack Overflow用户

发布于 2022-10-19 12:41:57

对于python:

代码语言:javascript
复制
import argparse
import sys

from google.ads.googleads.client import GoogleAdsClient
from google.ads.googleads.errors import GoogleAdsException


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'\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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70041224

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档