Script 831: Auto Tag Campaigns with Brand or NonBrand
Purpose
Automatically tags campaigns with ‘Brand’ or ‘NonBrand’ based on specific keywords in the campaign name.
To Elaborate
The Python script automatically tags campaigns as ‘Brand’ or ‘NonBrand’ based on specific keywords in the campaign name. If the campaign name contains the keyword ‘marca’, it is tagged as ‘Brand’. If the campaign name contains the keywords ‘curso’, ‘generica’, or ‘oportunidade’, it is tagged as ‘NonBrand’. The script extracts the tag from the campaign name and updates the output dataframe with the extracted tag.
Walking Through the Code
- Define the configurable parameters:
SEP
: The separator used to split the campaign name.TAG_LOCATION
: The position of the tag in the campaign name (comes before the first separator).
- Define the primary data source and columns.
- Define column names for the output dataframe.
- Define a function
extract_tag_from_campaign_name
to extract the tag (Brand or NonBrand) from the campaign name based on specific keywords appearing before the separator. - Copy all input rows to the output dataframe.
- Iterate through all rows in the input dataframe.
- Extract the tag from the campaign name using the
extract_tag_from_campaign_name
function. - Print the campaign name and the extracted tag.
- Update the output dataframe with the extracted tag.
- Drop rows with NaN values in the ‘Brand vs NonBrand’ column.
- Display the resulting dataframe.
Vitals
- Script ID : 831
- Client ID / Customer ID: 1306927165 / 60270223
- Action Type: Bulk Upload
- Item Changed: Campaign
- Output Columns: Account, Campaign, Brand vs NonBrand
- Linked Datasource: M1 Report
- Reference Datasource: None
- Owner: dwaidhas@marinsoftware.com (dwaidhas@marinsoftware.com)
- Created by dwaidhas@marinsoftware.com on 2024-03-20 16:26
- Last Updated by dwaidhas@marinsoftware.com on 2024-03-22 16:41
> See it in Action
Python Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
##
## name: Auto Tag Campaigns with Brand
## description:
## Automatically tags all campaigns that have the word 'marca' in the campaign name with brand (=marca)
##
## author: Dana Waidhas
## created: 2024-03-20
##
# Define the configurable parameters
SEP = '|'
TAG_LOCATION = 1 # Comes before the first separator
# Primary data source and columns
inputDf = dataSourceDict["1"]
# Define column names
RPT_COL_CAMPAIGN = 'Campaign'
RPT_COL_ACCOUNT = 'Account'
RPT_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'
# Output columns and initial values
BULK_COL_ACCOUNT = 'Account'
BULK_COL_CAMPAIGN = 'Campaign'
BULK_COL_BRAND_VS_NONBRAND = 'Brand vs NonBrand'
# Function to extract brand from campaign name
def extract_tag_from_campaign_name(campaign_name, separator='|'):
"""
Extracts the tag (Brand or NonBrand) from the campaign name based on specific keywords appearing before the separator.
Args:
campaign_name (str): The name of the campaign.
separator (str): The separator used to split the campaign name.
Returns:
str or None: The extracted tag (Brand or NonBrand) if found, else None.
"""
# Split the campaign name by the separator
parts = campaign_name.split(separator)
# Get the first part of the campaign name
first_part = parts[TAG_LOCATION - 1].lower() # Adjust index to zero-based
# Check for keywords and return the corresponding tag
if 'marca' in first_part:
return 'Brand'
elif 'curso' in first_part or 'generica' in first_part or 'oportunidade' in first_part:
return 'NonBrand'
else:
return None
# Copy all input rows to output
outputDf = inputDf.copy()
# Iterate through all rows
for index, row in inputDf.iterrows():
campaign_name = row[RPT_COL_CAMPAIGN]
# Extract tag from campaign name
brand = extract_tag_from_campaign_name(campaign_name, separator=SEP)
# Print campaign and brand information
print("Campaign [%s] => Brand [%s]" % (campaign_name, brand))
# Update the output dataframe with the extracted tag
outputDf.at[index, BULK_COL_BRAND_VS_NONBRAND] = brand
# Drop rows with NaN values in the 'Brand vs NonBrand' column
outputDf = outputDf.dropna(subset=[BULK_COL_BRAND_VS_NONBRAND])
# Display the resulting dataframe
if not outputDf.empty:
print("outputDf", tableize(outputDf))
else:
print("Empty outputDf")
Post generated on 2024-05-15 07:44:05 GMT