|
| 1 | +#!/usr/bin/env python |
| 2 | +# Copyright 2025 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 illustrates how to generate audience insights.""" |
| 16 | + |
| 17 | +import argparse |
| 18 | +import argparse |
| 19 | +import sys |
| 20 | + |
| 21 | +from google.ads.googleads.client import GoogleAdsClient |
| 22 | +from google.ads.googleads.errors import GoogleAdsException |
| 23 | + |
| 24 | + |
| 25 | +def main(client, customer_id, custom_name): |
| 26 | + """The main method that creates all necessary entities for the example. |
| 27 | +
|
| 28 | + Args: |
| 29 | + client: an initialized GoogleAdsClient instance. |
| 30 | + customer_id: a client customer ID. |
| 31 | + custom_name: custom name to define audience. |
| 32 | + """ |
| 33 | + location_id = "2840" # US |
| 34 | + product_name = "Google" |
| 35 | + user_interest_category = "92948" # Technology |
| 36 | + # Initialize appropriate services. |
| 37 | + audience_insights_service = client.get_service("AudienceInsightsService") |
| 38 | + googleads_service = client.get_service("GoogleAdsService") |
| 39 | + |
| 40 | + audience_composition_insights( |
| 41 | + client, |
| 42 | + audience_insights_service, |
| 43 | + googleads_service, |
| 44 | + customer_id, |
| 45 | + location_id, |
| 46 | + user_interest_category, |
| 47 | + custom_name |
| 48 | + ) |
| 49 | + generate_suggested_targeting_insights( |
| 50 | + client, |
| 51 | + audience_insights_service, |
| 52 | + googleads_service, |
| 53 | + customer_id, |
| 54 | + location_id, |
| 55 | + custom_name |
| 56 | + ) |
| 57 | + list_audience_insights_attributes( |
| 58 | + client, |
| 59 | + audience_insights_service, |
| 60 | + customer_id, |
| 61 | + product_name, |
| 62 | + custom_name |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +# [START composition_insights] |
| 67 | +def audience_composition_insights( |
| 68 | + client, |
| 69 | + audience_insights_service, |
| 70 | + googleads_service, |
| 71 | + customer_id, |
| 72 | + location_id, |
| 73 | + user_interest, |
| 74 | + custom_name |
| 75 | +): |
| 76 | + """Returns a collection of attributes represented in an audience of interest. |
| 77 | +
|
| 78 | + Please refere here for more: |
| 79 | + https://developers.google.com/google-ads/api/data/codes-formats |
| 80 | +
|
| 81 | + Args: |
| 82 | + client: an initialized GoogleAdsClient instance. |
| 83 | + audience_insights_service: an initialized AudienceInsightsService |
| 84 | + instance. |
| 85 | + googleads_service: an initialized GoogleAds Service instance. |
| 86 | + customer_id: The customer ID for the audience insights service. |
| 87 | + location_id: The location ID for the audience of interest. |
| 88 | + user_interest: The criterion ID of the category. |
| 89 | + custom_name: custom defined name. |
| 90 | + """ |
| 91 | + request = client.get_type("GenerateAudienceCompositionInsightsRequest") |
| 92 | + request.customer_id = customer_id |
| 93 | + |
| 94 | + insights_info = client.get_type("InsightsAudienceAttributeGroup") |
| 95 | + attributes = client.get_type("AudienceInsightsAttribute") |
| 96 | + attributes.user_interest.user_interest_category = googleads_service.user_interest_path( |
| 97 | + customer_id, user_interest |
| 98 | + ) |
| 99 | + |
| 100 | + insights_info.attributes.append(attributes) |
| 101 | + request.audience.topic_audience_combinations.append(insights_info) |
| 102 | + |
| 103 | + location_info = client.get_type("LocationInfo") |
| 104 | + location_info.geo_target_constant = ( |
| 105 | + googleads_service.geo_target_constant_path(location_id) |
| 106 | + ) |
| 107 | + request.audience.country_locations.append(location_info) |
| 108 | + |
| 109 | + request.customer_insights_group = custom_name |
| 110 | + request.dimensions = ( |
| 111 | + "AFFINITY_USER_INTEREST", |
| 112 | + "IN_MARKET_USER_INTEREST", |
| 113 | + "YOUTUBE_CHANNEL", |
| 114 | + ) |
| 115 | + response = audience_insights_service.generate_audience_composition_insights( |
| 116 | + request=request |
| 117 | + ) |
| 118 | + print(response) |
| 119 | + # [END composition_insights] |
| 120 | + |
| 121 | + |
| 122 | +# [START targeted_insights] |
| 123 | +def generate_suggested_targeting_insights( |
| 124 | + client, |
| 125 | + audience_insights_service, |
| 126 | + googleads_service, |
| 127 | + customer_id, |
| 128 | + location_id, |
| 129 | + custom_name |
| 130 | +): |
| 131 | + """Returns a collection of targeting insights (e.g.targetable audiences) |
| 132 | + that are relevant to the requested audience. |
| 133 | +
|
| 134 | + Args: |
| 135 | + client: an initialized GoogleAdsClient instance. |
| 136 | + audience_insights_service: an initialized AudienceInsightsService |
| 137 | + instance. |
| 138 | + googleads_service: an initialized GoogleAds Service instance. |
| 139 | + customer_id: The customer ID for the audience insights service. |
| 140 | + location_id: The location ID for the audience of interest. |
| 141 | + custom_name: custom defined name. |
| 142 | + """ |
| 143 | + request = client.get_type("GenerateSuggestedTargetingInsightsRequest") |
| 144 | + |
| 145 | + request.customer_id = customer_id |
| 146 | + request.customer_insights_group = custom_name |
| 147 | + |
| 148 | + audience_definition = request.audience_definition |
| 149 | + location_info = client.get_type("LocationInfo") |
| 150 | + location_info.geo_target_constant = ( |
| 151 | + googleads_service.geo_target_constant_path(location_id) |
| 152 | + ) |
| 153 | + audience_definition.audience.country_locations.append(location_info) |
| 154 | + |
| 155 | + request.audience_definition = audience_definition |
| 156 | + response = audience_insights_service.generate_suggested_targeting_insights( |
| 157 | + request=request |
| 158 | + ) |
| 159 | + print(response) |
| 160 | + # [END targeted_insights] |
| 161 | + |
| 162 | + |
| 163 | +# [START insights_attributes] |
| 164 | +def list_audience_insights_attributes( |
| 165 | + client, audience_insights_service, customer_id, product_name, custom_name |
| 166 | +): |
| 167 | + """Searches for audience attributes that can be used to generate insights. |
| 168 | +
|
| 169 | + Args: |
| 170 | + client: an initialized GoogleAdsClient instance. |
| 171 | + audience_insights_service: an initialized AudienceInsightsService |
| 172 | + instance. |
| 173 | + customer_id: The customer ID for the audience insights service. |
| 174 | + product_name: The brand/product for which insights are expected. |
| 175 | + custom_name: custom defined name. |
| 176 | + """ |
| 177 | + request = client.get_type("ListAudienceInsightsAttributesRequest") |
| 178 | + |
| 179 | + request.customer_id = customer_id |
| 180 | + request.query_text = product_name |
| 181 | + category_dimension = client.enums.AudienceInsightsDimensionEnum.CATEGORY |
| 182 | + kg_dimension = client.enums.AudienceInsightsDimensionEnum.KNOWLEDGE_GRAPH |
| 183 | + request.dimensions = [category_dimension, kg_dimension] |
| 184 | + request.customer_insights_group = custom_name |
| 185 | + response = audience_insights_service.list_audience_insights_attributes( |
| 186 | + request=request |
| 187 | + ) |
| 188 | + for attribute in response.attributes: |
| 189 | + if attribute.dimension == 3: |
| 190 | + print(attribute.attribute.entity.knowledge_graph_machine_id) |
| 191 | + # [END insights_attributes] |
| 192 | + |
| 193 | + |
| 194 | +if __name__ == "__main__": |
| 195 | + parser = argparse.ArgumentParser(description="Create audience insights.") |
| 196 | + |
| 197 | + # The following argument(s) should be provided to run the example. |
| 198 | + parser.add_argument( |
| 199 | + "-c", |
| 200 | + "--customer_id", |
| 201 | + type=str, |
| 202 | + required=True, |
| 203 | + help="The Google Ads customer ID.", |
| 204 | + ) |
| 205 | + |
| 206 | + parser.add_argument( |
| 207 | + "-n", |
| 208 | + "--custom_name", |
| 209 | + type=str, |
| 210 | + required=True, |
| 211 | + help="Custom name to indentify audiences", |
| 212 | + ) |
| 213 | + parser.add_argument |
| 214 | + args = parser.parse_args() |
| 215 | + |
| 216 | + # GoogleAdsClient will read the google-ads.yaml configuration file in the |
| 217 | + # home directory if none is specified. |
| 218 | + googleads_client = GoogleAdsClient.load_from_storage(version="v19") |
| 219 | + |
| 220 | + try: |
| 221 | + main(googleads_client, args.customer_id, args.custom_name) |
| 222 | + except GoogleAdsException as ex: |
| 223 | + print( |
| 224 | + 'Request with ID "{}" failed with status "%s" and includes the ' |
| 225 | + "following errors:".format(ex.request_id, ex.error.code().name) |
| 226 | + ) |
| 227 | + for error in ex.failure.errors: |
| 228 | + print('\tError with message "{}".'.format(error.message)) |
| 229 | + if error.location: |
| 230 | + for field_path_element in error.location.field_path_elements: |
| 231 | + print( |
| 232 | + "\t\tOn field: {}".format(field_path_element.field_name) |
| 233 | + ) |
| 234 | + sys.exit(1) |
0 commit comments