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 export the API generated report in the UI? #528

Closed Shaharbad closed 11 months ago

Shaharbad commented 11 months ago

Hey Everyone, can someone help me with loading the generated report in a table on the UI instead of downloading it? Here's my code:

<!DOCTYPE html>
<html>
<head>
    <title>Ad Manager Report Generator</title>
    <style>
        /* Reset default margin and padding for html and body elements */
        html, body {
            margin: 0;
            padding: 0;
            height: 100vh; /* Make sure the body takes up the full height of the viewport */
        }

        /* Set the background image to cover the entire screen */
        body {
            background-image: url("https://ssaitest.s3.us-west-2.amazonaws.com/screenshot.png");
            background-size: cover; /* Scale the image to cover the entire viewport */
            background-repeat: no-repeat; /* Prevent image repetition */
            background-attachment: fixed; /* Keep the image fixed in place */
        }

        /* Style for content */
        #content {
            text-align: center;
            padding: 20px;
            color: white; /* Change the text color to be visible on the image */
        }

        /* Modal styles */
        .modal-container {
            display: none;
            position: fixed;
            z-index: 1000;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            justify-content: center;
            align-items: center;
        }

        .modal-content {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            text-align: left;
        }

        /* Run button styles */
            #runButton{
            position: absolute;
            padding-left: 7px;
            padding-right: 7px;
            top: 26.7%; 
            right: 17.5%;
            cursor: pointer;
        }

        /* Form styles */
        #modalForm {
            display: none;
        }
    </style>
</head>
<body>
    <!-- Run button -->
    <button id="runButton" onclick="showModal()" style="background-color: white; border: 1px solid #0dcaf0; color: #0dcaf0;display: flex;justify-content: center;align-items: center; padding-top: 6px;padding-bottom: 6px;"><svg data-v-2aae0b34="" viewBox="0 0 16 16" width="1em" height="1em" focusable="false" role="img" aria-label="list" xmlns="http://www.w3.org/2000/svg" fill="currentColor" class="bi-list b-icon bi"><g data-v-2aae0b34=""><path fill-rule="evenodd" d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z"></path></g></svg></button>
    <div class="modal-container" id="modalContainer">
        <div class="modal-content">
            <h1>Ad Manager Report Generator</h1>
            <form method="POST" id="modalForm">
                <label for="start_date">Start Date:</label>
                <input type="date" id="start_date" name="start_date" required><br><br>

                <label for="end_date">End Date:</label>
                <input type="date" id="end_date" name="end_date" required><br><br>

                <label for="dimensions">Select Dimensions:</label><br>
                <input type="checkbox" id="dimensions" name="dimensions" value="DATE"> Date<br>
                <input type="checkbox" id="dimensions" name="dimensions" value="AD_UNIT_NAME"> AD Unit<br>
                <input type="checkbox" id="dimensions" name="dimensions" value="MOBILE_APP_NAME"> App Name<br>
                <input type="checkbox" id="dimensions" name="dimensions" value="MOBILE_APP_RESOLVED_ID"> Bundle ID<br>
                <input type="checkbox" id="dimensions" name="dimensions" value="BROWSER_NAME"> Device<br><br>

                <input type="submit" value="Generate Report">
                <!-- Hidden download button -->
                <a id="downloadButton" style="display: none;"></a>
            </form>
        </div>
    </div>

    <script>
        // Show the modal
        function showModal() {
            var modalContainer = document.getElementById('modalContainer');
            var modalForm = document.getElementById('modalForm');
            modalContainer.style.display = 'flex';
            modalForm.style.display = 'block';
        }
    </script>
</body>
</html>
from flask import Flask, request, render_template, jsonify
from datetime import date
from googleads import ad_manager
from googleads import errors

app = Flask(__name__)

CUSTOM_FIELD_ID = 'INSERT_CUSTOM_FIELD_ID'
CUSTOM_DIMENSION_KEY_ID = 'INSERT_CUSTOM_DIMENSION_KEY_ID'

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        start_date_str = request.form['start_date']
        end_date_str = request.form['end_date']
        dimensions = request.form.getlist('dimensions')

        # Parse the selected date strings into date objects
        start_date_parts = start_date_str.split('-')
        end_date_parts = end_date_str.split('-')
        start_date = date(int(start_date_parts[0]), int(start_date_parts[1]), int(start_date_parts[2]))
        end_date = date(int(end_date_parts[0]), int(end_date_parts[1]), int(end_date_parts[2]))

        # Initialize client object.
        ad_manager_client = ad_manager.AdManagerClient.LoadFromStorage()

        report_data = generate_report(ad_manager_client, CUSTOM_FIELD_ID, CUSTOM_DIMENSION_KEY_ID, start_date, end_date, dimensions)

        return jsonify(report_data)

    return render_template('index.html')

def generate_report(client, custom_field_id, custom_dimension_key_id, start_date, end_date, dimensions):
    report_downloader = client.GetDataDownloader(version='v202308')

    report_job = {
        'reportQuery': {
            'dimensions': dimensions,
            'adUnitView': 'FLAT',
            'columns': [
                'AD_EXCHANGE_LINE_ITEM_LEVEL_IMPRESSIONS',
                'AD_EXCHANGE_LINE_ITEM_LEVEL_REVENUE',
                'AD_EXCHANGE_LINE_ITEM_LEVEL_AVERAGE_ECPM',
                'AD_EXCHANGE_TOTAL_REQUESTS',
                'AD_EXCHANGE_MATCH_RATE',
                'CHILD_NETWORK_CODE'
            ],
            'dimensionAttributes': [],
            'customFieldIds': [],
            'cmsMetadataKeyIds': [],
            'customDimensionKeyIds': [],
            'startDate': start_date,
            'endDate': end_date,
            'dateRangeType': 'CUSTOM_DATE',
            'statement': "WHERE CHILD_NETWORK_CODE = 'xxxx'",
            'reportCurrency': 'USD',
        }
    }

    try:
        report_job_id = report_downloader.WaitForReport(report_job)
    except errors.AdManagerReportError as e:
        print('Failed to generate report. Error was: %s' % e)

    export_format = 'TSV'

    # Download the report as a bytestring.
    report_data = report_downloader.DownloadReport(report_job_id, export_format)

    # Decode the report data to a string.
    report_data = report_data.decode('utf-8')

    # Parse the report data into a list of dictionaries
    report_data_list = []
    lines = report_data.split('\n')
    headers = lines[0].split('\t')
    for line in lines[1:]:
        values = line.split('\t')
        if len(values) == len(headers):
            report_data_list.append(dict(zip(headers, values)))

    return report_data_list

if __name__ == '__main__':
    app.run(debug=True)
christopherseeley commented 11 months ago

The general approach would be to download the report and then parse it for display as you are doing. There is no API method for reading the report data directly, it must be downloaded as an intermediary.

Shaharbad commented 11 months ago

The general approach would be to download the report and then parse it for display as you are doing. There is no API method for reading the report data directly, it must be downloaded as an intermediary.

Are you familiar with a way to do so? I tried a few with no success

christopherseeley commented 11 months ago

There is example code here for running and downloading a report. Note that by default the file is gzipped, so you'll need to decompress it before reading it as a TSV.

Shaharbad commented 11 months ago

There is example code here for running and downloading a report. Note that by default the file is gzipped, so you'll need to decompress it before reading it as a TSV.

I meant a way to show it in the UI

Shaharbad commented 11 months ago

@christopherseeley Can you please help with this? I looked everywhere for a method to display it instead of downloading it.

christopherseeley commented 11 months ago

You must download the report and read the downloaded data to display it in a UI. There is no API method to get the data directly.

Shaharbad commented 11 months ago

@christopherseeley Thank you for that. My last question is whether there's a way to create a pricing rule using the API. I've been looking at the documentation for a while but haven't found a method.

christopherseeley commented 10 months ago

There is not currently a way to create pricing rules via the API.

Shaharbad commented 10 months ago

thanks

Root-FTW commented 3 months ago

There is not currently a way to create pricing rules via the API.

Sorry in advance if my question is silly:

I understand that creating new pricing rules is not possible, but modifying existing pricing rules is possible? or is there any future plan to have this option?

christopherseeley commented 3 months ago

There is no support in the API for reading or writing pricing rules, but it is being considered for future API versions.