...
SAVED_QUERY_ID = '10000118344'
def main(client, saved_query_id): # Initialize appropriate service. report_service = client.GetService('ReportService', version='v201705')
# Initialize a DataDownloader. report_downloader = client.GetDataDownloader(version='v201705')
# Create statement object to filter for an order. values = [{ 'key': 'id', 'value': { 'xsi_type': 'NumberValue', 'value': saved_query_id } }] query = '' statement = dfp.FilterStatement(query, values)
response = report_service.getSavedQueriesByStatement( statement.ToStatement()) print (response)
....(SavedQueryPage){ totalResultSetSize = 0 startIndex = 0 }Failed to generate report. Error was: DFP report job failed. The ID of the failed report is: 10004293710Traceback (most recent call last): File "run_reach_report.py", line 75, in <module> main(dfp_client) File "run_reach_report.py", line 57, in main report_job_id, export_format, report_file)UnboundLocalError: local variable 'report_job_id' referenced before assignmentdef main(client): # Initialize a DataDownloader. report_downloader = client.GetDataDownloader(version='v201705')
# Create report job. report_job = { 'reportQuery': { 'dimensions': ['DATE', 'CREATIVE_NAME', 'PLACEMENT_NAME', 'DEVICE_CATEGORY_NAME', 'AGGREGATED_DEMAND_CHANNEL', 'LINE_ITEM_TYPE'], 'columns': ['TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS', 'TOTAL_LINE_ITEM_LEVEL_CLICKS', 'TOTAL_LINE_ITEM_LEVEL_ALL_REVENUE', 'TOTAL_ACTIVE_VIEW_MEASURABLE_IMPRESSIONS', 'TOTAL_ACTIVE_VIEW_VIEWABLE_IMPRESSIONS'], 'dateRangeType': 'YESTERDAY' } }#'dimensions': ['DATE', 'PLACEMENT_NAME', 'DEVICE_CATEGORY_NAME', 'AGGREGATED_DEMAND_CHANNEL', 'LINE_ITEM_TYPE'], try: # Run the report and wait for it to finish. report_job_id = report_downloader.WaitForReport(report_job) except errors.DfpReportError as e: print('Failed to generate report. Error was: %s' % e)#!/usr/bin/env python## Copyright 2015 Google Inc. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at### Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
"""Runs a report similar to the "Orders report" on the DFP website.
Includes additional attributes and can filter to include just one order."""
from datetime import datetimefrom datetime import timedeltaimport tempfile
# Import appropriate modules from the client library.from googleads import dfpfrom googleads import errors
ORDER_ID = 'INSERT_ORDER_ID_HERE'
def main(client, order_id): # Create statement object to filter for an order. values = [{ 'key': 'id', 'value': { 'xsi_type': 'NumberValue', 'value': order_id } }] filter_statement = {'query': 'WHERE ORDER_ID = :id', 'values': values}
# Set the start and end dates of the report to run (past 8 days). end_date = datetime.now().date() start_date = end_date - timedelta(days=8)
# Create report job. report_job = { 'reportQuery': { 'dimensions': ['ORDER_ID', 'ORDER_NAME'], 'dimensionAttributes': ['ORDER_TRAFFICKER', 'ORDER_START_DATE_TIME', 'ORDER_END_DATE_TIME'], 'columns': ['AD_SERVER_IMPRESSIONS', 'AD_SERVER_CLICKS', 'AD_SERVER_CTR', 'AD_SERVER_CPM_AND_CPC_REVENUE', 'AD_SERVER_WITHOUT_CPD_AVERAGE_ECPM'], 'dateRangeType': 'CUSTOM_DATE', 'startDate': start_date, 'endDate': end_date } }
# Initialize a DataDownloader. report_downloader = client.GetDataDownloader(version='v201705')
try: # Run the report and wait for it to finish. report_job_id = report_downloader.WaitForReport(report_job) except errors.DfpReportError as e: print ('Failed to generate report. Error was: %s' % e)
# Change to your preferred export format. export_format = 'CSV_DUMP'
report_file = tempfile.NamedTemporaryFile(suffix='.csv.gz', delete=False)
# Download report data. report_downloader.DownloadReportToFile( report_job_id, export_format, report_file)
report_file.close()
# Display results. print ('Report job with id \'%s\' downloaded to:\n%s' % ( report_job_id, report_file.name))
if __name__ == '__main__': # Initialize client object. dfp_client = dfp.DfpClient.LoadFromStorage() main(dfp_client, ORDER_ID) report_job = { 'reportQuery': { 'dimensions': ['DATE', 'PLACEMENT_NAME', 'DEVICE_CATEGORY_NAME', 'AGGREGATED_DEMAND_CHANNEL', 'LINE_ITEM_TYPE'], 'columns': ['TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS', 'TOTAL_LINE_ITEM_LEVEL_CLICKS', 'TOTAL_LINE_ITEM_LEVEL_ALL_REVENUE', 'TOTAL_ACTIVE_VIEW_MEASURABLE_IMPRESSIONS', 'TOTAL_ACTIVE_VIEW_VIEWABLE_IMPRESSIONS'], 'dateRangeType': 'YESTERDAY' } }#!/usr/bin/env python## Copyright 2015 Google Inc. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at### Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.
"""This code example runs a reach report."""
import osimport timeimport tempfileimport gzip
# Import appropriate modules from the client library.from googleads import dfpfrom googleads import errors
def main(client): # Initialize a DataDownloader. report_downloader = client.GetDataDownloader(version='v201705')
# Create report job. report_job = { 'reportQuery': { 'dimensions': ['DATE', 'PLACEMENT_NAME', 'DEVICE_CATEGORY_NAME', 'AGGREGATED_DEMAND_CHANNEL', 'LINE_ITEM_TYPE'], 'columns': ['TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS', 'TOTAL_LINE_ITEM_LEVEL_CLICKS', 'TOTAL_LINE_ITEM_LEVEL_ALL_REVENUE', 'TOTAL_ACTIVE_VIEW_MEASURABLE_IMPRESSIONS', 'TOTAL_ACTIVE_VIEW_VIEWABLE_IMPRESSIONS'], 'dateRangeType': 'YESTERDAY' } } try: # Run the report and wait for it to finish. report_job_id = report_downloader.WaitForReport(report_job) except errors.DfpReportError as e: print('Failed to generate report. Error was: %s' % e)
# Change to your preferred export format. export_format = 'XLSX'
report_file = tempfile.NamedTemporaryFile(suffix='.xlsx.gz', delete=False)
# Download report data. report_downloader.DownloadReportToFile( report_job_id, export_format, report_file)
report_file.close()
destination_file_name = "C:/GoogleServices/dfp/dfp_" + str(int(round(time.time()))) +".xlsx.gz" os.rename(report_file.name, destination_file_name) with gzip.GzipFile(destination_file_name, 'rb') as inF: with open(destination_file_name[:-3], 'wb') as outF: s = inF.read() outF.write(s)
os.remove(destination_file_name)
if __name__ == '__main__': # Initialize client object. dfp_client = dfp.DfpClient.LoadFromStorage() main(dfp_client)
Failed to generate report. Error was: DFP report job failed. The ID of the failed report is: 10004325334Traceback (most recent call last): File "run_reach_report.py", line 74, in <module> main(dfp_client) File "run_reach_report.py", line 56, in main report_job_id, export_format, report_file)UnboundLocalError: local variable 'report_job_id' referenced before assignment