From c8f93125115910ab6998c4bcc9bb24d4155d6629 Mon Sep 17 00:00:00 2001 From: Nathan Moore Date: Thu, 12 Oct 2023 15:18:42 -0400 Subject: [PATCH] Added completions pivot table for data prior to Oct 4 change --- gdrive/analytics_api.py | 11 +++++--- gdrive/settings.py | 5 +++- gdrive/sheets_client.py | 56 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/gdrive/analytics_api.py b/gdrive/analytics_api.py index 4542d30..f4b15c5 100644 --- a/gdrive/analytics_api.py +++ b/gdrive/analytics_api.py @@ -87,7 +87,7 @@ async def run_analytics_task(start_date: datetime, end_date: datetime): sheets_id = export(analytics_df, start_date, end_date) do_analytics_export_post_processing(analytics_df, sheets_id=sheets_id) except Exception as e: - log.error(e.args) + log.error(e) async def list_accounts_task(): @@ -127,7 +127,7 @@ def export( """ filename_str = get_filename(date_of_report, end_date) analytics_folder_id = drive_client.create_folder( - "Google Analytics", parent_id=settings.ROOT_DIRECTORY + "Google Analytics", parent_id=settings.ANALYTICS_ROOT ) # We have to do this in multiple steps with more than one client because the Sheets API @@ -156,11 +156,14 @@ def do_analytics_export_post_processing(df: pd.DataFrame, sheets_id: str): page1 = "Rekrewt Pivot Table - First Visit" page2 = "Rekrewt Pivot Table - Sessions" page3 = "GSA Use Pivot Table" + page4 = "Completions" - new_sheet_name_to_id = sheets_client.add_new_pages([page1, page2, page3], sheets_id) + new_sheet_name_to_id = sheets_client.add_new_pages( + [page1, page2, page3, page4], sheets_id + ) log.info("Added %s pages to %s" % (len(new_sheet_name_to_id.keys()), sheets_id)) sheets_client.do_create_pivot_tables( - df, (page1, page2, page3), new_sheet_name_to_id, sheets_id + df, (page1, page2, page3, page4), new_sheet_name_to_id, sheets_id ) diff --git a/gdrive/settings.py b/gdrive/settings.py index 0eb4db0..ebe2aa1 100644 --- a/gdrive/settings.py +++ b/gdrive/settings.py @@ -15,7 +15,6 @@ LOG_LEVEL = os.getenv("LOG_LEVEL", logging.getLevelName(logging.INFO)) ANALYTICS = os.getenv("ANALYTICS", False) -ANALYTICS_PROPERTY_ID = os.getenv("ANALYTICS_PROPERTY_ID", 377091467) SCOPES = [ "https://www.googleapis.com/auth/analytics", @@ -27,6 +26,8 @@ ROOT_DIRECTORY = "" CODE_NAMES = None CREDENTIALS = None +ANALYTICS_ROOT = None +ANALYTICS_PROPERTY_ID = None ES_HOST = os.getenv("ES_HOST") ES_PORT = os.getenv("ES_PORT") @@ -49,6 +50,8 @@ log.info("Loading credentials from creds file") config = json.load(file) CREDENTIALS = config["credentials"] + ANALYTICS_ROOT = config["analytics_root"] + ANALYTICS_PROPERTY_ID = config["analytics_property_id"] ROOT_DIRECTORY = config["root_directory"] CODE_NAMES = config["code_names"] SHEETS_ID = config["sheets_id"] diff --git a/gdrive/sheets_client.py b/gdrive/sheets_client.py index 24e69a0..4b70d6a 100644 --- a/gdrive/sheets_client.py +++ b/gdrive/sheets_client.py @@ -209,6 +209,11 @@ def do_create_pivot_tables( "Added pivot table to %s (%s)" % (page_names[2], names_to_id[page_names[2]]) ) + create_feedback_pt(sheets_id, names_to_id[page_names[3]], col_dict) + log.info( + "Added pivot table to %s (%s)" % (page_names[3], names_to_id[page_names[3]]) + ) + update_cell_value(sheets_id, page_names[0], "A17", "Total First Visits") update_cell_value( sheets_id, @@ -550,6 +555,57 @@ def create_clicks_pt(sheets_id, page_id, col_dict): ) +def create_feedback_pt(sheets_id, page_id, col_dict): + add_pivot_tables( + sheets_id, + page_id, + ( + { + "pivotTable": { + "source": { + "sheetId": 0, + }, + "rows": [ + { + "sourceColumnOffset": col_dict["eventName"], + "showTotals": True, + "sortOrder": "ASCENDING", + }, + { + "sourceColumnOffset": col_dict["eventCount"], + "showTotals": True, + "sortOrder": "ASCENDING", + }, + ], + "filterSpecs": [ + { + "filterCriteria": { + "condition": { + "type": "TEXT_CONTAINS", + "values": [ + { + "userEnteredValue": "feedback", + } + ], + }, + "visibleByDefault": True, + }, + "columnOffsetIndex": col_dict["linkUrl"], + }, + ], + "values": [ + { + "summarizeFunction": "SUM", + "sourceColumnOffset": col_dict["eventCount"], + } + ], + "valueLayout": "HORIZONTAL", + } + } + ), + ) + + def upload_participant( first, last,