Hi again,

Thanks for your reply. According to the documentation you shared 
<https://developers.google.com/google-ads/api/docs/keyword-planning/generate-forecast-metrics>
 the keyword 
forecasts seem to be available, but the documentation does not mention if 
this functionality is going to end after the 1st of June 2023. In order to 
clarify this, I have created the below Python script, can you please verify 
if the script is going to continue to provide the forecasts after the 
deprecation of the 1st of June 2023?

Thanks in advance


from google.ads.googleads.client import GoogleAdsClient
import os
import uuid
from time import sleep


def print_forecasts(googleads_client, keyword_plans):
    keywords_forecasts = []
    keyword_plan_service = 
googleads_client.get_service("KeywordPlanService")
    for keyword_plan in keyword_plans:
        broad_keyword_plan = keyword_plan["broad_keyword_plan"]
        exact_keyword_plan = keyword_plan["exact_keyword_plan"]
        broad_response = keyword_plan_service.generate_forecast_metrics(
            keyword_plan=broad_keyword_plan
        )
        sleep(5)
        exact_response = keyword_plan_service.generate_forecast_metrics(
            keyword_plan=exact_keyword_plan
        )
        keywords_forecasts.append({
            "keywordText": keyword_plan["keyword"],
            "broad_response": broad_response,
            "exact_response": exact_response,
        })

    for keyword in keywords_forecasts:
        print(f"Keyword: {keyword['keywordText']}")
        # BROAD
        for broad_forecast in keyword["broad_response"].keyword_forecasts:
            metrics = broad_forecast.keyword_forecast

            click_val = metrics.clicks
            clicks = f"{click_val:.2f}" if click_val else "unspecified"
            print(f"Broad Estimated total clicks: {clicks}")

            imp_val = metrics.impressions
            impressions = f"{imp_val:.2f}" if imp_val else "unspecified"
            print(f"Broad Estimated total impressions: {impressions}")

            cpc_val = metrics.average_cpc
            cpc = f"{cpc_val:.2f}" if cpc_val else "unspecified"
            print(f"Broad Estimated average cpc: {cpc}\n")
        # EXACT
        for exact_forecast in keyword["exact_response"].keyword_forecasts:
            metrics = exact_forecast.keyword_forecast

            click_val = metrics.clicks
            clicks = f"{click_val:.2f}" if click_val else "unspecified"
            print(f"Exact Estimated total clicks: {clicks}")

            imp_val = metrics.impressions
            impressions = f"{imp_val:.2f}" if imp_val else "unspecified"
            print(f"Exact Estimated total impressions: {impressions}")

            cpc_val = metrics.average_cpc
            cpc = f"{cpc_val:.2f}" if cpc_val else "unspecified"
            print(f"Exact Estimated average cpc: {cpc}\n")


def main_keyword_plans(
    googleads_client,
    keywords,
    geoTarget: int,
    languageConst: int,
    customer_id: str,
):
    keyword_plans = []
    for keyword in keywords:
        broad_keyword_plan = _add_keyword_plan(
            googleads_client, keywords, "BROAD", geoTarget, languageConst, 
customer_id
        )
        exact_keyword_plan = _add_keyword_plan(
            googleads_client, keywords, "EXACT", geoTarget, languageConst, 
customer_id
        )
        keyword_plans.append({
            "keyword": keyword,
            "broad_keyword_plan": broad_keyword_plan,
            "exact_keyword_plan": exact_keyword_plan
        })
    return keyword_plans


def _add_keyword_plan(
    googleads_client,
    keywords,
    matchType: str,
    geoTarget: int,
    languageConst: int,
    customer_id: str,
):
    keyword_plan = _create_keyword_plan(googleads_client, customer_id)
    keyword_plan_campaign = _create_keyword_plan_campaign(
        googleads_client, customer_id, keyword_plan, geoTarget, 
languageConst
    )
    keyword_plan_ad_group = _create_keyword_plan_ad_group(
        googleads_client, customer_id, keyword_plan_campaign
    )
    _create_keyword_plan_ad_group_keywords(
        googleads_client, customer_id, keyword_plan_ad_group, keywords, 
matchType
    )

    return keyword_plan


def _create_keyword_plan(googleads_client, customer_id):
    keyword_plan_service = 
googleads_client.get_service("KeywordPlanService")

    operation = googleads_client.get_type("KeywordPlanOperation")
    keyword_plan = operation.create

    keyword_plan.name = f"Keyword plan for traffic estimate {uuid.uuid4()}"

    forecast_interval = googleads_client.get_type(
        "KeywordPlanForecastIntervalEnum"
    ).KeywordPlanForecastInterval.NEXT_QUARTER
    keyword_plan.forecast_period.date_interval = forecast_interval

    response = keyword_plan_service.mutate_keyword_plans(
        customer_id=customer_id, operations=[operation]
    )
    global resource_name
    resource_name = response.results[0].resource_name

    return resource_name


def _create_keyword_plan_campaign(
    googleads_client,
    customer_id,
    keyword_plan,
    geoTarget: int,
    languageConst: int,
    cpc_bid: int = 86_000_000
):
    keyword_plan_campaign_service = googleads_client.get_service(
        "KeywordPlanCampaignService"
    )
    operation = googleads_client.get_type("KeywordPlanCampaignOperation")
    keyword_plan_campaign = operation.create

    keyword_plan_campaign.name = f"Keyword plan campaign {uuid.uuid4()}"
    keyword_plan_campaign.cpc_bid_micros = cpc_bid
    keyword_plan_campaign.keyword_plan = keyword_plan

    network = googleads_client.get_type(
        "KeywordPlanNetworkEnum"
    ).KeywordPlanNetwork.GOOGLE_SEARCH
    keyword_plan_campaign.keyword_plan_network = network

    geo_target = googleads_client.get_type("KeywordPlanGeoTarget")
    geo_target.geo_target_constant = f"geoTargetConstants/{geoTarget}"

    keyword_plan_campaign.geo_targets.append(geo_target)

    # Constant for English
    language = f"languageConstants/{languageConst}"
    keyword_plan_campaign.language_constants.append(language)

    response = keyword_plan_campaign_service.mutate_keyword_plan_campaigns(
        customer_id=customer_id, operations=[operation]
    )

    resource_name = response.results[0].resource_name

    return resource_name


def _create_keyword_plan_ad_group(googleads_client, customer_id, 
keyword_plan_campaign, cpc_bid: int = 86_000_000):
    operation = googleads_client.get_type("KeywordPlanAdGroupOperation")
    keyword_plan_ad_group = operation.create

    keyword_plan_ad_group.name = f"Keyword plan ad group {uuid.uuid4()}"
    keyword_plan_ad_group.cpc_bid_micros = cpc_bid
    keyword_plan_ad_group.keyword_plan_campaign = keyword_plan_campaign

    keyword_plan_ad_group_service = googleads_client.get_service(
        "KeywordPlanAdGroupService"
    )
    response = keyword_plan_ad_group_service.mutate_keyword_plan_ad_groups(
        customer_id=customer_id, operations=[operation]
    )

    resource_name = response.results[0].resource_name

    return resource_name


def _create_keyword_plan_ad_group_keywords(
    googleads_client, customer_id, plan_ad_group, keywords, matchType: str, 
cpc_bid: int = 86_000_000
):
    keyword_plan_ad_group_keyword_service = googleads_client.get_service(
        "KeywordPlanAdGroupKeywordService"
    )
    operations = []

    for i, keyword in enumerate(keywords):

        operation = 
googleads_client.get_type("KeywordPlanAdGroupKeywordOperation")
        globals()[f"keyword_plan_ad_group_keyword{i}"] = operation.create
        globals()[f"keyword_plan_ad_group_keyword{i}"].text = keyword
        globals()[f"keyword_plan_ad_group_keyword{i}"].cpc_bid_micros = 
cpc_bid

        if matchType == "EXACT":
            globals()[
                f"keyword_plan_ad_group_keyword{i}"
            ].match_type = googleads_client.get_type(
                "KeywordMatchTypeEnum"
            ).KeywordMatchType.EXACT
        elif matchType == "BROAD":
            globals()[
                f"keyword_plan_ad_group_keyword{i}"
            ].match_type = googleads_client.get_type(
                "KeywordMatchTypeEnum"
            ).KeywordMatchType.BROAD
        else:
            globals()[
                f"keyword_plan_ad_group_keyword{i}"
            ].match_type = googleads_client.get_type(
                "KeywordMatchTypeEnum"
            ).KeywordMatchType.PHRASE
        globals()[
            f"keyword_plan_ad_group_keyword{i}"
        ].keyword_plan_ad_group = plan_ad_group
        operations.append(operation)
    # todo:verify the order of the response is aligned with order of the 
list
    response = (
        
keyword_plan_ad_group_keyword_service.mutate_keyword_plan_ad_group_keywords(
            customer_id=customer_id, operations=operations
        )
    )


def main():
    # This is a static example with values
    gads_credentials = {
        "developer_token": os.getenv("GOOGLE_ADS_DEVELOPER_TOKEN"),
        "refresh_token": os.getenv("GOOGLE_ADS_REFRESH_TOKEN"),
        "client_id": os.getenv("GOOGLE_ADS_CLIENT_ID"),
        "client_secret": os.getenv("GOOGLE_ADS_CLIENT_SECRET"),
        "login_customer_id": os.getenv("GADS_CUSTOMERID"),
        "use_proto_plus": True,
    }
    googleads_client = GoogleAdsClient.load_from_dict(
        config_dict=gads_credentials, version="v13"
    )
    keywords = ["tofu", "vegan"]
    geo_target = 2826
    language_const = 1000
    customer_id = "XXXXXXXXXX"

    # start keyword plans
    keyword_plans = main_keyword_plans(
        googleads_client, keywords, geo_target, language_const, customer_id
    )

    # extract keyword forecasts
    print_forecasts(googleads_client, keyword_plans)


if __name__ == "__main__":
    main()
On Tuesday, May 16, 2023 at 10:02:13 AM UTC+3 Google Ads API Forum Advisor 
wrote:

> Hi,
>
> Thank you for the reply. 
>
> Unfortunately, our team does not comment on any Ads UI features or its 
> deprecation as the appropriate team such questions is the Google Ads 
> Product support team(*https://support.google.com/google-ads/gethelp* 
> <https://support.google.com/google-ads/gethelp>). However, as mentioned 
> in the above post, Ad group and keyword forecasts are being removed in the 
> Google Ads API. Let us know if you have further questions.
> Best regards,
>
> [image: Google Logo] Google Ads API Team 
>
> ref:_00D1U1174p._5004Q2lHJWr:ref
>

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to adwords-api@googlegroups.com
To unsubscribe from this group, send email to
adwords-api+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to adwords-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/0a6e2b31-873f-4879-aa16-b4c1590ac2c0n%40googlegroups.com.
  • Ke... dimitris pfx
    • ... dimitris pfx
      • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum

Reply via email to