Skip to content

Commit

Permalink
Adding pivot table for linked.com
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-moore-97 committed Jan 22, 2024
1 parent 08b6dc7 commit 90c2d53
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
47 changes: 46 additions & 1 deletion gdrive/idva/flow_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def create_pages(sheets_id: str) -> dict:
names_to_id (dict): A Dictionary mapping string sheet names to IDs
"""
new_sheet_name_to_id = sheets_client.add_new_pages(
[SheetsEnum.REKREWT.value, SheetsEnum.GSA.value], sheets_id
[SheetsEnum.REKREWT.value, SheetsEnum.GSA.value], sheets_id, column_count=30
)
log.info("Added %s pages to %s" % (len(new_sheet_name_to_id.keys()), sheets_id))
return new_sheet_name_to_id
Expand All @@ -133,6 +133,7 @@ def create_pivot_tables(df: pd.DataFrame, names_to_id: dict, sheets_id: str):
reddit_pivot(sheets_id, names_to_id, col_dict)
twitter_x_pivot(sheets_id, names_to_id, col_dict)
linkedin_pivot(sheets_id, names_to_id, col_dict)
linked_pivot(sheets_id, names_to_id, col_dict)

sheets_client.add_pivot_tables(
sheets_id, names_to_id[SheetsEnum.GSA.value], idva.clicks(col_dict)
Expand Down Expand Up @@ -380,6 +381,50 @@ def linkedin_pivot(sheets_id, names_to_id, col_dict):
)


def linked_pivot(sheets_id, names_to_id, col_dict):
sheets_client.update_cell_value(
sheets_id, SheetsEnum.REKREWT.value, "AA5", "LINKED.COM"
) # Pivot table Label
sheets_client.update_cell_value(
sheets_id, SheetsEnum.REKREWT.value, "H1", "LINKED.COM"
) # Totals label

sheets_client.add_pivot_tables(
sheets_id,
names_to_id[SheetsEnum.REKREWT.value],
idva.linkedin(col_dict),
row_idx=5,
col_idx=26,
)

linkedin_sessions = FormulaBuilder(
FormulaEnum.GET_PIVOT_DATA,
params=[
StringLiteral("SUM of eventCount"),
"AA6",
StringLiteral("eventName"),
StringLiteral("session_start"),
],
)

linkedin_visit = FormulaBuilder(
FormulaEnum.GET_PIVOT_DATA,
params=[
StringLiteral("SUM of eventCount"),
"AA6",
StringLiteral("eventName"),
StringLiteral("first_visit"),
],
)

sheets_client.update_cell_value(
sheets_id, SheetsEnum.REKREWT.value, "H2", linkedin_sessions.render()
)
sheets_client.update_cell_value(
sheets_id, SheetsEnum.REKREWT.value, "H3", linkedin_visit.render()
)


def generate_filename(date: datetime, end_date: datetime = None):
"""
Return filename for the new spreadsheet to be saved as
Expand Down
48 changes: 48 additions & 0 deletions gdrive/idva/pivot_director.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,51 @@ def linkedin(self, col_dict: dict) -> dict:
)

return builder.render()

def linked(self, col_dict: dict) -> dict:
builder = PivotTableBuilder(0, col_dict)
builder.add_row("eventName", SortOrderEnum.ASCENDING, show_totals=False)
builder.add_row("firstUserMedium", SortOrderEnum.ASCENDING)
builder.add_row("firstUserSource", SortOrderEnum.ASCENDING)
builder.add_value("eventCount", SummarizeFunctionEnum.SUM)

# =OR(regexmatch(firstUserSource,"linked.com"))
linked = FormulaBuilder(
FormulaEnum.OR,
[
FormulaBuilder(
FormulaEnum.REGEX_MATCH,
["firstUserSource", StringLiteral("linked.com")],
),
FormulaBuilder(
FormulaEnum.REGEX_MATCH,
["firstUserMedium", StringLiteral("linked.com")],
),
],
)

sessions = FormulaBuilder(
FormulaEnum.OR,
[
FormulaBuilder(
FormulaEnum.REGEX_MATCH,
["eventName", StringLiteral("session_start")],
),
FormulaBuilder(
FormulaEnum.REGEX_MATCH, ["eventName", StringLiteral("first_visit")]
),
],
)

builder.add_filter(
"firstUserSource",
FilterTypeEnum.CUSTOM,
values=[UserEnteredValue(linked.render())],
)
builder.add_filter(
"eventName",
FilterTypeEnum.CUSTOM,
values=[UserEnteredValue(sessions.render())],
)

return builder.render()
8 changes: 7 additions & 1 deletion gdrive/sheets_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ def add_pivot_tables(
return response


def add_new_pages(page_names: [str], sheets_id: str):
def add_new_pages(
page_names: [str], sheets_id: str, row_count: int = 1000, column_count: int = 26
):
new_sheets_reqs = []
for label in page_names:
req = {
"addSheet": {
"properties": {
"title": label,
"gridProperties": {
"rowCount": row_count,
"columnCount": column_count,
},
}
}
}
Expand Down

0 comments on commit 90c2d53

Please sign in to comment.