Skip to content

Commit 3c59584

Browse files
authored
Add new generate_historical_metrics example. (#620)
1 parent 5b7336b commit 3c59584

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/usr/bin/env python
2+
# Copyright 2022 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
"""This example generates historical metrics for a keyword plan.
16+
17+
To create a keyword plan, run the add_keyword_plan.py example.
18+
"""
19+
20+
21+
import argparse
22+
import sys
23+
24+
from google.ads.googleads.client import GoogleAdsClient
25+
from google.ads.googleads.errors import GoogleAdsException
26+
27+
28+
# [START generate_historical_metrics]
29+
def main(client, customer_id, keyword_plan_id):
30+
"""The main method that creates all necessary entities for the example.
31+
32+
Args:
33+
client: an initialized GoogleAdsClient instance.
34+
customer_id: a client customer ID.
35+
keyword_plan_id: the ID for a keyword plan.
36+
"""
37+
keyword_plan_service = client.get_service("KeywordPlanService")
38+
resource_name = keyword_plan_service.keyword_plan_path(
39+
customer_id, keyword_plan_id
40+
)
41+
42+
response = keyword_plan_service.generate_historical_metrics(
43+
keyword_plan=resource_name
44+
)
45+
46+
for metric in response.metrics:
47+
# These metrics include those for both the search query and any close
48+
# variants included in the response.
49+
print(
50+
f"The search query, '{metric.search_query}', (and the following "
51+
f"variants: {', '.join(metrics.close_variants)}), generated the "
52+
"following historical metrics:"
53+
)
54+
55+
# Approximate number of monthly searches on this query averaged for
56+
# the past 12 months.
57+
print(
58+
f"\tApproximate monthly searches: {metric.keyword_metrics.avg_monthly_searches}."
59+
)
60+
61+
# The competition level for this search query.
62+
print(
63+
f"\tCompetition level: {metric.keyword_metrics.competition.name}."
64+
)
65+
66+
# The competition index for the query in the range [0, 100]. This shows
67+
# how competitive ad placement is for a keyword. The level of
68+
# competition from 0-100 is determined by the number of ad slots filled
69+
# divided by the total number of ad slots available. If not enough data
70+
# is available, None will be returned.
71+
print(
72+
f"\tCompetition index: {metric.keyword_metrics.competition_index}."
73+
)
74+
75+
# Top of page bid low range (20th percentile) in micros for the keyword.
76+
print(
77+
f"\tTop of page bid low range: {metric.keyword_metrics.low_top_of_page_bid_micros}."
78+
)
79+
80+
# Top of page bid high range (80th percentile) in micros for the keyword.
81+
print(
82+
f"\tTop of page bid high range: {metric.keyword_metrics.high_top_of_page_bid_micros}."
83+
)
84+
85+
# Approximate number of searches on this query for the past twelve months.
86+
for month in metric.keyword_metrics.monthly_search_volumes:
87+
print(
88+
f"\tApproximately {month.monthly_searches} searches in {month.month.name}, {month.year}."
89+
)
90+
# [END generate_historical_metrics]
91+
92+
93+
if __name__ == "__main__":
94+
# GoogleAdsClient will read the google-ads.yaml configuration file in the
95+
# home directory if none is specified.
96+
googleads_client = GoogleAdsClient.load_from_storage(version="v10")
97+
98+
parser = argparse.ArgumentParser(
99+
description="Generates historical metrics for a keyword plan."
100+
)
101+
# The following argument(s) should be provided to run the example.
102+
parser.add_argument(
103+
"-c",
104+
"--customer_id",
105+
type=str,
106+
required=True,
107+
help="The Google Ads customer ID.",
108+
)
109+
parser.add_argument(
110+
"-k",
111+
"--keyword_plan_id",
112+
type=str,
113+
required=True,
114+
help="A Keyword Plan ID.",
115+
)
116+
args = parser.parse_args()
117+
118+
try:
119+
main(googleads_client, args.customer_id, args.keyword_plan_id)
120+
except GoogleAdsException as ex:
121+
print(
122+
f'Request with ID "{ex.request_id}" failed with status '
123+
f'"{ex.error.code().name}" and includes the following errors:'
124+
)
125+
for error in ex.failure.errors:
126+
print(f'\tError with message "{error.message}".')
127+
if error.location:
128+
for field_path_element in error.location.field_path_elements:
129+
print(f"\t\tOn field: {field_path_element.field_name}")
130+
sys.exit(1)

0 commit comments

Comments
 (0)