[Python] Google DV360 Reporting - Error while fetching performance metrices

Discussão em 'Python' iniciado por Stack, Dezembro 8, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to extract and download reports from DV360 API using python. I have gone through the documentation https://developers.google.com/bid-manager/guides/get-started/send-request ,but not able to fetch the performance metrics like impressions,clicks,revenue. I have tried the below code,

    from google.oauth2 import service_account
    from googleapiclient.discovery import build

    SCOPES = ['https://www.googleapis.com/auth/display-video']
    SERVICE_ACCOUNT_FILE = 'client_secret.json'

    credentials = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES
    )

    service = build('displayvideo', 'v1', credentials=credentials)

    def create_report_query(filters=None):
    query_body = {
    "params": {
    "type": "STANDARD",
    "groupBys": [
    "FILTER_ADVERTISER_NAME",
    "FILTER_ADVERTISER",
    "FILTER_ADVERTISER_CURRENCY",
    "FILTER_INSERTION_ORDER_NAME",
    "FILTER_INSERTION_ORDER",
    "FILTER_LINE_ITEM_NAME",
    "FILTER_LINE_ITEM",
    ],
    "filters": filters if filters else [],
    "metrics": [
    "METRIC_IMPRESSIONS",
    "METRIC_BILLABLE_IMPRESSIONS",
    "METRIC_CLICKS",
    "METRIC_CTR",
    "METRIC_TOTAL_CONVERSIONS",
    "METRIC_LAST_CLICKS",
    "METRIC_LAST_IMPRESSIONS",
    "METRIC_REVENUE_ADVERTISER",
    "METRIC_MEDIA_COST_ADVERTISER",
    ],
    }
    }
    return query_body

    def run_query(service, advertiser_id, query_body):
    request = service.reports().createquery(advertiserId=advertiser_id, body=query_body)
    response = request.execute()
    print("Report created:", response)
    return response

    def download_report(service, report_id):
    # Assuming the report is ready and downloadable as CSV
    request = service.reports().getfile(reportId=report_id, fileId=0)
    response = request.execute()
    with open("report.csv", "wb") as f:
    f.write(response)
    print("Report downloaded as 'report.csv'")

    if __name__ == "__main__":
    # Replace with your Advertiser ID
    ADVERTISER_ID = "your_advertiser_id_here"

    query_body = create_report_query()

    report_response = run_query(service, ADVERTISER_ID, query_body)

    report_id = report_response.get("reportId")

    if report_id:
    download_report(service, report_id)


    for request = service.reports().createquery(advertiserId=advertiser_id, body=query_body) I'm receiving the following error as AttributeError: 'Resource' object has no attribute 'reports'

    Can anyone please help me to get this? Is there a way to fetch these metrices using DV360 API.

    Continue reading...

Compartilhe esta Página