googleads / googleads-python-lib

The Python client library for Google's Ads APIs
Apache License 2.0
681 stars 974 forks source link

How to properly use the API to generate a report? #525

Closed Shaharbad closed 1 year ago

Shaharbad commented 1 year ago

Hey everyone,

I'm having trouble with using the run_saved_report, I am able to execute other files but none of the report samples seemed to work for me. I'm using this file, and for some results is empty for me:

#!/usr/bin/env python
#
# Copyright 2016 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
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# 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 example runs a report from a saved query.
"""

# Import appropriate modules from the client library.
import tempfile

from googleads import ad_manager
from googleads import errors

# in my app i use a valid id, this is just a sample one
SAVED_QUERY_ID = '13876738231' 

def main(client, saved_query_id):
  # Initialize appropriate service.
  report_service = client.GetService('ReportService', version='v202308')

  # Initialize a DataDownloader.
  report_downloader = client.GetDataDownloader(version='v202308')

  # Create statement object to filter for an order.
  statement = (ad_manager.StatementBuilder(version='v202308')
               .Where('id = :id')
               .WithBindVariable('id', int(saved_query_id))
               .Limit(1))
  response = report_service.getSavedQueriesByStatement(
      statement.ToStatement())
  print(response['results'])
  if 'results' in response and len(response['results']):
    saved_query = response['results'][0]

    if saved_query['isCompatibleWithApiVersion']:
      report_job = {}

      # Set report query and optionally modify it.
      report_job['reportQuery'] = saved_query['reportQuery']

      try:
        # Run the report and wait for it to finish.
        report_job_id = report_downloader.WaitForReport(report_job)
      except errors.AdManagerReportError 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)
      print(report_file)
      # 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))
    else:
      print('The query specified is not compatible with the API version.')

if __name__ == '__main__':
  # Initialize client object.
  ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage()
  main(ad_manager_client, SAVED_QUERY_ID)
msaniscalchi commented 1 year ago

Hello,

We're a bit limited here in that we only investigate issues with the library itself. For questions more focused on troubleshooting the API, such as why it might return an empty report for your account, you would be better served by reaching out to the support forum.

Regards, Mark

christopherseeley commented 1 year ago

You can also check the Reporting guide on the developer docs: https://developers.google.com/ad-manager/api/reporting#faq