Google Keyword Planner

390 views
Skip to first unread message

Maciek eF

unread,
Mar 12, 2024, 8:28:08 AM3/12/24
to Google Ads API and AdWords API Forum
Hi
I'm trying to use keyword planner from API.

so something like this :

def generate_historical_metrics(client, customer_id):
    """Generates historical metrics and prints the results.

    Args:
        client: an initialized GoogleAdsClient instance.
        customer_id: a client customer ID.
    """
    googleads_service = client.get_service("GoogleAdsService")
    keyword_plan_idea_service = client.get_service("KeywordPlanIdeaService")
    request = client.get_type("GenerateKeywordHistoricalMetricsRequest")
    request.customer_id = customer_id
    request.keywords = ["mars cruise"]
    # Geo target constant 2840 is for USA.
    request.geo_target_constants.append(
        googleads_service.geo_target_constant_path("2840")
    )
    request.keyword_plan_network = (
        client.enums.KeywordPlanNetworkEnum.GOOGLE_SEARCH
    )
    # Language criteria 1000 is for English. For the list of language criteria
    # IDs, see:
    # https://developers.google.com/google-ads/api/reference/data/codes-formats#languages
    request.language = googleads_service.language_constant_path("1000")

    response = keyword_plan_idea_service.generate_keyword_historical_metrics(
        request=request
    )

    for result in response.results:
        metrics = result.keyword_metrics
        # These metrics include those for both the search query and any variants
        # included in the response.
        print(
            f"The search query '{result.text}' (and the following variants: "
            f"'{result.close_variants if result.close_variants else 'None'}'), "
            "generated the following historical metrics:\n"
        )

        # Approximate number of monthly searches on this query averaged for the
        # past 12 months.
        print(f"\tApproximate monthly searches: {metrics.avg_monthly_searches}")

        # The competition level for this search query.
        print(f"\tCompetition level: {metrics.competition}")

        # The competition index for the query in the range [0, 100]. This shows
        # how competitive ad placement is for a keyword. The level of
        # competition from 0-100 is determined by the number of ad slots filled
        # divided by the total number of ad slots available. If not enough data
        # is available, undef will be returned.
        print(f"\tCompetition index: {metrics.competition_index}")

        # Top of page bid low range (20th percentile) in micros for the keyword.
        print(
            f"\tTop of page bid low range: {metrics.low_top_of_page_bid_micros}"
        )

        # Top of page bid high range (80th percentile) in micros for the
        # keyword.
        print(
            "\tTop of page bid high range: "
            f"{metrics.high_top_of_page_bid_micros}"
        )

        # Approximate number of searches on this query for the past twelve
        # months.
        for month in metrics.monthly_search_volumes:
            print(
                f"\tApproximately {month.monthly_searches} searches in "
                f"{month.month.name}, {month.year}"
            )


and when I  try to run function:
generate_historical_metrics(client, customer_id)
it goes with that error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[48], line 1 ----> 1 generate_historical_metrics(client, customer_id) Cell In[30], line 12, in generate_historical_metrics(client, customer_id) 10 request = client.get_type("GenerateKeywordHistoricalMetricsRequest") 11 request.customer_id = customer_id ---> 12 request.keywords = ["mars cruise"] 13 # Geo target constant 2840 is for USA. 14 request.geo_target_constants.append( 15 googleads_service.geo_target_constant_path("2840") 16 ) AttributeError: Assignment not allowed to message, map, or repeated field "keywords" in protocol message object.

I'm stuck here and don't know what to do next.

I'll appriciate your help very much

Google Ads API Forum Advisor

unread,
Mar 12, 2024, 12:47:58 PM3/12/24
to maciek.f...@gmail.com, adwor...@googlegroups.com

Hi,

Thank you for contacting the Google Ads API support team.

After reviewing your concern, I understand that you are trying to generate historical metrics from the Google Ads API but getting errors. In order to address your issue, please provide us with complete API logs (request and response with request-id and request header) generated at your end.

Kindly note that you have to enable logging if you are using a client library. You can refer to the guides Java, .Net, PHP, Python, Ruby or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag. 

This message is in relation to case "ref:!00D1U01174p.!5004Q02rzsFm:ref" (ADR-00226957)

Thanks,
 
Google Logo Google Ads API Team


Maciek eF

unread,
Apr 3, 2024, 6:57:17 AM4/3/24
to Google Ads API and AdWords API Forum
Hi,
this is request and response

[2024-04-03 12:56:06,156 - INFO] Request ------- Method: /google.ads.googleads.v13.services.KeywordPlanService/MutateKeywordPlans Host: googleads.googleapis.com Headers: { "developer-token": "REDACTED", "login-customer-id": "7832449095", "x-goog-api-client": "gl-python/3.10.9 grpc/1.54.2 gax/2.11.0 gccl/21.1.0 pb/4.23.0", "x-goog-request-params": "customer_id=5974745636" } Request: customer_id: "5974745636" operations { create { name: "Keyword plan for traffic estimate ca4987f2-cf5c-4a09-9d85-0575b0969f8f" forecast_period { date_interval: NEXT_QUARTER } } } Response ------- Headers: { "google.ads.googleads.v13.errors.googleadsfailure-bin": "\nJ\n\u0002\b&\u0012DVersion v13 is deprecated. Requests to this version will be blocked.\u0012\u001687xXWcheyLMwQItRWy_JYA", "grpc-status-details-bin": "\b\u0003\u0012%Request contains an invalid argument.\u001a\u0001\nDtype.googleapis.com/google.ads.googleads.v13.errors.GoogleAdsFailure\u0012d\nJ\n\u0002\b&\u0012DVersion v13 is deprecated. Requests to this version will be blocked.\u0012\u001687xXWcheyLMwQItRWy_JYA", "request-id": "87xXWcheyLMwQItRWy_JYA" } Fault: errors { error_code { request_error: UNSUPPORTED_VERSION } message: "Version v13 is deprecated. Requests to this version will be blocked." } request_id: "87xXWcheyLMwQItRWy_JYA"

Google Ads API Forum Advisor

unread,
Apr 3, 2024, 5:10:43 PM4/3/24
to maciek.f...@gmail.com, adwor...@googlegroups.com

Hi,

Upon checking your provided error logs, I found that you are using Google Ads API version 13. Kindly note that v13 is deprecated as of June 7, 2023, and sunset was done at the end of January 2024. That means, after the end of January, if you hit the API with version 13, that request will not be accepted or blocked. That means after sunset, the respective versions will no longer be available. Please refer to Deprecation and Sunset to get more information about sunset dates for Google Ads API versions. 

So, I would suggest you upgrade your API to the latest version 16, and for more information, you can refer Upgrade to the latest version API documentation. 

Reply all
Reply to author
Forward
0 new messages