Skip to content

Commit

Permalink
Support multiple templates per language (#91)
Browse files Browse the repository at this point in the history
* wip

* Refactoring

* Cleanup

* Mandatory columns update
  • Loading branch information
rjambrecic authored Sep 2, 2024
1 parent 77a23f1 commit 89b4af6
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 60 deletions.
3 changes: 3 additions & 0 deletions google_sheets/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ async def get_all_sheet_titles(
"Final Url From",
"Final Url To",
"Language Code",
"Category",
]

MANDATORY_CAMPAIGN_TEMPLATE_COLUMNS = [
Expand All @@ -343,6 +344,7 @@ async def get_all_sheet_titles(

MANDATORY_AD_TEMPLATE_COLUMNS = [
"Language Code",
"Category",
"Headline 1",
"Headline 2",
"Headline 3",
Expand All @@ -352,6 +354,7 @@ async def get_all_sheet_titles(

MANDATORY_KEYWORD_TEMPLATE_COLUMNS = [
"Language Code",
"Category",
"Keyword",
"Keyword Match Type",
"Level",
Expand Down
124 changes: 71 additions & 53 deletions google_sheets/data_processing/processing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Literal
from typing import Any, List, Literal

import pandas as pd

Expand Down Expand Up @@ -32,6 +32,7 @@ def validate_input_data(
INSERT_COUNTRY = "{INSERT_COUNTRY}"
INSERT_CRITERION_TYPE = "{INSERT_CRITERION_TYPE}"
INSERT_LANGUAGE_CODE = "{INSERT_LANGUAGE_CODE}"
INSERT_CATEGORY = "{INSERT_CATEGORY}"


def _update_campaign_name(
Expand All @@ -44,6 +45,7 @@ def _update_campaign_name(
INSERT_COUNTRY=new_campaign_row["Country"],
INSERT_STATION_FROM=new_campaign_row["Station From"],
INSERT_STATION_TO=new_campaign_row["Station To"],
INSERT_CATEGORY=new_campaign_row["Category"],
INSERT_LANGUAGE_CODE=language_code,
INSERT_TARGET_LOCATION=include_locations,
)
Expand Down Expand Up @@ -156,6 +158,70 @@ def process_campaign_data_f(
return final_df


def _use_template_row(category: Any, template_row: pd.Series) -> bool:
if not template_row["Category"]:
return True

return template_row["Category"] == category # type: ignore[no-any-return]


def _process_row(
new_campaign_row: pd.Series,
template_row: pd.Series,
final_df: pd.DataFrame,
target_resource: str,
) -> pd.DataFrame:
if not _use_template_row(new_campaign_row["Category"], template_row):
return final_df

stations = [
{
"Station From": new_campaign_row["Station From"],
"Station To": new_campaign_row["Station To"],
},
# Reverse the order of the stations
{
"Station From": new_campaign_row["Station To"],
"Station To": new_campaign_row["Station From"],
},
]
if target_resource == "ad":
stations[0]["Final Url"] = new_campaign_row["Final Url From"]
stations[1]["Final Url"] = new_campaign_row["Final Url To"]

for station in stations:
new_row = template_row.copy()
include_locations = _get_target_location(new_campaign_row)
new_row["Campaign Name"] = _update_campaign_name(
new_campaign_row,
campaign_name=new_row["Campaign Name"],
language_code=new_row["Language Code"],
include_locations=include_locations,
)

new_row = new_row.str.replace(INSERT_COUNTRY, new_campaign_row["Country"])
new_row = new_row.str.replace(INSERT_STATION_FROM, station["Station From"])
new_row = new_row.str.replace(INSERT_STATION_TO, station["Station To"])
new_row = new_row.str.replace(INSERT_CRITERION_TYPE, new_row["Match Type"])
new_row = new_row.str.replace(INSERT_CATEGORY, new_campaign_row["Category"])

if target_resource == "ad":
new_row["Final URL"] = station["Final Url"]
elif (
target_resource == "keyword"
and new_row["Negative"]
and new_row["Negative"].lower() == "true"
):
new_row["Match Type"] = new_row["Keyword Match Type"]

if "Campaign" in new_row["Level"]:
new_row["Ad Group Name"] = None

final_df = pd.concat([final_df, pd.DataFrame([new_row])], ignore_index=True)

return final_df


def process_data_f(
merged_campaigns_ad_groups_df: pd.DataFrame,
template_df: pd.DataFrame,
Expand Down Expand Up @@ -185,59 +251,11 @@ def process_data_f(
for _, template_row in template_df[
template_df["Language Code"] == new_campaign_row["Language Code"]
].iterrows():
stations = [
{
"Station From": new_campaign_row["Station From"],
"Station To": new_campaign_row["Station To"],
},
# Reverse the order of the stations
{
"Station From": new_campaign_row["Station To"],
"Station To": new_campaign_row["Station From"],
},
]
if target_resource == "ad":
stations[0]["Final Url"] = new_campaign_row["Final Url From"]
stations[1]["Final Url"] = new_campaign_row["Final Url To"]

for station in stations:
new_row = template_row.copy()
include_locations = _get_target_location(new_campaign_row)
new_row["Campaign Name"] = _update_campaign_name(
new_campaign_row,
campaign_name=new_row["Campaign Name"],
language_code=new_row["Language Code"],
include_locations=include_locations,
)

new_row = new_row.str.replace(
INSERT_COUNTRY, new_campaign_row["Country"]
)
new_row = new_row.str.replace(
INSERT_STATION_FROM, station["Station From"]
)
new_row = new_row.str.replace(INSERT_STATION_TO, station["Station To"])
new_row = new_row.str.replace(
INSERT_CRITERION_TYPE, new_row["Match Type"]
)

if target_resource == "ad":
new_row["Final URL"] = station["Final Url"]
elif (
target_resource == "keyword"
and new_row["Negative"]
and new_row["Negative"].lower() == "true"
):
new_row["Match Type"] = new_row["Keyword Match Type"]

if "Campaign" in new_row["Level"]:
new_row["Ad Group Name"] = None

final_df = pd.concat(
[final_df, pd.DataFrame([new_row])], ignore_index=True
)
final_df = _process_row(
new_campaign_row, template_row, final_df, target_resource
)

final_df = final_df.drop(columns=["Language Code"])
final_df = final_df.drop(columns=["Language Code", "Category"])
if target_resource == "keyword":
final_df = final_df.drop(columns=["Keyword Match Type"])
final_df = final_df.drop_duplicates(ignore_index=True)
Expand Down
17 changes: 13 additions & 4 deletions tests/app/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class TestProcessCampaignData:
"Default max. CPC",
],
[
"{INSERT_COUNTRY} - {INSERT_STATION_FROM} - {INSERT_STATION_TO}",
"{INSERT_COUNTRY} - {INSERT_STATION_FROM} - {INSERT_STATION_TO} | {INSERT_CATEGORY}",
"EN",
100,
True,
Expand All @@ -274,6 +274,7 @@ class TestProcessCampaignData:
"Final Url From",
"Final Url To",
"Language Code",
"Category",
],
[
"India",
Expand All @@ -282,6 +283,7 @@ class TestProcessCampaignData:
"https://www.example.com/from",
"https://www.example.com/to",
"EN",
"Bus",
],
]
),
Expand All @@ -295,7 +297,7 @@ class TestProcessCampaignData:
"Google Search Network",
"Default max. CPC",
],
["India - Delhi - Mumbai", "EN", 100, True, False, 1.2],
["India - Delhi - Mumbai | Bus", "EN", 100, True, False, 1.2],
],
),
),
Expand Down Expand Up @@ -391,9 +393,10 @@ class TestProcessData:
"Level",
"Negative",
"Language Code",
"Category",
],
["Keyword A", "Exact", None, "False", "EN"],
["Keyword N", "Broad", "Campaign", "True", "EN"],
["Keyword A", "Exact", None, "False", "EN", "Bus"],
["Keyword N", "Broad", "Campaign", "True", "EN", "Bus"],
]
),
GoogleSheetValues(
Expand All @@ -405,6 +408,7 @@ class TestProcessData:
"Final Url From",
"Final Url To",
"Language Code",
"Category",
],
[
"India",
Expand All @@ -413,6 +417,7 @@ class TestProcessData:
"https://www.example.com/from",
"https://www.example.com/to",
"EN",
"Bus",
],
]
),
Expand Down Expand Up @@ -497,6 +502,7 @@ async def test_process_data_ads(self) -> None:
values=[
[
"Language Code",
"Category",
"Final URL",
"Headline 1",
"Headline 2",
Expand All @@ -508,6 +514,7 @@ async def test_process_data_ads(self) -> None:
],
[
"EN",
"Bus",
"https://www.example.com/from",
"H" * 31,
"Headline 2",
Expand All @@ -528,6 +535,7 @@ async def test_process_data_ads(self) -> None:
"Final Url From",
"Final Url To",
"Language Code",
"Category",
],
[
"India",
Expand All @@ -536,6 +544,7 @@ async def test_process_data_ads(self) -> None:
"https://www.example.com/from",
"https://www.example.com/to",
"EN",
"Bus",
],
]
)
Expand Down
Loading

0 comments on commit 89b4af6

Please sign in to comment.