Skip to content

Commit

Permalink
Applied common styling to SQL in examples (#310)
Browse files Browse the repository at this point in the history
* Applied common styling to SQL in examples

* Addressed comments

* Addressed comments
  • Loading branch information
AndrewMBurke authored Sep 11, 2020
1 parent ad8a17b commit 036f93b
Show file tree
Hide file tree
Showing 35 changed files with 529 additions and 438 deletions.
46 changes: 23 additions & 23 deletions examples/account_management/get_account_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 30,17 @@
def resource_name_for_resource_type(resource_type, row):
"""Return the resource name for the resource type.
Each returned row contains all possible changed fields. This function
returns the resource name of the changed field based on the
resource type. The changed field's parent is also populated but is not used.
Each returned row contains all possible changed fields. This function
returns the resource name of the changed field based on the
resource type. The changed field's parent is also populated but is not used.
Args:
resource_type: the string equivalent of the resource type
row: a single row returned from the service
Args:
resource_type: the string equivalent of the resource type
row: a single row returned from the service
Returns:
The resource name of the field that changed.
"""
Returns:
The resource name of the field that changed.
"""
resource_name = "" # default for UNSPECIFIED or UNKNOWN
if resource_type == "AD_GROUP":
resource_name = row.change_status.ad_group
Expand All @@ -57,20 57,20 @@ def resource_name_for_resource_type(resource_type, row):

def main(client, customer_id):
ads_service = client.get_service("GoogleAdsService", version="v5")
query = (
"SELECT change_status.resource_name, "
"change_status.last_change_date_time, "
"change_status.resource_type, "
"change_status.campaign, "
"change_status.ad_group, "
"change_status.resource_status, "
"change_status.ad_group_ad, "
"change_status.ad_group_criterion, "
"change_status.campaign_criterion "
"FROM change_status "
"WHERE change_status.last_change_date_time DURING LAST_7_DAYS "
"ORDER BY change_status.last_change_date_time"
)
query = """
SELECT
change_status.resource_name,
change_status.last_change_date_time,
change_status.resource_type,
change_status.campaign,
change_status.ad_group,
change_status.resource_status,
change_status.ad_group_ad,
change_status.ad_group_criterion,
change_status.campaign_criterion
FROM change_status
WHERE change_status.last_change_date_time DURING LAST_7_DAYS
ORDER BY change_status.last_change_date_time"""

response = ads_service.search(
customer_id, query=query, page_size=ADS_PAGE_SIZE
Expand Down
23 changes: 11 additions & 12 deletions examples/account_management/get_account_hierarchy.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 48,17 @@ def main(client, login_customer_id=None):

# Creates a query that retrieves all child accounts of the manager
# specified in search calls below.
query = """SELECT
customer_client.client_customer,
customer_client.level,
customer_client.manager,
customer_client.descriptive_name,
customer_client.currency_code,
customer_client.time_zone,
customer_client.id
FROM
customer_client
WHERE
customer_client.level <= 1"""
query = """
SELECT
customer_client.client_customer,
customer_client.level,
customer_client.manager,
customer_client.descriptive_name,
customer_client.currency_code,
customer_client.time_zone,
customer_client.id
FROM customer_client
WHERE customer_client.level <= 1"""

try:
# If a Manager ID was provided in the customerId parameter, it will be
Expand Down
19 changes: 5 additions & 14 deletions examples/account_management/link_manager_to_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 16,10 @@


import argparse
import sys
import uuid
from datetime import datetime, timedelta

from google.api_core import protobuf_helpers

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


def main(client, customer_id, manager_customer_id):
Expand Down Expand Up @@ -60,16 57,10 @@ def main(client, customer_id, manager_customer_id):
# the resource name for the link from the client side. Note that since we
# are filtering by resource_name, a unique identifier, only one
# customer_client_link resource will be returned in the response
query = """
SELECT
customer_client_link.manager_link_id
FROM
customer_client_link
WHERE
customer_client_link.resource_name = "{}"
""".format(
resource_name
)
query = f"""
SELECT customer_client_link.manager_link_id
FROM customer_client_link
WHERE customer_client_link.resource_name = '{resource_name}'"""

ga_service = client.get_service("GoogleAdsService", version="v5")
response = ga_service.search(manager_customer_id, query=query)
Expand Down
41 changes: 15 additions & 26 deletions examples/advanced_operations/add_dynamic_page_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 23,7 @@
import argparse
import sys
import uuid
from datetime import datetime, timedelta

from google.api_core import protobuf_helpers

from google.ads.google_ads.client import GoogleAdsClient
Expand All @@ -32,6 32,7 @@

# Class to keep track of page feed details.
class FeedDetails(object):

def __init__(self, resource_name, url_attribute_id, label_attribute_id):
self.resource_name = resource_name
self.url_attribute_id = url_attribute_id
Expand Down Expand Up @@ -90,7 91,7 @@ def main(client, customer_id, campaign_id, ad_group_id):


def create_feed(client, customer_id):
"""Creates a page feed with URLs
"""Creates a page feed with URLs.
Args:
client: an initialized GoogleAdsClient instance.
Expand Down Expand Up @@ -141,17 142,10 @@ def get_feed_details(client, customer_id, resource_name):
A FeedDetails instance with information about the feed that was
retrieved in the search request.
"""
query = """
SELECT
feed.attributes
FROM
feed
WHERE
feed.resource_name = "{}"
LIMIT 1
""".format(
resource_name
)
query = f"""
SELECT feed.attributes
FROM feed
WHERE feed.resource_name = '{resource_name}'"""

ga_service = client.get_service("GoogleAdsService", version="v5")
response = ga_service.search(customer_id, query=query)
Expand All @@ -171,7 165,7 @@ def get_feed_details(client, customer_id, resource_name):


def create_feed_mapping(client, customer_id, feed_details):
"""Creates feed mapping using the given feed details
"""Creates feed mapping using the given feed details.
Args:
client: an initialized GoogleAdsClient instance.
Expand Down Expand Up @@ -219,7 213,7 @@ def create_feed_mapping(client, customer_id, feed_details):


def create_feed_items(client, customer_id, feed_details, label):
"""Creates feed items with the given feed_details and label
"""Creates feed items with the given feed_details and label.
Args:
client: an initialized GoogleAdsClient instance.
Expand Down Expand Up @@ -284,29 278,24 @@ def map_feed_urls(url):


def update_campaign_dsa_setting(client, customer_id, campaign_id, feed_details):
"""Updates the given campaign with the given feed details
"""Updates the given campaign with the given feed details.
Args:
client: an initialized GoogleAdsClient instance.
customer_id: a client customer ID str.
campaign_id: a campaign ID str;
feed_details: a FeedDetails instance with feed attribute information.
"""
query = """
query = f"""
SELECT
campaign.id,
campaign.name,
campaign.dynamic_search_ads_setting.domain_name,
campaign.dynamic_search_ads_setting.language_code,
campaign.dynamic_search_ads_setting.use_supplied_urls_only
FROM
campaign
WHERE
campaign.id = {}
LIMIT 1
""".format(
campaign_id
)
FROM campaign
WHERE campaign.id = {campaign_id}
LIMIT 1"""

ga_service = client.get_service("GoogleAdsService", version="v5")
results = ga_service.search(customer_id, query=query)
Expand Down Expand Up @@ -362,7 351,7 @@ def update_campaign_dsa_setting(client, customer_id, campaign_id, feed_details):


def add_dsa_targeting(client, customer_id, ad_group_resource_name, label):
"""Adds Dynamic Search Ad targeting criteria to the given ad group
"""Adds Dynamic Search Ad targeting criteria to the given ad group.
Args:
client: an initialized GoogleAdsClient instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 32,10 @@ def main(client, customer_id, page_size, campaign_id):
)

# First, retrieve all shared sets associated with the campaign.
shared_sets_query = (
"SELECT shared_set.id, shared_set.name FROM campaign_shared_set "
f"WHERE campaign.id = {campaign_id}"
)
shared_sets_query = f"""
SELECT shared_set.id, shared_set.name
FROM campaign_shared_set
WHERE campaign.id = {campaign_id}"""

try:
shared_set_response = ga_service.search(
Expand Down Expand Up @@ -64,12 64,14 @@ def main(client, customer_id, page_size, campaign_id):
sys.exit(1)

# Next, retrieve shared criteria for all found shared sets.
shared_criteria_query = (
"SELECT shared_criterion.type, shared_criterion.keyword.text, "
"shared_criterion.keyword.match_type, shared_set.id "
"FROM shared_criterion WHERE shared_set.id IN "
f'({", ".join(shared_set_ids)})'
)
shared_criteria_query = f"""
SELECT
shared_criterion.type,
shared_criterion.keyword.text,
shared_criterion.keyword.match_type,
shared_set.id
FROM shared_criterion
WHERE shared_set.id IN ({", ".join(shared_set_ids)})"""

try:
shared_criteria_response = ga_service.search(
Expand Down
16 changes: 9 additions & 7 deletions examples/advanced_operations/get_ad_group_bid_modifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 26,17 @@
def main(client, customer_id, page_size, ad_group_id=None):
ga_service = client.get_service("GoogleAdsService", version="v5")

query = (
"SELECT campaign.id, ad_group.id, "
"ad_group_bid_modifier.criterion_id, "
"ad_group_bid_modifier.bid_modifier, "
"ad_group_bid_modifier.device.type FROM ad_group_bid_modifier"
)
query = """
SELECT
campaign.id,
ad_group.id,
ad_group_bid_modifier.criterion_id,
ad_group_bid_modifier.bid_modifier,
ad_group_bid_modifier.device.type
FROM ad_group_bid_modifier"""

if ad_group_id:
query = "%s WHERE ad_group.id = %s" % (query, ad_group_id)
query = f" WHERE ad_group.id = {ad_group_id}"

results = ga_service.search(customer_id, query=query, page_size=page_size)

Expand Down
4 changes: 3 additions & 1 deletion examples/basic_operations/get_ad_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 26,9 @@
def main(client, customer_id, page_size, campaign_id=None):
ga_service = client.get_service("GoogleAdsService", version="v5")

query = "SELECT campaign.id, ad_group.id, ad_group.name FROM ad_group"
query = """
SELECT campaign.id, ad_group.id, ad_group.name
FROM ad_group"""

if campaign_id:
query = "%s WHERE campaign.id = %s" % (query, campaign_id)
Expand Down
16 changes: 11 additions & 5 deletions examples/basic_operations/get_artifact_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 46,17 @@ def main(client, artifact_name, page_size):
gaf_service = client.get_service("GoogleAdsFieldService", version="v5")

# Searches for an artifact with the specified name.
query = (
"SELECT name, category, selectable, filterable, sortable, "
"selectable_with, data_type, is_repeated "
"WHERE name = '%s'"
) % artifact_name
query = f"""
SELECT
name,
category,
selectable,
filterable,
sortable,
selectable_with,
data_type,
is_repeated
WHERE name = '{artifact_name}'"""

response = gaf_service.search_google_ads_fields(
query=query, page_size=page_size
Expand Down
8 changes: 4 additions & 4 deletions examples/basic_operations/get_campaigns.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 28,10 @@
def main(client, customer_id):
ga_service = client.get_service("GoogleAdsService", version="v5")

query = (
"SELECT campaign.id, campaign.name FROM campaign "
"ORDER BY campaign.id"
)
query = """
SELECT campaign.id, campaign.name
FROM campaign
ORDER BY campaign.id"""

# Issues a search request using streaming.
response = ga_service.search_stream(customer_id, query=query)
Expand Down
16 changes: 9 additions & 7 deletions examples/basic_operations/get_expanded_text_ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 28,15 @@ def main(client, customer_id, ad_group_id=None):
"AdGroupAdStatusEnum", version="v5"
).AdGroupAdStatus

query = (
"SELECT ad_group.id, ad_group_ad.ad.id, "
"ad_group_ad.ad.expanded_text_ad.headline_part1, "
"ad_group_ad.ad.expanded_text_ad.headline_part2, "
"ad_group_ad.status FROM ad_group_ad "
"WHERE ad_group_ad.ad.type = EXPANDED_TEXT_AD"
)
query = """
SELECT
ad_group.id,
ad_group_ad.ad.id,
ad_group_ad.ad.expanded_text_ad.headline_part1,
ad_group_ad.ad.expanded_text_ad.headline_part2,
ad_group_ad.status
FROM ad_group_ad
WHERE ad_group_ad.ad.type = EXPANDED_TEXT_AD"""

if ad_group_id:
query = f" AND ad_group.id = {ad_group_id}"
Expand Down
18 changes: 10 additions & 8 deletions examples/basic_operations/get_keywords.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 27,18 @@
def main(client, customer_id, page_size, ad_group_id=None):
ga_service = client.get_service("GoogleAdsService", version="v5")

query = (
"SELECT ad_group.id, ad_group_criterion.type, "
"ad_group_criterion.criterion_id, "
"ad_group_criterion.keyword.text, "
"ad_group_criterion.keyword.match_type FROM ad_group_criterion "
"WHERE ad_group_criterion.type = KEYWORD"
)
query = """
SELECT
ad_group.id,
ad_group_criterion.type,
ad_group_criterion.criterion_id,
ad_group_criterion.keyword.text,
ad_group_criterion.keyword.match_type
FROM ad_group_criterion
WHERE ad_group_criterion.type = KEYWORD"""

if ad_group_id:
query = "%s AND ad_group.id = %s" % (query, ad_group_id)
query = f" AND ad_group.id = {ad_group_id}"

results = ga_service.search(customer_id, query=query, page_size=page_size)

Expand Down
Loading

0 comments on commit 036f93b

Please sign in to comment.